Skip to content

Commit

Permalink
Implement find_spec in vendored module importers
Browse files Browse the repository at this point in the history
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
* pypa#2632 (comment)

Fixes pypa#2632

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
  • Loading branch information
webknjaz and jaraco committed Apr 8, 2021
1 parent c5185cd commit c826dff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
21 changes: 15 additions & 6 deletions pkg_resources/extern/__init__.py
@@ -1,3 +1,4 @@
import importlib.machinery
import sys


Expand All @@ -20,17 +21,18 @@ def search_path(self):
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):
"""
Expand Down Expand Up @@ -60,6 +62,13 @@ def create_module(self, spec):
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.
Expand Down
21 changes: 15 additions & 6 deletions setuptools/extern/__init__.py
@@ -1,3 +1,4 @@
import importlib.machinery
import sys


Expand All @@ -20,17 +21,18 @@ def search_path(self):
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):
"""
Expand Down Expand Up @@ -60,6 +62,13 @@ def create_module(self, spec):
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.
Expand Down

0 comments on commit c826dff

Please sign in to comment.