mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-17735: inspect.findsource now raises OSError when co_lineno is out of range (GH-23633)
This can happen when a file was edited after it was imported.
This commit is contained in:
parent
8d4f57dbd1
commit
2e0760bb2e
3 changed files with 21 additions and 1 deletions
|
@ -868,7 +868,12 @@ def findsource(object):
|
|||
lnum = object.co_firstlineno - 1
|
||||
pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
|
||||
while lnum > 0:
|
||||
if pat.match(lines[lnum]): break
|
||||
try:
|
||||
line = lines[lnum]
|
||||
except IndexError:
|
||||
raise OSError('lineno is out of bounds')
|
||||
if pat.match(line):
|
||||
break
|
||||
lnum = lnum - 1
|
||||
return lines, lnum
|
||||
raise OSError('could not find code object')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue