[New-bugs-announce] [issue4182] warnings.warn shows the wrong filename and line number for stacklevel of 0

Jean-Paul Calderone report at bugs.python.org
Wed Oct 22 23:55:26 CEST 2008


New submission from Jean-Paul Calderone <exarkun at divmod.com>:

In Python 2.5 and earlier, passing stacklevel=0 to warnings.warn
resulted in a warning shown which pointed to warnings.py.  In Python
2.6, passing a stacklevel of 0 results in the warning shown pointing to
the file containing the call to warnings.warn.  To illustrate:

  exarkun at charm:~$ python
  Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 
  [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import warnings
  >>> def f():
  ...     warnings.warn("foo", stacklevel=0)
  ... 
  >>> f()
  /usr/lib/python2.5/warnings.py:41: UserWarning: foo
    lineno = caller.f_lineno
  >>> 

as compared to

  exarkun at charm:~$ ~/Projects/python/branches/release26-maint/python 
  Python 2.6+ (trunk:66997, Oct 22 2008, 14:43:32) 
  [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import warnings
  >>> def f():
  ...     warnings.warn("foo", stacklevel=0)
  ... 
  >>> f()
  __main__:2: UserWarning: foo
  >>> 

This breaks code which assumes that stacklevel=0 will result in a
different file than stacklevel=1.

----------
messages: 75116
nosy: exarkun
severity: normal
status: open
title: warnings.warn shows the wrong filename and line number for stacklevel of 0
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4182>
_______________________________________


More information about the New-bugs-announce mailing list