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

SourceForge.net noreply at sourceforge.net
Fri Apr 29 12:54:46 CEST 2005


Bugs item #1191104, was opened at 2005-04-27 17:55
Message generated for change (Comment added) made by ivilata
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: Ivan Vilata i Balaguer (ivilata)
Date: 2005-04-29 12:54

Message:
Logged In: YES 
user_id=1064183

OK, I see. Then the documentation for ``warnings.warn()``
may be more precise.  Where it says “This function raises an
exception if the particular warning issued is changed into
an error by the warnings filter see above.” it may append
“and that warning has not already been filtered”.

Anyway, I still think that one should expect to *always* get
an exception once the warnings filter has been instructed to
turn a warning into an exception, instead of maybe filtering
it.  The later could lead to some errors to pass
unadvertised and get delayed.  It also disables the
possibility of turning every warning into an error in the
middle of a session (à la gcc's ``-pedantic-errors`` option).

My proposal is not to filter a warning once it has been
instructed to cause an exception.  The existing code should
not need to worry about using ``warn()`` or
``warn_explicit()`` (since in the usual situation, duplicate
warnings --not exceptions-- should still be filtered).

Thanks for your attention,
    Ivan

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

Comment By: Vinay Sajip (vsajip)
Date: 2005-04-29 10: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