[Python-Dev] Make stacklevel=2 by default in warnings.warn()
Serhiy Storchaka
storchaka at gmail.com
Mon Sep 21 19:00:13 CEST 2015
On 21.09.15 10:18, Victor Stinner wrote:
> 2015-09-20 8:44 GMT+02:00 Serhiy Storchaka <storchaka at gmail.com>:
>> I propose to make the default value of stacklevel to be 2.
>> I think that unlikely this will break existing code.
>
> Consider this simple script:
> ---
> import warnings
> warnings.warn("here")
> ---
>
> Currrent output:
> ---
> x.py:3: UserWarning: here
> warnings.warn("here")
> ---
>
> => it shows the script name (x.py), the line number and the line, as expected.
>
> Now try stacklevel=2:
> ---
> import warnings
> warnings.warn("here", stacklevel=2)
> ---
>
> New output:
> ---
> sys:1: UserWarning: here
> ---
>
> "sys:1" is not really useful :-/
This is not new. The same output we get when run a module that correctly
emits a warning at module level (with explicit stacklevel=2).
$ ./python -Wa Lib/imp.py
sys:1: PendingDeprecationWarning: the imp module is deprecated in favour
of importlib; see the module's documentation for alternative uses
If this message looks confusing for you, we can avoid it if stop
skipping frames further if frame.f_back is None. This will got rid of
"sys:1:" in both cases, explicit and implicit stacklevel=2.
More information about the Python-Dev
mailing list