From f2ed88371e6f236237267476ee72a064ae0cd70f Mon Sep 17 00:00:00 2001 From: "Jose E. Roman" Date: Tue, 20 Jun 2023 17:31:19 +0200 Subject: [PATCH] Fix for python-3.12: 'imp' module has been removed Fixes #1401 --- config/BuildSystem/script.py | 11 +++--- lib/petsc/bin/petsc_tas_analysis.py | 1 - .../petsc4py/src/petsc4py/lib/__init__.py | 38 ++++++++++++++----- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/config/BuildSystem/script.py b/config/BuildSystem/script.py index 5dbf2934fff..f120d335712 100644 --- a/config/BuildSystem/script.py +++ b/config/BuildSystem/script.py @@ -132,13 +132,12 @@ class Script(logger.Logger): @staticmethod def getModule(root, name): '''Retrieve a specific module from the directory root, bypassing the usual paths''' - import imp + import importlib.util - (fp, pathname, description) = imp.find_module(name, [root]) - try: - return imp.load_module(name, fp, pathname, description) - finally: - if fp: fp.close() + spec = importlib.util.spec_from_file_location(name, root) + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + spec.loader.exec_module(module) @staticmethod def importModule(moduleName): diff --git a/lib/petsc/bin/petsc_tas_analysis.py b/lib/petsc/bin/petsc_tas_analysis.py index 7a74fd06d31..bb1c3e62b55 100755 --- a/lib/petsc/bin/petsc_tas_analysis.py +++ b/lib/petsc/bin/petsc_tas_analysis.py @@ -4,7 +4,6 @@ import os import sys import importlib import datetime as date -import importlib # Check to ensure that the environmental variable PETSC_DIR has been assigned. # MPLCONFIGDIR is needed for matplotlib diff --git a/src/lib/__init__.py b/src/lib/__init__.py index 85e9daf118e..310334eae21 100644 --- a/src/lib/__init__.py +++ b/src/lib/__init__.py @@ -13,7 +13,7 @@ This package is a holds all the available variants of the PETSc extension module built against specific PETSc configurations. It also -provides a convenience function using of the builtin ``imp`` module +provides a convenience function using of the ``importlib`` module for easily importing any of the available extension modules depending on the value of a user-provided configuration name, the ``PETSC_ARCH`` environmental variable, or a configuration file. @@ -45,19 +45,35 @@ Import helper for PETSc-based extension modules. """ import sys, os, warnings - # TODO: use 'importlib' module under Python 3 - with warnings.catch_warnings(): - warnings.filterwarnings("ignore") + try: + import importlib.machinery + import importlib.util + except ImportError: + importlib = None import imp def get_ext_suffix(): - return imp.get_suffixes()[0][0] + if importlib: + return importlib.machinery.EXTENSION_SUFFIXES[0] + else: + return imp.get_suffixes()[0][0] + def import_module(pkg, name, path, arch): - fullname = '%s.%s' % (pkg, name) + fullname = '{}.{}'.format(pkg, name) pathlist = [os.path.join(path, arch)] - f, fn, info = imp.find_module(name, pathlist) - with f: return imp.load_module(fullname, f, fn, info) + if importlib: + finder = importlib.machinery.PathFinder() + spec = finder.find_spec(fullname, pathlist) + module = importlib.util.module_from_spec(spec) + sys.modules[fullname] = module + spec.loader.exec_module(module) + return module + else: + f, fn, info = imp.find_module(name, pathlist) + with f: + return imp.load_module(fullname, f, fn, info) + # test if extension module was already imported - module = sys.modules.get('%s.%s' % (pkg, name)) + module = sys.modules.get('{}.{}'.format(pkg, name)) filename = getattr(module, '__file__', '') if filename.endswith(get_ext_suffix()): # if 'arch' is None, do nothing; otherwise this @@ -66,6 +82,7 @@ if arch is not None and arch != module.__arch__: raise ImportError("%s already imported" % module) return module + # silence annoying Cython warning warnings.filterwarnings("ignore", message="numpy.dtype size changed") warnings.filterwarnings("ignore", message="numpy.ndarray size changed") From c36e9c688a542448939d85251c93951e001443de Mon Sep 17 00:00:00 2001 From: "Jose E. Roman" Date: Thu, 22 Jun 2023 10:28:53 +0200 Subject: [PATCH 2/2] script.py: improve portability for python < 3.5 --- config/BuildSystem/script.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/config/BuildSystem/script.py b/config/BuildSystem/script.py index f120d335712..d3305288195 100644 --- a/config/BuildSystem/script.py +++ b/config/BuildSystem/script.py @@ -132,12 +132,23 @@ class Script(logger.Logger): @staticmethod def getModule(root, name): '''Retrieve a specific module from the directory root, bypassing the usual paths''' - import importlib.util - - spec = importlib.util.spec_from_file_location(name, root) - module = importlib.util.module_from_spec(spec) - sys.modules[name] = module - spec.loader.exec_module(module) + try: + import importlib.util + except ImportError: + importlib = None + import imp + + if importlib and sys.version_info > (3,4): + spec = importlib.util.spec_from_file_location(name, root) + module = importlib.util.module_from_spec(spec) # novermin + sys.modules[name] = module + spec.loader.exec_module(module) + else: + (fp, pathname, description) = imp.find_module(name, [root]) + try: + return imp.load_module(name, fp, pathname, description) + finally: + if fp: fp.close() @staticmethod def importModule(moduleName): -- GitLab