Skip to content

Commit 05abdb9

Browse files
committed
fix: Extend definition regex to include class (fix #137477)
1 parent 97ffaf0 commit 05abdb9

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Lib/inspect.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,15 @@ def findsource(object):
10021002
lnum = object.co_firstlineno - 1
10031003
if lnum >= len(lines):
10041004
raise OSError('lineno is out of bounds')
1005+
pat = re.compile(r'^(\s*def\s)|(\s*class\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
1006+
while lnum > 0:
1007+
try:
1008+
line = lines[lnum]
1009+
except IndexError:
1010+
raise OSError('lineno is out of bounds')
1011+
if pat.match(line):
1012+
break
1013+
lnum = lnum - 1
10051014
return lines, lnum
10061015
raise OSError('could not find code object')
10071016

0 commit comments

Comments
 (0)