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.

classification
Title: Enhance frame handing in warnings.warn()
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.2
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, doerwalter, jayvdb, zseil
Priority: normal Keywords: patch

Created on 2003-11-27 23:12 by doerwalter, last changed 2022-04-11 14:56 by admin.

Files
File name Uploaded Description Edit
diff.txt doerwalter, 2003-11-27 23:12
diff2.txt doerwalter, 2007-04-08 11:44
Messages (6)
msg44953 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2003-11-27 23:12
This patch enhances warnings.warn() in the following
way: The stacklevel passed in may be negative. In that
case the call stack is searched for the innermost frame
whose module name differs in the first -stacklevel
components. This frame will be used in the report.

So when you have the following call stack:
m1.f()
m1.m11.f()
m1.m12.f()
m2.m21.f()
m2.m22.f()

and the innermost function() m2.m22.f() call
warnings.warn() with a stacklevel of -2 the frame
reported will be from m1.m12.f(), because it is the
first one from outside the m2 package.
msg44954 - (view) Author: Ziga Seilnacht (zseil) * (Python committer) Date: 2007-04-02 08:41
I don't know how desireable this feature is, but
I think that a better solution would be to add
a new parameter to warnings.warn(), instead of
reusing the old one.

Also, your description is wrong; when warn() in
your example is called with stacklevel -2, the
reported frame is m2.m21.f().  To get your result,
you have to call warn() with stacklevel -1.

I think that a better solution would be to add
a skipmodules='moduleprefix' parameter to the
warn() function.  warn() would then simply look
for the first frame whose module name doesn't
start with this prefix.
 
msg44955 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2007-04-08 11:44
Here's an updated patch that implements your suggestion: skipmodules is a regular expression. Any module whose name matches the RE will be skipped when determining the stacklevel.
File Added: diff2.txt
msg44956 - (view) Author: Ziga Seilnacht (zseil) * (Python committer) Date: 2007-04-15 19:46
Looks good to me.  I like that skipmodules is a
regular expression, but I don't know how will it
affect the intended rewrite in C:

http://www.python.org/sf/1631171

It would be nice if the documentation mentioned
what is the interaction between stacklevel and
skipmodules parameters, at least something like:

"If you want to issue a warning outside your module,
and you don't know how many frames will have to be
skipped for that, pass some big integer for stacklevel"

If this patch is accepted, it should be put to use in
the _struct module:

>>> struct.Struct("<l").pack(4023029188) # note: module is sys
sys:1: DeprecationWarning: struct integer overflow masking is deprecated
msg82016 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-02-14 11:36
Patch has tests, needs updating.
msg256170 - (view) Author: John Mark Vandenberg (jayvdb) * Date: 2015-12-10 08:16
See similar http://bugs.python.org/issue25216
History
Date User Action Args
2022-04-11 14:56:01adminsetgithub: 39615
2015-12-10 08:16:31jayvdbsetnosy: + jayvdb
messages: + msg256170
2012-11-17 17:09:42brett.cannonsetnosy: - brett.cannon
2010-08-18 23:43:31BreamoreBoysetstage: needs patch -> patch review
versions: + Python 3.2, - Python 2.7
2009-02-14 11:36:01ajaksu2setnosy: + ajaksu2, brett.cannon
stage: needs patch
type: enhancement
messages: + msg82016
versions: + Python 2.7, - Python 2.4
2003-11-27 23:12:08doerwaltercreate