8000 Merge pull request #2633 from webknjaz/bugfixes/2632-importlib-find_spec · pypa/setuptools@fe10ebf · GitHub
[go: up one dir, main page]

Skip to content

Commit fe10ebf

Browse files
authored
Merge pull request #2633 from webknjaz/bugfixes/2632-importlib-find_spec
Implement `find_spec` in vendored module importers
2 parents ee6a5ff + b5f3ae5 commit fe10ebf

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

changelog.d/2632.change.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Implemented ``VendorImporter.find_spec()`` method to get rid
2+
of ``ImportWarning`` that Python 3.10 emits when only the old-style
3+
importer hooks are present -- by :user:`webknjaz`

pkg_resources/extern/__init__.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib.util
12
import sys
23

34

@@ -20,17 +21,10 @@ def search_path(self):
2021
yield self.vendor_pkg + '.'
2122
yield ''
2223

23-
def find_module(self, fullname, path=None):
24-
"""
25-
Return self when fullname starts with root_name and the
26-
target module is one vendored through this importer.
27-
"""
24+
def _module_matches_namespace(self, fullname):
25+
"""Figure out if the target module is vendored."""
2826
root, base, target = fullname.partition(self.root_name + '.')
29-
if root:
30-
return
31-
if not any(map(target.startswith, self.vendored_names)):
32-
return
33-
return self
27+
return not root and any(map(target.startswith, self.vendored_names))
3428

3529
def load_module(self, fullname):
3630
"""
@@ -60,6 +54,13 @@ def create_module(self, spec):
6054
def exec_module(self, module):
6155
pass
6256

57+
def find_spec(self, fullname, path=None, target=None):
58+
"""Return a module spec for vendored names."""
59+
return (
60+
importlib.util.spec_from_loader(fullname, self)
61+
if self._module_matches_namespace(fullname) else None
62+
)
63+
6364
def install(self):
6465
"""
6566
Install this importer into sys.meta_path if not already present.

setuptools/extern/__init__.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib.util
12
import sys
23

34

@@ -20,17 +21,10 @@ def search_path(self):
2021
yield self.vendor_pkg + '.'
2122
yield ''
2223

23-
def find_module(self, fullname, path=None):
24-
"""
25-
Return self when fullname starts with root_name and the
26-
target module is one vendored through this importer.
27-
"""
24+
def _module_matches_namespace(self, fullname):
25+
"""Figure out if the target module is vendored."""
2826
root, base, target = fullname.partition(self.root_name + '.')
29-
if root:
30-
return
31-
if not any(map(target.startswith, self.vendored_names)):
32-
return
33-
return self
27+
return not root and any(map(target.startswith, self.vendored_names))
3428

3529
def load_module(self, fullname):
3630
"""
@@ -60,6 +54,13 @@ def create_module(self, spec):
6054
def exec_module(self, module):
6155
pass
6256

57+
def find_spec(self, fullname, path=None, target=None):
58+
"""Return a module spec for vendored names."""
59+
return (
60+
importlib.util.spec_from_loader(fullname, self)
61+
if self._module_matches_namespace(fullname) else None
62+
)
63+
6364
def ins 41E2 tall(self):
6465
"""
6566
Install this importer into sys.meta_path if not already present.

0 commit comments

Comments
 (0)
0