changeset 984:e7ca3c577305

fix: Compatibility for Python 3.4.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 30 Oct 2017 19:23:10 -0700
parents 7fe1151595bf
children 74f4bde40fd0
files piecrust/plugins/base.py
diffstat 1 files changed, 14 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/plugins/base.py	Mon Oct 30 18:58:28 2017 -0700
+++ b/piecrust/plugins/base.py	Mon Oct 30 19:23:10 2017 -0700
@@ -2,7 +2,6 @@
 import sys
 import logging
 import importlib
-import importlib.util
 
 
 logger = logging.getLogger(__name__)
@@ -135,11 +134,20 @@
             # Import as a loose Python file from the plugins dir.
             pfile = os.path.join(self.app.plugins_dir, plugin_name + '.py')
             if os.path.isfile(pfile):
-                spec = importlib.util.spec_from_file_location(plugin_name,
-                                                              pfile)
-                mod = importlib.util.module_from_spec(spec)
-                spec.loader.exec_module(mod)
-                sys.modules[mod_name] = mod
+                if sys.version_info[1] >= 5:
+                    # Python 3.5+
+                    from importlib.util import (spec_from_file_location,
+                                                module_from_spec)
+                    spec = spec_from_file_location(plugin_name, pfile)
+                    mod = module_from_spec(spec)
+                    spec.loader.exec_module(mod)
+                    sys.modules[mod_name] = mod
+                else:
+                    # Python 3.4, 3.3.
+                    from importlib.machinery import SourceFileLoader
+                    mod = SourceFileLoader(
+                        plugin_name, pfile).load_module()
+                    sys.modules[mod_name] = mod
 
         if mod is None:
             logger.error("Failed to load plugin '%s'." % plugin_name)