Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to removed stdlib APIs in Python 3.12. #2170

Merged
merged 4 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pex/pex_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ def __entry_point_from_filename__(filename):
if '__file__' in locals() and __file__ is not None and os.path.exists(__file__):
__entry_point__ = __entry_point_from_filename__(__file__)
elif '__loader__' in locals():
from pkgutil import ImpLoader
if hasattr(__loader__, 'archive'):
__entry_point__ = __loader__.archive
elif isinstance(__loader__, ImpLoader):
elif hasattr(__loader__, 'get_filename'):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's still true Pex contains vendored code that uses ImpLoader (inside Pip), this adjustment as well as the support for find_spec / PEP-451 below is enough to close #2137.

# The source of the loader interface has changed over the course of Python history from
# `pkgutil.ImpLoader` to `importlib.abc.Loader`, but the existence and semantics of
# `get_filename` has remained constant; so we just check for the method.
__entry_point__ = __entry_point_from_filename__(__loader__.get_filename())

if __entry_point__ is None:
Expand Down
11 changes: 10 additions & 1 deletion pex/third_party/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,16 @@ def uninstall(self):
loader.unload()
_tracer().log("Uninstalled {}".format(self), V=3)

# The PEP-302 finder API.
def find_spec(self, fullname, path, target=None):
# Python 2.7 does not know about this API and does not use it.
from importlib.util import spec_from_loader # type: ignore[import]

loader = self.find_module(fullname, path)
if loader:
return spec_from_loader(fullname, loader)
return None

# The Legacy PEP-302 finder API.
# See: https://www.python.org/dev/peps/pep-0302/#specification-part-1-the-importer-protocol
def find_module(self, fullname, path=None):
for importable in self._importables:
Expand Down