-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Closed
Labels
stdlibPython modules in the Lib dirPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
In the following MRE, code.co_firstlineno
correctly returns 4, the first line number of the class definition, but getsource(code)
returns the content of the entire file:
import sys
from inspect import getsource
class A:
code = sys._getframe(0).f_code
print(code.co_firstlineno)
print(getsource(code))
This is because inspect.findsource
uses a regex pattern that does not match class
:
Line 1155 in a8e93d3
pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') |
CPython versions tested on:
3.12
Operating systems tested on:
Linux, Windows
Linked PRs
Metadata
Metadata
Assignees
Labels
stdlibPython modules in the Lib dirPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error