-
Notifications
You must be signed in to change notification settings - Fork 17
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
peterebden
merged 3 commits into
please-build:master
from
peterebden:get-rid-of-load-module
Nov 18, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.5.2 | ||
1.5.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import re | ||
import runpy | ||
import sys | ||
import tempfile | ||
import zipfile | ||
|
||
|
||
|
@@ -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: | ||
|
@@ -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):]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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``. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofexec_module
...