[New-bugs-announce] [issue26568] Add a new warnings.showmsg() function taking a warnings.WarningMessage object

STINNER Victor report at bugs.python.org
Tue Mar 15 10:32:00 EDT 2016

New submission from STINNER Victor:

Currently, the warnings.showformat() function take between 4 and 6 parameters and it's not possible to add new parameters.

warnings.showformat() calls warnings.formatwarnings() with 5 parameters. Again, it's not easy to pass new parameters.

I would like to add a new "source" parameter for ResourceWarning: see issue #26567 "ResourceWarning: Use tracemalloc to display the traceback where an object was allocated when a ResourceWarning is emitted".

To make warnings extensible, I propose to:

* Add new showmsg() and formatmsg() functions to the warnings module, these functions take a warnings.WarningMessage instance
* The _warnings module calls warnings.showmsg() rather than warnings.showwarning()
* For backward compatibility, warnings.showmsg() calls warnings.showwarning() if warnings.showwarning() was replaced. Same for warnings.formatmsg(): call warnings.formatwarning() if replaced.

Attached patch implements these changes.

With these changes, the warnings looks a little bit more like the logging module and its logging.LogRecord class.

The patch is incomplete, it doesn't add much tests yet and it doesn't touch the documentation. I would prefer to get the new API (and the whole idea) approved before going too far.

Note: If warnings.showwarning is deleted ("del warnings.showwarning"), showmsg() uses its own implementation. Same for formatmsg().

files: warnings_showmsg.patch
keywords: patch
messages: 261814
nosy: haypo
priority: normal
severity: normal
status: open
title: Add a new warnings.showmsg() function taking a warnings.WarningMessage object
versions: Python 3.6
Added file: http://bugs.python.org/file42172/warnings_showmsg.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list