New submission from Thomas Kluyver <tho...@kluyver.me.uk>:

The warnings module tries to find and show the line of code which is 
responsible for a warning, for the same reasons that tracebacks show a line of 
code from each stack frame. However, they work in quite different ways.

Native tracebacks, the traceback module and pdb all do something like this:

frame.f_code.co_filename

But warnings does something like this (paraphrasing C code in _warnings.c):

frame.f_globals.get('__file__', sys.argv[0])

This causes problems for interactive interpreters like IPython, because there 
are multiple pieces of entered code which have to share the same global 
namespace. E.g. https://github.com/ipython/ipython/issues/11080

This was raised a long time ago in #1692664. Back then, the answer was that 
co_filename could be wrong if the path of a pyc file changed. However, that 
issue was fixed in #1180193. And it seems that the co_filename approach must 
largely work today, because tracebacks and pdb rely on it.

So I'm proposing to make warnings match how those other tools find filenames. I 
think this should also be a minor simplification of the code.

----------
components: Library (Lib)
messages: 315848
nosy: takluyver
priority: normal
severity: normal
status: open
title: warnings: get filename from frame.f_code.co_filename

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33375>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to