[Python-Dev] Warnings behave differently in Python 2 and Python 3?

Victor Stinner victor.stinner at gmail.com
Mon Nov 14 12:16:45 EST 2016


You are looking for the bug fix http://bugs.python.org/issue4180:

"warnings.simplefilter("always") does not make warnings always show up"

Python 2.7 has a bug, Python 3.4.3+ behaves correctly.

It is surprising that Python 3.4.0 and Python 3.4.3 behaves
differently :-) (Ubuntu 14.04 uses Python 3.4.0..

Victor

2016-11-14 14:58 GMT+01:00 Christoph Groth <christoph at grothesque.org>:
> (I assume that this list is appropriate for this topic, but if it isn't, I
> will be grateful for being redirected to the appropriate place.)
>
> It seems that warnings behave differently in Python 2 and Python 3.
> Consider the following script.
>
> ****************
> def func():
>    warnings.warn("Blabla", RuntimeWarning)
>
> warnings.simplefilter("ignore")
> func()
> warnings.simplefilter("always")
> func()
> ****************
>
> When run with CPython 2.7, there is no output, when run with CPython 3.5,
> there is the following output:
>
> ****************
> /tmp/test.py:4: RuntimeWarning: Blabla
>  warnings.warn("Blabla", RuntimeWarning)
> ****************
>
> Was there indeed a change of how warnings behave in Python 3?  I searched,
> but couldn't find any documentation for this.
>
> Understanding how warnings work is especially important when testing for
> them.
> This is how I stumbled across this, in case anyone is interested:
> https://gitlab.kwant-project.org/kwant/kwant/issues/1#note_2608
>
> I also note that the current documentation still uses DeprecationWarning in
> the example of how to suppress a warning:
> https://docs.python.org/dev/library/warnings.html#temporarily-suppressing-warnings.
> Since DeprecationWarning is disabled by default in modern Python, perhaps a
> different warning would provide a better example?
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/victor.stinner%40gmail.com


More information about the Python-Dev mailing list