Martin Panter added the comment:
My original demonstration was distilled from a real world case. I get this
issue with other real world cases too, so I am expanding the title to reflect
what I think is going on. This problem seems to be caused by a ResourceWarning
triggered from a garbage cycle being freed just before Python exits. The flood
of ImportWarning lines makes it annoying to scroll up to see what other
ResourceWarning messages, exceptions, etc you are missing out on.
Much simpler one-liner demonstration adapted from Message 222403 (because class
definitions create garbage cycles in my experience):
python3 -Wall -c 'class C: a = open("/dev/null")'
The best workaround is probably to use “python -Wdefault” rather than “python
-Wall”. Then you only get one ImportWarning line.
I suspect the problem might be caused by a recursive loop between emitting a
warning and importing something, as Antoine hinted. Calling
sys.setrecursionlimit(30) reduces the flood to 5 lines. Unfortunately my
modifications to “importlib/_bootstrap.py” and “warnings.py” on my OS are not
having any effect, otherwise I might try to make a patch.
Perhaps if it is not appropriate for the ImportWarning to be removed, could the
code printing out the warning be changed to avoid triggering it in the first
place?
----------
title: Flood of “ImportWarning: sys.meta_path is empty” after exception with
socket subclass -> Warning at interpreter exit triggers flood of
“ImportWarning: sys.meta_path is empty”
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue21049>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com