[New-bugs-announce] [issue33375] warnings: get filename from frame.f_code.co_filename

Thomas Kluyver report at bugs.python.org
Fri Apr 27 12:38:55 EDT 2018


New submission from Thomas Kluyver <thomas at 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 <report at bugs.python.org>
<https://bugs.python.org/issue33375>
_______________________________________


More information about the New-bugs-announce mailing list