[New-bugs-announce] [issue29672] `catch_warnings` context manager causes warnings to be reprinted
Gerrit Holl
report at bugs.python.org
Mon Feb 27 17:29:56 EST 2017
New submission from Gerrit Holl:
Entering the `catch_warnings` context manager is causing warnings to be printed over and over again, rather than just once as it should. Without such a context manager, the behaviour is as expected:
$ cat ./mwe.py
#!/usr/bin/env python3.5
import warnings
for i in range(3):
try:
print(i, __warningregistry__)
except NameError:
print(i, "first warning")
warnings.warn("I don't like this.", UserWarning)
print(i, __warningregistry__)
# with warnings.catch_warnings():
# pass
print(i, __warningregistry__)
warnings.warn("I don't like this.", UserWarning)
$ ./mwe.py
0 first warning
./mwe.py:10: UserWarning: I don't like this.
warnings.warn("I don't like this.", UserWarning)
0 {'version': 0, ("I don't like this.", <class 'UserWarning'>, 10): True}
0 {'version': 0, ("I don't like this.", <class 'UserWarning'>, 10): True}
./mwe.py:15: UserWarning: I don't like this.
warnings.warn("I don't like this.", UserWarning)
1 {'version': 0, ("I don't like this.", <class 'UserWarning'>, 15): True, ("I don't like this.", <class 'UserWarning'>, 10): True}
1 {'version': 0, ("I don't like this.", <class 'UserWarning'>, 15): True, ("I don't like this.", <class 'UserWarning'>, 10): True}
1 {'version': 0, ("I don't like this.", <class 'UserWarning'>, 15): True, ("I don't like this.", <class 'UserWarning'>, 10): True}
2 {'version': 0, ("I don't like this.", <class 'UserWarning'>, 15): True, ("I don't like this.", <class 'UserWarning'>, 10): True}
2 {'version': 0, ("I don't like this.", <class 'UserWarning'>, 15): True, ("I don't like this.", <class 'UserWarning'>, 10): True}
2 {'version': 0, ("I don't like this.", <class 'UserWarning'>, 15): True, ("I don't like this.", <class 'UserWarning'>, 10): True}
Uncommenting the context manager causes the warning to be printed every time:
$ ./mwe.py
0 first warning
./mwe.py:10: UserWarning: I don't like this.
warnings.warn("I don't like this.", UserWarning)
0 {'version': 0, ("I don't like this.", <class 'UserWarning'>, 10): True}
0 {'version': 0, ("I don't like this.", <class 'UserWarning'>, 10): True}
./mwe.py:15: UserWarning: I don't like this.
warnings.warn("I don't like this.", UserWarning)
1 {'version': 2, ("I don't like this.", <class 'UserWarning'>, 15): True}
./mwe.py:10: UserWarning: I don't like this.
warnings.warn("I don't like this.", UserWarning)
1 {'version': 2, ("I don't like this.", <class 'UserWarning'>, 15): True, ("I don't like this.", <class 'UserWarning'>, 10): True}
1 {'version': 2, ("I don't like this.", <class 'UserWarning'>, 15): True, ("I don't like this.", <class 'UserWarning'>, 10): True}
./mwe.py:15: UserWarning: I don't like this.
warnings.warn("I don't like this.", UserWarning)
2 {'version': 4, ("I don't like this.", <class 'UserWarning'>, 15): True}
./mwe.py:10: UserWarning: I don't like this.
warnings.warn("I don't like this.", UserWarning)
2 {'version': 4, ("I don't like this.", <class 'UserWarning'>, 15): True, ("I don't like this.", <class 'UserWarning'>, 10): True}
2 {'version': 4, ("I don't like this.", <class 'UserWarning'>, 15): True, ("I don't like this.", <class 'UserWarning'>, 10): True}
./mwe.py:15: UserWarning: I don't like this.
warnings.warn("I don't like this.", UserWarning)
I think this is undesirable. There is code deep inside a module that I'm using that is using the context manager, and as a consequence, I get warnings printed every time that should be printed only once.
See also on Stack Overflow: http://stackoverflow.com/q/42496579/974555
----------
messages: 288677
nosy: Gerrit.Holl
priority: normal
severity: normal
status: open
title: `catch_warnings` context manager causes warnings to be reprinted
versions: Python 3.5
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29672>
_______________________________________
More information about the New-bugs-announce
mailing list