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

inspect.getframeinfo() cannot show first line #60016

Closed
sbt mannequin opened this issue Aug 29, 2012 · 11 comments
Closed

inspect.getframeinfo() cannot show first line #60016

sbt mannequin opened this issue Aug 29, 2012 · 11 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sbt
Copy link
Mannequin

sbt mannequin commented Aug 29, 2012

BPO 15812
Nosy @pwaller, @berkerpeksag, @serhiy-storchaka
PRs
  • [Do Not Merge] Convert Misc/NEWS so that it is managed by towncrier #552
  • Files
  • inspect_getframeinfo_line1.patch
  • 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 2017-01-02.03:59:36.628>
    created_at = <Date 2012-08-29.14:54:39.228>
    labels = ['3.7', 'type-bug', 'library']
    title = 'inspect.getframeinfo() cannot show first line'
    updated_at = <Date 2017-03-31.16:36:20.449>
    user = 'https://bugs.python.org/sbt'

    bugs.python.org fields:

    activity = <Date 2017-03-31.16:36:20.449>
    actor = 'dstufft'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-01-02.03:59:36.628>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2012-08-29.14:54:39.228>
    creator = 'sbt'
    dependencies = []
    files = ['27357']
    hgrepos = []
    issue_num = 15812
    keywords = ['patch']
    message_count = 11.0
    messages = ['169387', '171638', '171640', '171641', '171643', '282427', '284452', '284453', '284471', '284516', '284517']
    nosy_count = 6.0
    nosy_names = ['Peter.Waller', 'python-dev', 'sbt', 'berker.peksag', 'serhiy.storchaka', 'Sam.Breese']
    pr_nums = ['552']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue15812'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7']

    @sbt
    Copy link
    Mannequin Author

    sbt mannequin commented Aug 29, 2012

    When inspect.getframeinfo() tries to collect lines of context it never shows the first line (unless context is as big as the number of lines in the file).

    The relevant code is

            start = lineno - 1 - context//2
            try:
                lines, lnum = findsource(frame)
            except IOError:
                lines = index = None
            else:
    -->         start = max(start, 1)
                start = max(0, min(start, len(lines) - context))
                lines = lines[start:start+context]
                index = lineno - 1 - start

    I think that

                start = max(start, 1)

    should be replaced by

                start = max(start, 0)

    For some reason getframeinfo() (and the functions which use it) don't seem to be tested by the testsuite...

    @SamBreese
    Copy link
    Mannequin

    SamBreese mannequin commented Sep 30, 2012

    Looking into this now. Should have a patch either later today or tommorow.

    @SamBreese
    Copy link
    Mannequin

    SamBreese mannequin commented Sep 30, 2012

    Also, would you mind posting an example? I'm having trouble replicating.

    @SamBreese
    Copy link
    Mannequin

    SamBreese mannequin commented Sep 30, 2012

    Nevermind, replicated it. Changing start = max(start, 1) to start = max(start, 0) DOES fix. Writing a test case now.

    @SamBreese
    Copy link
    Mannequin

    SamBreese mannequin commented Sep 30, 2012

    Here's a patch. Very, very simple, just changed that one line in inspect.py and wrote a highly primitive test case for inspect.getframeinfo. The test isn't actually testing the primary functionality right now, just this one bug. I can probably write more expansive coverage later, but in the interest of an expeditious reply I'm submitting now.

    @pwaller
    Copy link
    Mannequin

    pwaller mannequin commented Dec 5, 2016

    I have just hit this bug and independently invented the exact fix of changing the zero for a one. Any chance of getting this merged?

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jan 2, 2017

    New changeset 15454cad5f27 by Berker Peksag in branch '3.5':
    Issue bpo-15812: inspect.getframeinfo() now correctly shows the first line of a context
    https://hg.python.org/cpython/rev/15454cad5f27

    New changeset 410caf255a09 by Berker Peksag in branch '3.6':
    Issue bpo-15812: Merge from 3.5
    https://hg.python.org/cpython/rev/410caf255a09

    New changeset 803c3c21c3bc by Berker Peksag in branch 'default':
    Issue bpo-15812: Merge from 3.6
    https://hg.python.org/cpython/rev/803c3c21c3bc

    @berkerpeksag
    Copy link
    Member

    Thanks for the patch Sam and thanks for the ping Peter!

    @berkerpeksag berkerpeksag added 3.7 (EOL) end of life stdlib Python modules in the Lib dir labels Jan 2, 2017
    @berkerpeksag berkerpeksag added the type-bug An unexpected behavior, bug, or error label Jan 2, 2017
    @serhiy-storchaka
    Copy link
    Member

    start is bounded to 0 twice.

        start = max(start, 0)
        start = max(0, min(start, len(lines) - context))

    The first line can be just removed. Or two above lines can be rewritten as:

        start = min(start, len(lines) - context)
        start = max(start, 0)

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jan 3, 2017

    New changeset 2b6bdd6cd3f8 by Berker Peksag in branch '3.5':
    Issue bpo-15812: Delete redundant max(start, 0)
    https://hg.python.org/cpython/rev/2b6bdd6cd3f8

    New changeset 7cbcee0c53e3 by Berker Peksag in branch '3.6':
    Issue bpo-15812: Merge from 3.5
    https://hg.python.org/cpython/rev/7cbcee0c53e3

    New changeset 5b0dee884b0b by Berker Peksag in branch 'default':
    Issue bpo-15812: Merge from 3.6
    https://hg.python.org/cpython/rev/5b0dee884b0b

    @berkerpeksag
    Copy link
    Member

    You're right. Thanks for the review, Serhiy!

    @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.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants