Bug report
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.
|
try: |
|
import bz2 |
|
del bz2 |
|
_BZ2_SUPPORTED = True |
|
except ImportError: |
|
_BZ2_SUPPORTED = False |
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: #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
Bug report
Bug description:
Under
lazy_imports=all, modules imported in atry/exceptblock 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:
The
try/exceptcheck is onbz2, which succeeds because the import to_bz2which would fail is converted to a lazy import and is not used at top level so the import which would fail isn't triggered.cpython/Lib/shutil.py
Lines 21 to 26 in 87a879f
shutilcould directly check_bz2in this particular case, but I expect this pattern isn't uncommon. I looked into this after I ran into a similar issue withpipandrichwith an OS-specific import that should have failed in a similar manner.Related: #149640 as this also causes
test_shutilto fail underlazy_imports=allif_bz2isn't present.CPython versions tested on:
CPython main branch
Operating systems tested on:
No response