[Python-Dev] Make the warnings module extensible

Victor Stinner victor.stinner at gmail.com
Wed Mar 16 20:29:33 EDT 2016


I have an API question for you.

I would like to add a new parameter the the showwarning() function of
the warnings module. Problem: it's not possible to do that without
breaking the backward compatibility (when an application replaces
warnings.showwarning(), the warnings allows and promotes that).

I proposed a patch to add a new showmsg() function which takes a
warnings.WarningMessage object:

The design is inspired by the logging module and its logging.LogRecord
class. The warnings.WarningMessage already exists. Since it's class,
it's easy to add new attributes without breaking the API.

- If warnings.showwarning() is replaced by an application, this
function will be called in practice to log the warning.
- If warnings.showmsg() is replaced, again, this function will be
called in practice.
- If both functions are replaced, showmsg() will be called (replaced
showwarning() is ignored)

I'm not sure about function names: showmsg() and formatmsg(). Maybe:
showwarnmsg() and formatwarnmsg()? Bikeshedding.... fight!

The final goal is to log the traceback where the destroyed object was
allocated when a ResourceWarning warning is logged:

Adding a new parameter to warnings make the implementation much more
simple and gives more freedom to the logger to decide how to format
the warning.


More information about the Python-Dev mailing list