STINNER Victor added the comment:

The problem is tricky.

The modules are deleted in a random order at exit. The problem is that showing 
the warning requires to import the linecache module. But during Python 
finalization, we may or may not be able to import modules.

Py_Finalize() calls PyImport_Cleanup() which starts by setting sys.path and 
sys.meta_path to None. The _find_spec() function of importlib._bootstrap fails 
because sys.meta_path is None. In fact, find_spec() starts by emiting a warning 
which calls formatwarning() which tries to import linecache again... and then 
importlib emits again a warning... "import linecache" is tried twice and then 
everything fails.

A workaround would be to unload the __main__ module first, before clearing sys 
attributes.

Another workaround would be to not try to import modules in 
warnings.formatwarning() if Python is exiting.

The tricky exit code was already discussed in the issue #19421 ("FileIO 
destructor imports indirectly the io module at exit"). See also my more general 
issue #21788 ("Rework Python finalization").

----------
nosy: +haypo

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue21925>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to