Importing `bz2` will succeed under `lazy_imports=all` even if bz2 is not supported
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug report
Edit: Originally this bug was expressed through shutil's support detection. shutil now directly imports from _bz2 which works around this issue.
Bug description:
Under lazy_imports=all, modules imported in a try/except block are still imported eagerly. However, imports that occur within that module are still imported lazily.
This may be intentional, but this bug is the result of that behaviour.
On CPython built without bz2 support:
$ PYTHON_LAZY_IMPORTS=normal ./python -c "import shutil; print(shutil._BZ2_SUPPORTED)"
False
$ PYTHON_LAZY_IMPORTS=all ./python -c "import shutil; print(shutil._BZ2_SUPPORTED)"
True
The try/except check is on bz2, which succeeds because the import to _bz2 which would fail is converted to a lazy import and is not used at top level so the import which would fail isn't triggered.
shutil could directly check _bz2 in this particular case, but I expect this pattern isn't uncommon. I looked into this after I ran into a similar issue with pip and rich with an OS-specific import that should have failed in a similar manner.
Related: https://github.com/python/cpython/issues/149640 as this also causes test_shutil to fail under lazy_imports=all if _bz2 isn't present.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-150279
- gh-154771
- gh-157321
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.
Direzione di ricerca
Inizia con le righe 21-26 di Lib/shutil.py e riproduci le due modalità di PYTHON_LAZY_IMPORTS in una build di CPython senza supporto per bz2. Leggi la copertura correlata di test_shutil e le PR collegate per comprendere la direzione attuale. Il lavoro è completato quando il controllo di bz2 non supportato si comporta correttamente con lazy_imports=all senza introdurre regressioni nei test shutil esistenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100