From 271403f2e4886814f2893f46334a04512e4ed134 Mon Sep 17 00:00:00 2001 From: Peter Ebden Date: Mon, 18 Nov 2024 11:45:27 +0000 Subject: [PATCH 1/3] Update SoImport to use newer interfaces --- tools/please_pex/pex/pex_main.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/tools/please_pex/pex/pex_main.py b/tools/please_pex/pex/pex_main.py index cc7ca69..7cf3407 100644 --- a/tools/please_pex/pex/pex_main.py +++ b/tools/please_pex/pex/pex_main.py @@ -10,6 +10,7 @@ import re import runpy import sys +import tempfile import zipfile @@ -97,30 +98,29 @@ 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): + """Executes a module object once created.""" + # 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: From b479ed5397097a6fcf04c7624fe0abfc7bc15bf8 Mon Sep 17 00:00:00 2001 From: Peter Ebden Date: Mon, 18 Nov 2024 11:54:46 +0000 Subject: [PATCH 2/3] And for ModuleDirImport --- test/BUILD | 8 ++++++++ test/module_dir_import_test.py | 9 +++++++++ tools/please_pex/pex/pex_main.py | 25 ++++++++++--------------- 3 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 test/module_dir_import_test.py diff --git a/test/BUILD b/test/BUILD index fffefc2..30a159b 100644 --- a/test/BUILD +++ b/test/BUILD @@ -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", + ], +) diff --git a/test/module_dir_import_test.py b/test/module_dir_import_test.py new file mode 100644 index 0000000..54a4df4 --- /dev/null +++ b/test/module_dir_import_test.py @@ -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) diff --git a/tools/please_pex/pex/pex_main.py b/tools/please_pex/pex/pex_main.py index 7cf3407..75b23b3 100644 --- a/tools/please_pex/pex/pex_main.py +++ b/tools/please_pex/pex/pex_main.py @@ -102,7 +102,7 @@ def find_spec(self, name, path, target=None): return spec_from_loader(name, self) def create_module(self, spec): - """Create a module object that we're going to load..""" + """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: @@ -117,8 +117,7 @@ def create_module(self, spec): return mod def exec_module(self, mod): - """Executes a module object once created.""" - # Because we set spec.loader above, the ExtensionFileLoader's exec_module is called. + """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): @@ -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``. From c50bedd4e6e9ae16bc428034540fa5f9d62e0a15 Mon Sep 17 00:00:00 2001 From: Peter Ebden Date: Mon, 18 Nov 2024 12:56:47 +0000 Subject: [PATCH 3/3] version --- tools/ChangeLog | 4 ++++ tools/VERSION | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/ChangeLog b/tools/ChangeLog index 556ee86..be0fcee 100644 --- a/tools/ChangeLog +++ b/tools/ChangeLog @@ -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) diff --git a/tools/VERSION b/tools/VERSION index 4cda8f1..8af85be 100644 --- a/tools/VERSION +++ b/tools/VERSION @@ -1 +1 @@ -1.5.2 +1.5.3