test.support.warnings_helper.check_warnings() raises KeyError: 'warnings' under -X lazy_imports=all
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 25/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Ferma
- Stack tecnologico
- python
- Ambito
- testing-qa
Direzione di ricerca
Inizia da Lib/test/support/warnings_helper.py alla riga 176, quindi esamina gli accessi a sys.modules segnalati in Lib/test/test___all__.py. Esegui il riproduttore con -X lazy_imports=all e i moduli di test elencati; il lavoro è completato quando i test interessati passano sia con che senza import lazy, inclusi test_support e test_warnings.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 36k
- Merge medio
- 1g 9h
- PR unite (30g)
- 558
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/cpython
-
docs pending
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
stdlib type-feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
stdlib type-feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
build type-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
stdlib topic-email type-feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
Tutte le issue di python/cpython
Issue simili
-
link-check link-check:sphinx-theme
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
OpenHands/extensions#626 · 1 commento ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
CSCfi/sd-search-api#39 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100