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:
|
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
Bug report
Bug description:
In the following MRE,
code.co_firstlinenocorrectly returns 4, the first line number of the class definition, butgetsource(code)returns the content of the entire file:This is because
inspect.findsourceuses a regex pattern that does not matchclass:cpython/Lib/inspect.py
Line 1155 in a8e93d3
CPython versions tested on:
3.12
Operating systems tested on:
Linux, Windows
Linked PRs