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

Get rid of load_module from import hooks #222

Merged
merged 3 commits into from
Nov 18, 2024
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
8 changes: 8 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,11 @@ python_test(
name = "interpreter_not_included_test",
srcs = ["interpreter_not_included_test.py"],
)

python_test(
name = "module_dir_import_test",
srcs = ["module_dir_import_test.py"],
deps = [
"//third_party/python:six",
],
)
9 changes: 9 additions & 0 deletions test/module_dir_import_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import unittest


class TestModuleDirImport(unittest.TestCase):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Weirdly we didn't seem to have anything that tested this sufficiently directly - I would have thought we must have but I got ModuleDirImport through them all with a deliberately busted implementation of exec_module...


def test_imports_are_the_same(self):
import six
import third_party.python.six
self.assertEqual(six, third_party.python.six)
4 changes: 4 additions & 0 deletions tools/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Version 1.5.3
-------------
* Updated some deprecated functions on import hooks (#222)

Version 1.5.2
-------------
* Performance improvements to please_pex (#219, #220)
Expand Down
2 changes: 1 addition & 1 deletion tools/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.2
1.5.3
49 changes: 22 additions & 27 deletions tools/please_pex/pex/pex_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import re
import runpy
import sys
import tempfile
import zipfile


Expand Down Expand Up @@ -97,30 +98,28 @@ def __init__(self):

def find_spec(self, name, path, target=None):
"""Implements abc.MetaPathFinder."""
loader = self.find_module(name, path)
if loader is None:
return None
return spec_from_loader(name, loader)
if name in self.modules:
return spec_from_loader(name, self)

def find_module(self, fullname, path=None):
"""Attempt to locate module. Returns self if found, None if not."""
if fullname in self.modules:
return self

def load_module(self, fullname):
"""Actually load a module that we said we'd handle in find_module."""
import tempfile

filename = self.modules[fullname]
def create_module(self, spec):
"""Create a module object that we're going to load."""
filename = self.modules[spec.name]
prefix, ext = self.splitext(filename)
with tempfile.NamedTemporaryFile(suffix=ext, prefix=os.path.basename(prefix)) as f:
f.write(self.zf.read(filename))
f.flush()
mod = machinery.ExtensionFileLoader(fullname, f.name).load_module()
spec.origin = f.name
loader = machinery.ExtensionFileLoader(spec.name, f.name)
spec.loader = loader
mod = loader.create_module(spec)
# Make it look like module came from the original location for nicer tracebacks.
mod.__file__ = filename
return mod

def exec_module(self, mod):
"""Because we set spec.loader above, the ExtensionFileLoader's exec_module is called."""
raise NotImplementedError("SoImport.exec_module isn't used")

def splitext(self, path):
"""Similar to os.path.splitext, but splits our longest known suffix preferentially."""
for suffix in self.suffixes_by_length:
Expand Down Expand Up @@ -187,22 +186,18 @@ def _find_all_distributions(self, module_dir):

def find_spec(self, name, path, target=None):
"""Implements abc.MetaPathFinder."""
loader = self.find_module(name, path)
if loader is None:
return None
return spec_from_loader(name, loader)

def find_module(self, fullname, path=None):
"""Attempt to locate module. Returns self if found, None if not."""
if fullname.startswith(self.prefix):
return self
if name.startswith(self.prefix):
return spec_from_loader(name, self)

def load_module(self, fullname):
def create_module(self, spec):
"""Actually load a module that we said we'd handle in find_module."""
module = import_module(fullname[len(self.prefix):])
sys.modules[fullname] = module
module = import_module(spec.name[len(self.prefix):])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think maybe this is mildly not nice (it kind of does everything in create_module and leaves nothing for exec_module) but I can't find a convenient equivalent to break it into the two parts.

sys.modules[spec.name] = module
return module

def exec_module(self, mod):
"""Nothing to do, create_module already did the work."""

def find_distributions(self, context):
"""Return an iterable of all Distribution instances capable of
loading the metadata for packages for the indicated ``context``.
Expand Down
Loading