Skip to content

Commit 0452499

Browse files
authored
Get rid of load_module from import hooks (#222)
* Update SoImport to use newer interfaces * And for ModuleDirImport * version
1 parent 0f09c62 commit 0452499

File tree

5 files changed

+44
-28
lines changed

5 files changed

+44
-28
lines changed

test/BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,11 @@ python_test(
185185
name = "interpreter_not_included_test",
186186
srcs = ["interpreter_not_included_test.py"],
187187
)
188+
189+
python_test(
190+
name = "module_dir_import_test",
191+
srcs = ["module_dir_import_test.py"],
192+
deps = [
193+
"//third_party/python:six",
194+
],
195+
)

test/module_dir_import_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import unittest
2+
3+
4+
class TestModuleDirImport(unittest.TestCase):
5+
6+
def test_imports_are_the_same(self):
7+
import six
8+
import third_party.python.six
9+
self.assertEqual(six, third_party.python.six)

tools/ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Version 1.5.3
2+
-------------
3+
* Updated some deprecated functions on import hooks (#222)
4+
15
Version 1.5.2
26
-------------
37
* Performance improvements to please_pex (#219, #220)

tools/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.5.2
1+
1.5.3

tools/please_pex/pex/pex_main.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import re
1111
import runpy
1212
import sys
13+
import tempfile
1314
import zipfile
1415

1516

@@ -97,30 +98,28 @@ def __init__(self):
9798

9899
def find_spec(self, name, path, target=None):
99100
"""Implements abc.MetaPathFinder."""
100-
loader = self.find_module(name, path)
101-
if loader is None:
102-
return None
103-
return spec_from_loader(name, loader)
101+
if name in self.modules:
102+
return spec_from_loader(name, self)
104103

105-
def find_module(self, fullname, path=None):
106-
"""Attempt to locate module. Returns self if found, None if not."""
107-
if fullname in self.modules:
108-
return self
109-
110-
def load_module(self, fullname):
111-
"""Actually load a module that we said we'd handle in find_module."""
112-
import tempfile
113-
114-
filename = self.modules[fullname]
104+
def create_module(self, spec):
105+
"""Create a module object that we're going to load."""
106+
filename = self.modules[spec.name]
115107
prefix, ext = self.splitext(filename)
116108
with tempfile.NamedTemporaryFile(suffix=ext, prefix=os.path.basename(prefix)) as f:
117109
f.write(self.zf.read(filename))
118110
f.flush()
119-
mod = machinery.ExtensionFileLoader(fullname, f.name).load_module()
111+
spec.origin = f.name
112+
loader = machinery.ExtensionFileLoader(spec.name, f.name)
113+
spec.loader = loader
114+
mod = loader.create_module(spec)
120115
# Make it look like module came from the original location for nicer tracebacks.
121116
mod.__file__ = filename
122117
return mod
123118

119+
def exec_module(self, mod):
120+
"""Because we set spec.loader above, the ExtensionFileLoader's exec_module is called."""
121+
raise NotImplementedError("SoImport.exec_module isn't used")
122+
124123
def splitext(self, path):
125124
"""Similar to os.path.splitext, but splits our longest known suffix preferentially."""
126125
for suffix in self.suffixes_by_length:
@@ -187,22 +186,18 @@ def _find_all_distributions(self, module_dir):
187186

188187
def find_spec(self, name, path, target=None):
189188
"""Implements abc.MetaPathFinder."""
190-
loader = self.find_module(name, path)
191-
if loader is None:
192-
return None
193-
return spec_from_loader(name, loader)
194-
195-
def find_module(self, fullname, path=None):
196-
"""Attempt to locate module. Returns self if found, None if not."""
197-
if fullname.startswith(self.prefix):
198-
return self
189+
if name.startswith(self.prefix):
190+
return spec_from_loader(name, self)
199191

200-
def load_module(self, fullname):
192+
def create_module(self, spec):
201193
"""Actually load a module that we said we'd handle in find_module."""
202-
module = import_module(fullname[len(self.prefix):])
203-
sys.modules[fullname] = module
194+
module = import_module(spec.name[len(self.prefix):])
195+
sys.modules[spec.name] = module
204196
return module
205197

198+
def exec_module(self, mod):
199+
"""Nothing to do, create_module already did the work."""
200+
206201
def find_distributions(self, context):
207202
"""Return an iterable of all Distribution instances capable of
208203
loading the metadata for packages for the indicated ``context``.

0 commit comments

Comments
 (0)