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.findsource breaks on class frame objects #79282

Open
orlnub123 mannequin opened this issue Oct 29, 2018 · 3 comments
Open

inspect.findsource breaks on class frame objects #79282

orlnub123 mannequin opened this issue Oct 29, 2018 · 3 comments
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@orlnub123
Copy link
Mannequin

orlnub123 mannequin commented Oct 29, 2018

BPO 35101
Nosy @aivarannamaa, @1st1, @tirkarthi, @orlnub123
PRs
  • gh-79282: Fix inspect.findsource breaking on class frame objects #10209
  • 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 = None
    created_at = <Date 2018-10-29.04:17:50.821>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = 'inspect.findsource breaks on class frame objects'
    updated_at = <Date 2019-01-26.06:52:01.121>
    user = 'https://github.com/orlnub123'

    bugs.python.org fields:

    activity = <Date 2019-01-26.06:52:01.121>
    actor = 'Aivar.Annamaa'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2018-10-29.04:17:50.821>
    creator = 'orlnub123'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35101
    keywords = ['patch']
    message_count = 3.0
    messages = ['328788', '328800', '329033']
    nosy_count = 4.0
    nosy_names = ['Aivar.Annamaa', 'yselivanov', 'xtreak', 'orlnub123']
    pr_nums = ['10209']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue35101'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

    @orlnub123
    Copy link
    Mannequin Author

    orlnub123 mannequin commented Oct 29, 2018

    If you pass a frame object belonging to a class into findsource, it'll incorrectly give you the starting line number of the first function above it or 0 if no functions are defined.

    Here's some code to reproduce the issue:

    import inspect
    
    def test_func():
        ...  # This should not get printed
    
    class Test:
        frame = inspect.currentframe()
    
    print(inspect.getsource(Test.frame))  # Depends on findsource

    This unexpectedly prints the source of test_func instead of the class right below it.

    @orlnub123 orlnub123 mannequin added 3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 29, 2018
    @tirkarthi
    Copy link
    Member

    Seems there is a related open issue with a similar regex based solution proposed for another class definition related issue : https://bugs.python.org/issue22355#msg226538 . I think it's worth converting the example code reported in bpo-22355 as unit test cases. Since this issue already has a PR with tests then we can proceed further on this.

    Thanks for the report!

    @orlnub123
    Copy link
    Mannequin Author

    orlnub123 mannequin commented Nov 1, 2018

    Added the requested test cases. They aren't an exact match but they should serve the same purpose.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @erlend-aasland erlend-aasland added 3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes and removed 3.8 only security fixes 3.7 (EOL) end of life labels Jan 8, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes 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