Skip to content

Commit 2e0760b

Browse files
authored
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.
1 parent 8d4f57d commit 2e0760b

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Lib/inspect.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,12 @@ def findsource(object):
868868
lnum = object.co_firstlineno - 1
869869
pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
870870
while lnum > 0:
871-
if pat.match(lines[lnum]): break
871+
try:
872+
line = lines[lnum]
873+
except IndexError:
874+
raise OSError('lineno is out of bounds')
875+
if pat.match(line):
876+
break
872877
lnum = lnum - 1
873878
return lines, lnum
874879
raise OSError('could not find code object')

Lib/test/test_inspect.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,17 @@ def test_findsource_without_filename(self):
712712
self.assertRaises(IOError, inspect.findsource, co)
713713
self.assertRaises(IOError, inspect.getsource, co)
714714

715+
def test_findsource_with_out_of_bounds_lineno(self):
716+
mod_len = len(inspect.getsource(mod))
717+
src = '\n' * 2* mod_len + "def f(): pass"
718+
co = compile(src, mod.__file__, "exec")
719+
g, l = {}, {}
720+
eval(co, g, l)
721+
func = l['f']
722+
self.assertEqual(func.__code__.co_firstlineno, 1+2*mod_len)
723+
with self.assertRaisesRegex(IOError, "lineno is out of bounds"):
724+
inspect.findsource(func)
725+
715726
def test_getsource_on_method(self):
716727
self.assertSourceEqual(mod2.ClassWithMethod.method, 118, 119)
717728

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:func:`inspect.findsource` now raises :exc:`OSError` instead of
2+
:exc:`IndexError` when :attr:`co_lineno` of a code object is greater than the
3+
file length. This can happen, for example, when a file is edited after it was
4+
imported. PR by Irit Katriel.

0 commit comments

Comments
 (0)