Issue 29672: catch_warnings context manager causes all warnings to be printed every time, even after exiting (original) (raw)

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