Issue1191104
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.
Created on 2005-04-27 15:55 by ivilata, last changed 2022-04-11 14:56 by admin. This issue is now closed.
Messages (3) | |||
---|---|---|---|
msg25155 - (view) | Author: Ivan Vilata i Balaguer (ivilata) | Date: 2005-04-27 15:55 | |
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! |
|||
msg25156 - (view) | Author: Vinay Sajip (vinay.sajip) * | Date: 2005-04-29 08:17 | |
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(). |
|||
msg25157 - (view) | Author: Ivan Vilata i Balaguer (ivilata) | Date: 2005-04-29 10:54 | |
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 |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:56:11 | admin | set | github: 41914 |
2005-04-27 15:55:55 | ivilata | create |