Skip to content

Commit

Permalink
Get rid of load_module from import hooks (#222)
Browse files Browse the repository at this point in the history
* Update SoImport to use newer interfaces

* And for ModuleDirImport

* version
  • Loading branch information
peterebden authored Nov 18, 2024
1 parent 0f09c62 commit 0452499
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
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):

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):])
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

0 comments on commit 0452499

Please sign in to comment.