diff options
author | Sviatoslav Sydorenko <wk@sydorenko.org.ua> | 2021-04-08 19:58:24 +0200 |
---|---|---|
committer | Sviatoslav Sydorenko <wk@sydorenko.org.ua> | 2021-04-08 23:42:24 +0200 |
commit | c826dffc2fd99de0431ee2a8b483868ab54e9cdc (patch) | |
tree | c2ba2a1609bfe40a9cf83893646d2b848f86b28d /setuptools/extern | |
parent | c5185cd00cc3a96bad4cf5bca3968af710916d3a (diff) | |
download | python-setuptools-git-c826dffc2fd99de0431ee2a8b483868ab54e9cdc.tar.gz |
Implement `find_spec` in vendored module importers
This change makes the import warning emitted by Python 3.10 disappear
but implementing the hook that is supposed to replace the old import
mechanism.
Refs:
* https://bugs.python.org/issue42134
* https://bugs.python.org/issue43540
* https://github.com/pypa/setuptools/issues/2632#issuecomment-815701078
Fixes #2632
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Diffstat (limited to 'setuptools/extern')
-rw-r--r-- | setuptools/extern/__init__.py | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/setuptools/extern/__init__.py b/setuptools/extern/__init__.py index 399701a0..bd8636c7 100644 --- a/setuptools/extern/__init__.py +++ b/setuptools/extern/__init__.py @@ -1,3 +1,4 @@ +import importlib.machinery import sys @@ -20,17 +21,18 @@ class VendorImporter: yield self.vendor_pkg + '.' yield '' + def _module_matches_namespace(self, fullname): + """Figure out if the target module is vendored.""" + root, base, target = fullname.partition(self.root_name + '.') + return not root and any(map(target.startswith, self.vendored_names)) + def find_module(self, fullname, path=None): """ Return self when fullname starts with root_name and the target module is one vendored through this importer. """ - root, base, target = fullname.partition(self.root_name + '.') - if root: - return - if not any(map(target.startswith, self.vendored_names)): - return - return self + spec = self.find_spec(fullname, path) + return spec.loader if spec is not None else None def load_module(self, fullname): """ @@ -60,6 +62,13 @@ class VendorImporter: def exec_module(self, module): pass + def find_spec(self, fullname, path=None, target=None): + """Return a module spec for vendored names.""" + return ( + importlib.machinery.ModuleSpec(fullname, self) + if self._module_matches_namespace(fullname) else None + ) + def install(self): """ Install this importer into sys.meta_path if not already present. |