diff options
author | Irit Katriel <iritkatriel@yahoo.com> | 2020-12-04 21:22:03 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-04 21:22:03 (GMT) |
commit | 2e0760bb2edb595050aff82f236cd32b44d3dfb3 (patch) | |
tree | efc0a4ef54bbebff196efd1d4d9e984f91a62832 /Lib/inspect.py | |
parent | 8d4f57dbd10846ffb4881b6509a511be0ab3b913 (diff) | |
download | cpython-2e0760bb2edb595050aff82f236cd32b44d3dfb3.zip cpython-2e0760bb2edb595050aff82f236cd32b44d3dfb3.tar.gz cpython-2e0760bb2edb595050aff82f236cd32b44d3dfb3.tar.bz2 |
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.
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r-- | Lib/inspect.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 073a79d..9150ac1 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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') |