test_importlib: restore builtins after import#154276
Open
kddnewton wants to merge 1 commit into
Open
Conversation
Our CI (https://github.com/facebookincubator/cinder/actions/runs/29382592631/job/87249270535) was failing on test_pickle and test_pickletool with: ``` PicklingError: Can't pickle <class 'importlib._bootstrap.BuiltinImporter'>: it's not the same object as importlib._bootstrap.BuiltinImporter ``` I believe this is happening because we run multiple test modules in the same interpreter, so the following sequence happens: * `test_importlib.util.import_importlib()` imports a source copy of `importlib` while blocking `_frozen_importlib`. During this import, `importlib._bootstrap._setup()` initializes import metadata on existing built-in modules. If `builtins.__loader__` or `builtins.__spec__` was originally absent, the source copy installs its own `BuiltinImporter`. * Although `import_fresh_module()` restores `sys.modules`, it does not restore attributes mutated on existing module objects. Consequently, `builtins.__loader__` continues to reference the temporary source `BuiltinImporter`, while `importlib._bootstrap.BuiltinImporter` resolves to the restored frozen class. * Pickle serializes classes by module and qualified name and verifies that the resolved global is the same object. The two `BuiltinImporter` class objects therefore cause the identity check to fail. To fix this we instead snapshot `__loader__` and `__spec__` before importing `importlib` and then restore them to their original values after.
kddnewton
requested review from
FFY00,
brettcannon,
ericsnowcurrently,
ncoghlan and
warsaw
as code owners
July 20, 2026 17:14
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Member
|
Can this go under #152659? |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Our CI (https://github.com/facebookincubator/cinder/actions/runs/29382592631/job/87249270535) was failing on test_pickle and test_pickletool with:
I believe this is happening because we run multiple test modules in the same interpreter, so the following sequence happens:
test_importlib.util.import_importlib()imports a source copy ofimportlibwhile blocking_frozen_importlib. During this import,importlib._bootstrap._setup()initializes import metadata on existing built-in modules. Ifbuiltins.__loader__orbuiltins.__spec__was originally absent, the source copy installs its ownBuiltinImporter.import_fresh_module()restoressys.modules, it does not restore attributes mutated on existing module objects. Consequently,builtins.__loader__continues to reference the temporary sourceBuiltinImporter, whileimportlib._bootstrap.BuiltinImporterresolves to the restored frozen class.BuiltinImporterclass objects therefore cause the identity check to fail.To fix this we instead snapshot
__loader__and__spec__before importingimportliband then restore them to their original values after.