Skip to content

Commit

Permalink
Add Python 3.12 compatibility (#14)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pkludig authored Feb 22, 2024
1 parent 45bb9a0 commit 7ed7d8c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyxb/namespace/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
29 changes: 25 additions & 4 deletions pyxb/utils/six.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 7ed7d8c

Please sign in to comment.