mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
gh-116987: Support class code objects in inspect.findsource() (GH-117025)
This commit is contained in:
parent
6547330f4e
commit
d16c9d1278
4 changed files with 11 additions and 9 deletions
|
@ -1157,15 +1157,8 @@ def findsource(object):
|
|||
if not hasattr(object, 'co_firstlineno'):
|
||||
raise OSError('could not find function definition')
|
||||
lnum = object.co_firstlineno - 1
|
||||
pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
|
||||
while lnum > 0:
|
||||
try:
|
||||
line = lines[lnum]
|
||||
except IndexError:
|
||||
raise OSError('lineno is out of bounds')
|
||||
if pat.match(line):
|
||||
break
|
||||
lnum = lnum - 1
|
||||
if lnum >= len(lines):
|
||||
raise OSError('lineno is out of bounds')
|
||||
return lines, lnum
|
||||
raise OSError('could not find code object')
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue