This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: warnings: get filename from frame.f_code.co_filename
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, takluyver, vstinner
Priority: normal Keywords: patch

Created on 2018-04-27 16:38 by takluyver, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6622 merged takluyver, 2018-04-28 06:06
PR 7556 merged ZackerySpytz, 2018-06-09 07:06
Messages (9)
msg315848 - (view) Author: Thomas Kluyver (takluyver) * Date: 2018-04-27 16:38
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.
msg316133 - (view) Author: Thomas Kluyver (takluyver) * Date: 2018-05-03 19:52
Hi Brett! If you get a moment, any review of the linked PR would be welcome. :-)

https://github.com/python/cpython/pull/6622
msg316134 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-05-03 19:56
I just added myself to review the PR. At worst I will get to it during the
sprints at PyCon US.

On Thu, 3 May 2018 at 12:52 Thomas Kluyver <report@bugs.python.org> wrote:

>
> Thomas Kluyver <thomas@kluyver.me.uk> added the comment:
>
> Hi Brett! If you get a moment, any review of the linked PR would be
> welcome. :-)
>
> https://github.com/python/cpython/pull/6622
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue33375>
> _______________________________________
>
msg316135 - (view) Author: Thomas Kluyver (takluyver) * Date: 2018-05-03 20:11
Thanks! No rush, I just thought I'd take the opportunity when you added yourself to the nosy list.
msg319099 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-06-08 19:28
New changeset 11a896652ee98aa44e59ed25237f9efb56635dcf by Brett Cannon (Thomas Kluyver) in branch 'master':
bpo-33375: Get filename for warnings from frame.f_code.co_filename (GH-6622)
https://github.com/python/cpython/commit/11a896652ee98aa44e59ed25237f9efb56635dcf
msg319161 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-06-09 17:09
New changeset 3f45f5da8eb052f1b54d37086c67b7094f35b67b by Brett Cannon (Zackery Spytz) in branch 'master':
bpo-33375: Fix GCC warning in Python/_warnings.c (GH-7556)
https://github.com/python/cpython/commit/3f45f5da8eb052f1b54d37086c67b7094f35b67b
msg319162 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-06-09 17:10
Thanks everyone for making this happen!
msg320045 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-06-20 10:00
This change introduced a regression: bpo-33912 "[EASY] test_warnings: test_exec_filename() fails when run with -Werror". I consider that it's an easy issue, please don't fix it, but explain how to fix it and let a newcomer to fix it!
https://mail.python.org/pipermail/python-committers/2017-June/004564.html
msg355172 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-10-22 23:03
I closed bpo-8787 as a duplicate of this issue.
History
Date User Action Args
2022-04-11 14:58:59adminsetgithub: 77556
2019-10-22 23:03:40vstinnersetmessages: + msg355172
versions: + Python 3.8
2019-10-22 23:03:21vstinnerlinkissue8787 superseder
2018-06-20 10:00:32vstinnersetnosy: + vstinner
messages: + msg320045
2018-06-09 17:10:30brett.cannonsetstatus: open -> closed
resolution: fixed
messages: + msg319162

stage: patch review -> resolved
2018-06-09 17:09:48brett.cannonsetmessages: + msg319161
2018-06-09 07:06:33ZackerySpytzsetpull_requests: + pull_request7190
2018-06-08 19:28:39brett.cannonsetmessages: + msg319099
2018-05-03 20:11:21takluyversetmessages: + msg316135
2018-05-03 19:56:42brett.cannonsetmessages: + msg316134
2018-05-03 19:52:11takluyversetmessages: + msg316133
2018-04-30 18:34:56brett.cannonsetnosy: + brett.cannon
2018-04-28 06:06:28takluyversetkeywords: + patch
stage: patch review
pull_requests: + pull_request6318
2018-04-27 16:38:55takluyvercreate