Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warnings: get filename from frame.f_code.co_filename #77556

Closed
takluyver mannequin opened this issue Apr 27, 2018 · 9 comments
Closed

warnings: get filename from frame.f_code.co_filename #77556

takluyver mannequin opened this issue Apr 27, 2018 · 9 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir

Comments

@takluyver
Copy link
Mannequin

takluyver mannequin commented Apr 27, 2018

BPO 33375
Nosy @brettcannon, @vstinner, @takluyver
PRs
  • bpo-33375: Get filename for warnings from frame.f_code.co_filename #6622
  • bpo-33375: Fix GCC warning in Python/_warnings.c #7556
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-06-09.17:10:30.972>
    created_at = <Date 2018-04-27.16:38:55.779>
    labels = ['3.8', 'library']
    title = 'warnings: get filename from frame.f_code.co_filename'
    updated_at = <Date 2019-10-22.23:03:40.665>
    user = 'https://github.com/takluyver'

    bugs.python.org fields:

    activity = <Date 2019-10-22.23:03:40.665>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-06-09.17:10:30.972>
    closer = 'brett.cannon'
    components = ['Library (Lib)']
    creation = <Date 2018-04-27.16:38:55.779>
    creator = 'takluyver'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33375
    keywords = ['patch']
    message_count = 9.0
    messages = ['315848', '316133', '316134', '316135', '319099', '319161', '319162', '320045', '355172']
    nosy_count = 3.0
    nosy_names = ['brett.cannon', 'vstinner', 'takluyver']
    pr_nums = ['6622', '7556']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue33375'
    versions = ['Python 3.8']

    @takluyver
    Copy link
    Mannequin Author

    takluyver mannequin commented Apr 27, 2018

    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. ipython/ipython#11080

    This was raised a long time ago in bpo-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 bpo-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.

    @takluyver takluyver mannequin added the stdlib Python modules in the Lib dir label Apr 27, 2018
    @takluyver
    Copy link
    Mannequin Author

    takluyver mannequin commented May 3, 2018

    Hi Brett! If you get a moment, any review of the linked PR would be welcome. :-)

    #6622

    @brettcannon
    Copy link
    Member

    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. :-)

    #6622

    ----------


    Python tracker <report@bugs.python.org>
    <https://bugs.python.org/issue33375\>


    @takluyver
    Copy link
    Mannequin Author

    takluyver mannequin commented May 3, 2018

    Thanks! No rush, I just thought I'd take the opportunity when you added yourself to the nosy list.

    @brettcannon
    Copy link
    Member

    New changeset 11a8966 by Brett Cannon (Thomas Kluyver) in branch 'master':
    bpo-33375: Get filename for warnings from frame.f_code.co_filename (GH-6622)
    11a8966

    @brettcannon
    Copy link
    Member

    New changeset 3f45f5d by Brett Cannon (Zackery Spytz) in branch 'master':
    bpo-33375: Fix GCC warning in Python/_warnings.c (GH-7556)
    3f45f5d

    @brettcannon
    Copy link
    Member

    Thanks everyone for making this happen!

    @vstinner
    Copy link
    Member

    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

    @vstinner
    Copy link
    Member

    I closed bpo-8787 as a duplicate of this issue.

    @vstinner vstinner added the 3.8 only security fixes label Oct 22, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants