Commit f790e0f
committed
gh-157230: test_importlib restore builtins
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.1 parent 228b1bf commit f790e0f
2 files changed
Lines changed: 54 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
36 | 52 | | |
37 | 53 | | |
38 | 54 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
67 | 99 | | |
68 | 100 | | |
69 | 101 | | |
70 | 102 | | |
71 | | - | |
72 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
73 | 109 | | |
74 | 110 | | |
75 | 111 | | |
| |||
0 commit comments