[ python-Bugs-1191104 ] Warning ``error`` filter action is ignored.

SourceForge.net noreply at sourceforge.net
Fri Apr 29 10:17:31 CEST 2005


Bugs item #1191104, was opened at 2005-04-27 15:55
Message generated for change (Comment added) made by vsajip
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1191104&group_id=5470

Category: Python Library
Group: None
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Ivan Vilata i Balaguer (ivilata)
Assigned to: Vinay Sajip (vsajip)
Summary: Warning ``error`` filter action is ignored.

Initial Comment:
Hi all.  It seems that setting the ``error`` action on
a warning message once it has already been triggered
does not allow it to be triggered again.  As usual, the
code is clearer than the explanation::

    import warnings

    def do_warn():
        warnings.warn("A warning.", DeprecationWarning)

    do_warn()

    warnings.filterwarnings('error',
category=DeprecationWarning)
    do_warn()

    warnings.filterwarnings('always',
category=DeprecationWarning)
   do_warn()

The output of this program is::

    nowarn.py:4: DeprecationWarning: A warning.
      warnings.warn("A warning.", DeprecationWarning)

There is no exception raised, though the filter was
instructed to turn the warning into an error.  Take
note that using ``always`` has similar results.

Does it work in that way on purpose?  I find it quite
counterintuitive, IMHO.  If this behaviour is intended,
what would be the way to turn the second warning into
an exception?

Thanks!

----------------------------------------------------------------------

>Comment By: Vinay Sajip (vsajip)
Date: 2005-04-29 08:17

Message:
Logged In: YES 
user_id=308438

This does not appear to be a bug. For example, the following
script (indentation may get messed up):
#--------------------------------------
import sys, warnings

def do_warn():
    fn = sys.modules["__main__"].__file__
    warnings.warn_explicit("A warning.", DeprecationWarning,
fn, 0)

def main():
    do_warn()
    warnings.filterwarnings('error',
category=DeprecationWarning)
    try:
        do_warn()
    except Exception, e:
        print "exception handled: %s" % e
    warnings.filterwarnings('always',
category=DeprecationWarning)
    do_warn()

if __name__ == "__main__":
    main()
#--------------------------------------

prints

C:\temp\nowarn.py:0: DeprecationWarning: A warning.
exception handled: A warning.
C:\temp\nowarn.py:0: DeprecationWarning: A warning.

So the problem is that if you want explicit warnings, you
need to use warn_explicit() rather than warn().




----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1191104&group_id=5470


More information about the Python-bugs-list mailing list