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 Gerrit.Holl
Recipients Gerrit.Holl
Date 2017-02-27.22:29:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1488234596.55.0.435492915134.issue29672@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2017-02-27 22:29:56Gerrit.Hollsetrecipients: + Gerrit.Holl
2017-02-27 22:29:56Gerrit.Hollsetmessageid: <1488234596.55.0.435492915134.issue29672@psf.upfronthosting.co.za>
2017-02-27 22:29:56Gerrit.Holllinkissue29672 messages
2017-02-27 22:29:56Gerrit.Hollcreate