Wrap every deferred import exception in a new exception type LazyImportError, chained to the original exception.
Test Plan
When Lazy Imports is not enabled, every error will change nothing.
When Lazy Imports is enabled, every error cause by a (lazy) import action will wrap into LazyImportError. Thus, the error will show LazyImportError and link back to the original exception with tracebacks.
Pre-req
Create foo.py, value_error.py, run_foo.py, and run_value_error.py under the same path of python.exe.
foo.py
import applepie
applepie
value_error.py
raise ValueError("")
run_foo.py
import foo
foo
run_value_error.py
import value_error
value_error
Test without Lazy Imports
Test ModuleNotFoundError
Run
./python.exe run_foo.py
Expected results:
Traceback (most recent call last):
File "/Users/harperlin/Documents/GitHub/crashy_0623/run_foo.py", line 1, in <module>
import foo
^^^^^^^^^^
File "/Users/harperlin/Documents/GitHub/crashy_0623/foo.py", line 1, in <module>
import applepie
^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'applepie'
Test ValueError
Run
./python.exe run_value_error.py
Expected results:
Traceback (most recent call last):
File "/Users/harperlin/Documents/GitHub/crashy_0623/run_value_error.py", line 1, in <module>
import value_error
^^^^^^^^^^^^^^^^^^
File "/Users/harperlin/Documents/GitHub/crashy_0623/value_error.py", line 1, in <module>
raise ValueError("")
^^^^^^^^^^^^^^^^^^^^
ValueError
Test with Lazy Imports
Test ModuleNotFoundError
Run
./python.exe -L run_foo.py
Expected results:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'applepie'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/harperlin/Documents/GitHub/crashy_0623/run_foo.py", line 2, in <module>
foo
^^^
File "/Users/harperlin/Documents/GitHub/crashy_0623/foo.py", line 2, in <module>
applepie
^^^^^^^^
LazyImportError: Error occurred when loading a lazy import. Original import was at file /Users/harperlin/Documents/GitHub/crashy_0623/foo.py, line 1
Test ValueError
Run
./python.exe -L run_value_error.py
Expected results:
Traceback (most recent call last):
File "/Users/harperlin/Documents/GitHub/crashy_0623/value_error.py", line 1, in <module>
raise ValueError("")
^^^^^^^^^^^^^^^^^^^^
ValueError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/harperlin/Documents/GitHub/crashy_0623/run_value_error.py", line 2, in <module>
value_error
^^^^^^^^^^^
LazyImportError: Error occurred when loading a lazy import. Original import was at file /Users/harperlin/Documents/GitHub/crashy_0623/run_value_error.py, line 1
Summary
Wrap every deferred import exception in a new exception type
LazyImportError
, chained to the original exception.Test Plan
LazyImportError
. Thus, the error will showLazyImportError
and link back to the original exception with tracebacks.Pre-req
Create
foo.py
,value_error.py
,run_foo.py
, andrun_value_error.py
under the same path ofpython.exe
.foo.py
value_error.py
run_foo.py
run_value_error.py
Test without Lazy Imports
Test ModuleNotFoundError
Run
Expected results:
Test ValueError
Run
Expected results:
Test with Lazy Imports
Test ModuleNotFoundError
Run
Expected results:
Test ValueError
Run
Expected results: