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 2 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
12 changes: 9 additions & 3 deletions pex/pex_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,17 @@ 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):
__entry_point__ = __entry_point_from_filename__(__loader__.get_filename())
else:
try:
from importlib.abc import Loader
if isinstance(__loader__, Loader):
__entry_point__ = __entry_point_from_filename__(__loader__.get_filename())
except ImportError:
Copy link
Member Author

Choose a reason for hiding this comment

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

This order is a bit better since it stresses the code now as well as getting rid of warnings under newer Pythons. It penalizes existing Python 2.7 users with a small speed bump for the cold cache case in the ~ms range though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright - took the feedback but went further given the prompt to think.

from pkgutil import ImpLoader
if isinstance(__loader__, ImpLoader):
__entry_point__ = __entry_point_from_filename__(__loader__.get_filename())

if __entry_point__ is None:
sys.stderr.write('Could not launch python executable!\\n')
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