-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-132775: Add _PyPickle_GetXIData() #133107
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
Merged
ericsnowcurrently
merged 6 commits into
python:main
from
ericsnowcurrently:add-pypickle-getxidata
Apr 30, 2025
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dc066d2
Add _PyPickle_GetXIData().
ericsnowcurrently 647c538
Drop a TODO comment.
ericsnowcurrently 15ecef1
Merge branch 'main' into add-pypickle-getxidata
ericsnowcurrently 7f7b1d7
Merge branch 'main' into add-pypickle-getxidata
ericsnowcurrently bf0013c
Fix the tests.
ericsnowcurrently a135db8
Fix import_helper.module_restored().
ericsnowcurrently 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 hidden or 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 hidden or 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,6 +1,7 @@ | ||
import contextlib | ||
import _imp | ||
import importlib | ||
import importlib.machinery | ||
import importlib.util | ||
import os | ||
import shutil | ||
|
@@ -332,3 +333,110 @@ def ensure_lazy_imports(imported_module, modules_to_block): | |
) | ||
from .script_helper import assert_python_ok | ||
assert_python_ok("-S", "-c", script) | ||
|
||
|
||
@contextlib.contextmanager | ||
def module_restored(name): | ||
"""A context manager that restores a module to the original state.""" | ||
missing = name in sys.modules | ||
orig = sys.modules.get(name) | ||
if orig is None: | ||
mod = importlib.import_module(name) | ||
else: | ||
mod = type(sys)(name) | ||
mod.__dict__.update(orig.__dict__) | ||
sys.modules[name] = mod | ||
try: | ||
yield mod | ||
finally: | ||
if missing: | ||
sys.modules.pop(name, None) | ||
else: | ||
sys.modules[name] = orig | ||
|
||
|
||
def create_module(name, loader=None, *, ispkg=False): | ||
"""Return a new, empty module.""" | ||
spec = importlib.machinery.ModuleSpec( | ||
name, | ||
loader, | ||
origin='<import_helper>', | ||
is_package=ispkg, | ||
) | ||
return importlib.util.module_from_spec(spec) | ||
|
||
|
||
def _ensure_module(name, ispkg, addparent, clearnone): | ||
try: | ||
mod = orig = sys.modules[name] | ||
except KeyError: | ||
mod = orig = None | ||
missing = True | ||
else: | ||
missing = False | ||
if mod is not None: | ||
# It was already imported. | ||
return mod, orig, missing | ||
# Otherwise, None means it was explicitly disabled. | ||
|
||
assert name != '__main__' | ||
if not missing: | ||
assert orig is None, (name, sys.modules[name]) | ||
if not clearnone: | ||
raise ModuleNotFoundError(name) | ||
del sys.modules[name] | ||
# Try normal import, then fall back to adding the module. | ||
try: | ||
mod = importlib.import_module(name) | ||
except ModuleNotFoundError: | ||
if addparent and not clearnone: | ||
addparent = None | ||
mod = _add_module(name, ispkg, addparent) | ||
return mod, orig, missing | ||
|
||
|
||
def _add_module(spec, ispkg, addparent): | ||
if isinstance(spec, str): | ||
name = spec | ||
mod = create_module(name, ispkg=ispkg) | ||
spec = mod.__spec__ | ||
else: | ||
name = spec.name | ||
mod = importlib.util.module_from_spec(spec) | ||
sys.modules[name] = mod | ||
if addparent is not False and spec.parent: | ||
_ensure_module(spec.parent, True, addparent, bool(addparent)) | ||
return mod | ||
|
||
|
||
def add_module(spec, *, parents=True): | ||
"""Return the module after creating it and adding it to sys.modules. | ||
|
||
If parents is True then also create any missing parents. | ||
""" | ||
return _add_module(spec, False, parents) | ||
|
||
|
||
def add_package(spec, *, parents=True): | ||
"""Return the module after creating it and adding it to sys.modules. | ||
|
||
If parents is True then also create any missing parents. | ||
""" | ||
return _add_module(spec, True, parents) | ||
|
||
|
||
def ensure_module_imported(name, *, clearnone=True): | ||
"""Return the corresponding module. | ||
|
||
If it was already imported then return that. Otherwise, try | ||
importing it (optionally clear it first if None). If that fails | ||
then create a new empty module. | ||
|
||
It can be helpful to combine this with ready_to_import() and/or | ||
isolated_modules(). | ||
""" | ||
if sys.modules.get(name) is not None: | ||
mod = sys.modules[name] | ||
else: | ||
mod, _, _ = _force_import(name, False, True, clearnone) | ||
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.
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'm fixing this in gh-133482. |
||
return mod |
Oops, something went wrong.
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.
shouldn't
missing
bename not in sys.modules
? (or renamed topresent
)?also, is it different than
orig is not None
?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.
Yeah, it should be "not" in sys.modules. Good catch.
It is different from
orig is not None
sinceNone
could be set in sys.modules. I could use a temporary sentinel for that, I suppose.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.
fixed