Things to check first
AnyIO version
4.15.1
Python version
3.10.9 (because PyOxidizer uses this particular version of Python)
What happened?
When an app is packaged with PyOxidizer and it tries to import anyio, the app crashes with the following exception:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "anyio", line 11, in <module>
File "anyio._lazyimport", line 23, in install_lazy_importer
File "anyio._lazyimport", line 116, in _build_lazy_map
File "inspect", line 1285, in getsource
File "inspect", line 1267, in getsourcelines
File "inspect", line 1078, in findsource
File "inspect", line 955, in getsourcefile
File "inspect", line 916, in getfile
TypeError: <module 'anyio' (<oxidized_importer.OxidizedFinder object at 0x1054d0ac0>)> is a built-in module
This is because PyOxidizer uses an import mechanism that loads the modules directly from the binary of the executable and it does not make the source code of the module available. Contrary to anyio's expectations, it does not throw an OSError when inspect.getsource() fails but a TypeError is thrown instead. This is then ignored by _build_lazy_map() and the exception propagates to the top level. Extending _build_lazy_map() to catch TypeError (or maybe a blanket Exception) around inspect.getsource() would probably solve the problem. Checking getattr(sys, "frozen", False) would also be an alternative to detect PyOxidizer, PyInstaller and many other tools that freeze a Python app into an executable.
How can we reproduce the bug?
Build a simple application with PyOxidizer that attempts to import anyio. Attached is a self-contained ZIP file with the required files; running ./run.sh will create a virtualenv, install PyOxidizer in it from PyPI, then build the executable with PyOxidizer. Besides Python, a Rust compiler is also needed in the environment.
anyio-pyoxidizer-crash-repro.zip
Things to check first
I have searched the existing issues and didn't find my bug already reported there
I have checked that my bug is still present in the latest release
AnyIO version
4.15.1
Python version
3.10.9 (because PyOxidizer uses this particular version of Python)
What happened?
When an app is packaged with PyOxidizer and it tries to import
anyio, the app crashes with the following exception:This is because PyOxidizer uses an import mechanism that loads the modules directly from the binary of the executable and it does not make the source code of the module available. Contrary to
anyio's expectations, it does not throw anOSErrorwheninspect.getsource()fails but aTypeErroris thrown instead. This is then ignored by_build_lazy_map()and the exception propagates to the top level. Extending_build_lazy_map()to catchTypeError(or maybe a blanketException) aroundinspect.getsource()would probably solve the problem. Checkinggetattr(sys, "frozen", False)would also be an alternative to detect PyOxidizer, PyInstaller and many other tools that freeze a Python app into an executable.How can we reproduce the bug?
Build a simple application with PyOxidizer that attempts to import
anyio. Attached is a self-contained ZIP file with the required files; running./run.shwill create a virtualenv, install PyOxidizer in it from PyPI, then build the executable with PyOxidizer. Besides Python, a Rust compiler is also needed in the environment.anyio-pyoxidizer-crash-repro.zip