From f425f09527d871b4bd2624912e5c8dee6b0d08d6 Mon Sep 17 00:00:00 2001 From: Carlos Quiros Date: Thu, 29 Aug 2024 15:12:51 -0400 Subject: [PATCH 1/2] Migrate imp to importlib --- pyutilib/component/loader/plugin_importLoader.py | 14 ++++++++++++-- pyutilib/misc/import_file.py | 10 +++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pyutilib/component/loader/plugin_importLoader.py b/pyutilib/component/loader/plugin_importLoader.py index a0819f48..66b2e4c9 100644 --- a/pyutilib/component/loader/plugin_importLoader.py +++ b/pyutilib/component/loader/plugin_importLoader.py @@ -13,7 +13,8 @@ __all__ = ['ImportLoader'] from glob import glob -import imp +import importlib.util +import importlib.machinery import re import os import sys @@ -22,6 +23,15 @@ from pyutilib.component.config import ManagedSingletonPlugin from pyutilib.component.core import implements, ExtensionPoint, IIgnorePluginWhenLoading, IPluginLoader, Plugin +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module class ImportLoader(ManagedSingletonPlugin): """Loader that looks for Python source files in the plugins directories, @@ -53,7 +63,7 @@ def load(self, env, search_path, disable_re, name_re): if plugin_name not in sys.modules and name_re.match( plugin_name): try: - module = imp.load_source(plugin_name, plugin_file) + module = load_source(plugin_name, plugin_file) if generate_debug_messages: env.log.debug('Loading file plugin %s from %s' % \ (plugin_name, plugin_file)) diff --git a/pyutilib/misc/import_file.py b/pyutilib/misc/import_file.py index cd5c80ab..cab36121 100644 --- a/pyutilib/misc/import_file.py +++ b/pyutilib/misc/import_file.py @@ -8,7 +8,7 @@ # _________________________________________________________________________ import os -import imp +import importlib import sys import pyutilib.common @@ -125,15 +125,15 @@ def import_file(filename, context=None, name=None, clear_cache=False): else: if dirname is not None: # find_module will return the .py file (never .pyc) - fp, pathname, description = imp.find_module(modulename, - [dirname]) + fp, pathname, description = importlib.find_loader(modulename, + str(dirname)) fp.close() else: try: sys.path.insert(0, implied_dirname) # find_module will return the .py file # (never .pyc) - fp, pathname, description = imp.find_module(modulename) + fp, pathname, description = importlib.find_loader(modulename) fp.close() except ImportError: raise @@ -142,7 +142,7 @@ def import_file(filename, context=None, name=None, clear_cache=False): try: # Note: we are always handing load_source a .py file, but # it will use the .pyc or .pyo file if it exists - module = imp.load_source(name, pathname) + module = importlib.find_loader(name, pathname) except: et, e, tb = sys.exc_info() import traceback From 171d3c84d4b159b5ffd96c4888d005249c96a21c Mon Sep 17 00:00:00 2001 From: Carlos Quiros Date: Thu, 29 Aug 2024 15:29:53 -0400 Subject: [PATCH 2/2] Use load_source --- pyutilib/misc/import_file.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pyutilib/misc/import_file.py b/pyutilib/misc/import_file.py index cab36121..052b8735 100644 --- a/pyutilib/misc/import_file.py +++ b/pyutilib/misc/import_file.py @@ -9,6 +9,7 @@ import os import importlib +import importlib.machinery import sys import pyutilib.common @@ -24,6 +25,17 @@ runpy_available = False +def load_source(modname, filename): + loader = importlib.machinery.SourceFileLoader(modname, filename) + spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) + module = importlib.util.module_from_spec(spec) + # The module is always executed and not cached in sys.modules. + # Uncomment the following line to cache the module. + # sys.modules[module.__name__] = module + loader.exec_module(module) + return module + + def import_file(filename, context=None, name=None, clear_cache=False): """ Import a Python file as a module @@ -142,7 +154,7 @@ def import_file(filename, context=None, name=None, clear_cache=False): try: # Note: we are always handing load_source a .py file, but # it will use the .pyc or .pyo file if it exists - module = importlib.find_loader(name, pathname) + module = load_source(name, pathname) except: et, e, tb = sys.exc_info() import traceback