import_importlib leaks module attr sets
未关闭
还没有人认领这个 Issue。
tests
topic-importlib
type-bug
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
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 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.- Although
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. - Pickle serializes classes by module and qualified name and verifies that the resolved global is the same object. The two
BuiltinImporterclass objects therefore cause the identity check to fail.
To fix this we need to snapshot __loader__ and __spec__ before importing importlib and then restore them to their original values after.
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
- gh-154276
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先从 test_importlib.util.import_importlib() 开始,然后在 test_pickle 和 test_pickletool 中重现失败。跟踪临时导入 importlib 如何改变 builtins.loader 和 builtins.spec;当原始值得到恢复且两个测试都在没有 PicklingError 的情况下通过时,即表示完成。一个相关的 PR gh-154276 表明工作已经在进行中。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend, testing-qa
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 35/100