Skip to content

Commit

Permalink
Merge pull request #2633 from webknjaz/bugfixes/2632-importlib-find_spec
Browse files Browse the repository at this point in the history
Implement `find_spec` in vendored module importers
  • Loading branch information
jaraco authored Apr 8, 2021
2 parents ee6a5ff + b5f3ae5 commit fe10ebf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
3 changes: 3 additions & 0 deletions changelog.d/2632.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Implemented ``VendorImporter.find_spec()`` method to get rid
of ``ImportWarning`` that Python 3.10 emits when only the old-style
importer hooks are present -- by :user:`webknjaz`
21 changes: 11 additions & 10 deletions pkg_resources/extern/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib.util
import sys


Expand All @@ -20,17 +21,10 @@ def search_path(self):
yield self.vendor_pkg + '.'
yield ''

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.
"""
def _module_matches_namespace(self, fullname):
"""Figure out if the target module is vendored."""
root, base, target = fullname.partition(self.root_name + '.')
if root:
return
if not any(map(target.startswith, self.vendored_names)):
return
return self
return not root and any(map(target.startswith, self.vendored_names))

def load_module(self, fullname):
"""
Expand Down Expand Up @@ -60,6 +54,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.util.spec_from_loader(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: 11 additions & 10 deletions setuptools/extern/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib.util
import sys


Expand All @@ -20,17 +21,10 @@ def search_path(self):
yield self.vendor_pkg + '.'
yield ''

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.
"""
def _module_matches_namespace(self, fullname):
"""Figure out if the target module is vendored."""
root, base, target = fullname.partition(self.root_name + '.')
if root:
return
if not any(map(target.startswith, self.vendored_names)):
return
return self
return not root and any(map(target.startswith, self.vendored_names))

def load_module(self, fullname):
"""
Expand Down Expand Up @@ -60,6 +54,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.util.spec_from_loader(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 fe10ebf

Please sign in to comment.