This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients msmhrt, ned.deily, vstinner
Date 2014-07-07.21:04:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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").
Date User Action Args
2014-07-07 21:04:44vstinnersetrecipients: + vstinner, ned.deily, msmhrt
2014-07-07 21:04:44vstinnersetmessageid: <>
2014-07-07 21:04:44vstinnerlinkissue21925 messages
2014-07-07 21:04:43vstinnercreate