[Patches] [ python-Patches-850482 ] Enhance frame handing in warnings.warn()

SourceForge.net noreply at sourceforge.net
Sun Apr 15 21:46:23 CEST 2007


Patches item #850482, was opened at 2003-11-28 00:12
Message generated for change (Comment added) made by zseil
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=850482&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Library (Lib)
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Walter Dörwald (doerwalter)
Assigned to: Nobody/Anonymous (nobody)
Summary: Enhance frame handing in warnings.warn()

Initial Comment:
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.


----------------------------------------------------------------------

>Comment By: Ziga Seilnacht (zseil)
Date: 2007-04-15 21:46

Message:
Logged In: YES 
user_id=1326842
Originator: NO

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


----------------------------------------------------------------------

Comment By: Walter Dörwald (doerwalter)
Date: 2007-04-08 13:44

Message:
Logged In: YES 
user_id=89016
Originator: YES

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

----------------------------------------------------------------------

Comment By: Ziga Seilnacht (zseil)
Date: 2007-04-02 10:41

Message:
Logged In: YES 
user_id=1326842
Originator: NO

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.
 

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=850482&group_id=5470


More information about the Patches mailing list