From 7ed7d8c8a4e90990c09fe4ad3ad63cd2a2c73dee Mon Sep 17 00:00:00 2001 From: pkludig <128155050+pkludig@users.noreply.github.com> Date: Thu, 22 Feb 2024 10:39:12 +0100 Subject: [PATCH] Add Python 3.12 compatibility (#14) * Update _SixMetaPathImporter to work with python 3.12 * Use r-strings to avoid syntax warning * Add docu * Add old methods to ensure backwards compatibility * move import into find spec function to avoid calling it with older python versions * Remove changes introduced by autoformatting the code * Remove changes introduced by autoformatting the code the second --- pyxb/namespace/archive.py | 2 +- pyxb/utils/six.py | 29 +++++++++++++++++++++++++---- setup.py | 4 ++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/pyxb/namespace/archive.py b/pyxb/namespace/archive.py index b6809eb0..24191680 100644 --- a/pyxb/namespace/archive.py +++ b/pyxb/namespace/archive.py @@ -110,7 +110,7 @@ def __GetArchiveInstance (cls, archive_file, stage=None): rv._readToStage(stage) return rv - __ArchivePattern_re = re.compile('\.wxs$') + __ArchivePattern_re = re.compile(r'\.wxs$') @classmethod def PreLoadArchives (cls, archive_path=None, reset=False): diff --git a/pyxb/utils/six.py b/pyxb/utils/six.py index 34567fc3..cfcda03a 100644 --- a/pyxb/utils/six.py +++ b/pyxb/utils/six.py @@ -180,13 +180,15 @@ def _resolve(self): return getattr(module, self.attr) -class _SixMetaPathImporter(object): - +class _SixMetaPathImporter: """ A meta path importer to import six.moves and its submodules. - This class implements a PEP302 finder and loader. It should be compatible - with Python 2.5 and all existing versions of Python3 + This class implemented a PEP 451 finder and loader. It should be compatible + with Python 3.4 onwards. + + Also contains the finder and loader for a PEP 302 import system, which is + used by Python 3.3 and earlier. """ def __init__(self, six_module_name): @@ -205,12 +207,31 @@ def find_module(self, fullname, path=None): return self return None + def find_spec(self, fullname, path=None, target=None): + import importlib.util + if fullname in self.known_modules: + spec = importlib.util.spec_from_loader(fullname, self) + return spec + return None + def __get_module(self, fullname): try: return self.known_modules[fullname] except KeyError: raise ImportError("This loader does not know module " + fullname) + def create_module(self, spec): + return None + + def exec_module(self, module): + fullname = module.__name__ + mod = self.__get_module(fullname) + if isinstance(mod, MovedModule): + mod = mod._resolve() + else: + mod.__loader__ = self + sys.modules[fullname] = mod + def load_module(self, fullname): try: # in case of a reload diff --git a/setup.py b/setup.py index 850d14db..1b9f5836 100755 --- a/setup.py +++ b/setup.py @@ -42,8 +42,8 @@ ] package_data = {} -init_re = re.compile("^__init__\.py$") -wxs_re = re.compile("^.*\.wxs$") +init_re = re.compile(r"^__init__\.py$") +wxs_re = re.compile(r"^.*\.wxs$") setup_path = os.path.dirname(__file__) bundle_base = os.path.join(setup_path, "pyxb", "bundles")