python / python/cpython

test.support.warnings_helper.check_warnings() raises KeyError: 'warnings' under -X lazy_imports=all

未关闭
#156,570 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

tests type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

test.support.warnings_helper.check_warnings() raises KeyError: 'warnings'
under -X lazy_imports=all, taking down every test that uses it.

Reproducer:

./python -X lazy_imports=all -m test test_sundry test_time

Lib/test/support/warnings_helper.py:176:

# Because test_warnings swap the module, we need to look up in the
# sys.modules dictionary.
wmod = sys.modules['warnings']

warnings is not in sys.modules at interpreter startup, with or without lazy
imports, so that key is populated solely by the module-level import warnings
at the top of this same file. Reading the module out of sys.modules by
subscript means the global name is never touched, so under lazy imports the
import is never resolved and the key is never created.

41 files under Lib/test/ use check_warnings() / check_no_warnings().

One-line fix — the default argument is evaluated on every call, which resolves
the lazy import before the lookup, and the module swap done by test_warnings
still wins:

wmod = sys.modules.get('warnings', warnings)

Verified on main (d9154924138): with that change test_sundry, test_time
and test_warnings pass under -X lazy_imports=all; test___all__,
test_sundry, test_time, test_warnings and test_support still pass
without the flag.

test_sundry and test_time are not in the exclusion list proposed in
GH-151105, so this is a failure mode the planned CI would not have covered.

The same pattern appears in the tests themselves. After the helper is fixed,
test___all__ still fails at Lib/test/test___all__.py:49:

if not hasattr(sys.modules[modname], "__all__"):

with KeyError: '_osx_support' — the module is imported by
exec("import %s" % modname) a few lines above, which under lazy imports
leaves sys.modules untouched. Lines 71 and 76 do the same thing.

grep -rn 'sys\.modules\[' Lib/test/ reports 126 subscript reads. Most are
legitimate identity checks after an eager import; the ones worth auditing are
those where the key is expected to be filled by an import in the same file, or
by an execed import statement.

Related to #149640.

CPython versions tested on:

CPython main branch (d9154924138)

Operating systems tested on:

Linux


🤖 Investigated and drafted with Claude Code,
model Claude Opus 5 (1M context).

Linked PRs
  • gh-156573

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Lib/test/support/warnings_helper.py 的第 176 行开始,然后检查 Lib/test/test___all__.py 中报告的 sys.modules 访问。使用 -X lazy_imports=all 和列出的测试模块运行复现程序;完成的标准是受影响的测试在启用和不启用延迟导入时都通过,包括 test_support 和 test_warnings。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
testing-qa
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。