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: inspect.findsource breaks on class frame objects
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Aivar.Annamaa, orlnub123, xtreak, yselivanov
Priority: normal Keywords: patch

Created on 2018-10-29 04:17 by orlnub123, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 10209 open orlnub123, 2018-10-29 04:20
Messages (3)
msg328788 - (view) Author: (orlnub123) * Date: 2018-10-29 04:17
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.
msg328800 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-10-29 10:57
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 issue22355 as unit test cases. Since this issue already has a PR with tests then we can proceed further on this.

Thanks for the report!
msg329033 - (view) Author: (orlnub123) * Date: 2018-11-01 01:54
Added the requested test cases. They aren't an exact match but they should serve the same purpose.
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79282
2019-01-26 06:52:01Aivar.Annamaasetnosy: + Aivar.Annamaa
2018-11-01 01:54:38orlnub123setmessages: + msg329033
2018-10-29 10:57:47xtreaksetmessages: + msg328800
2018-10-29 09:11:30xtreaksetnosy: + yselivanov, xtreak
2018-10-29 04:20:11orlnub123setkeywords: + patch
stage: patch review
pull_requests: + pull_request9527
2018-10-29 04:17:50orlnub123create