Skip to content

Commit d1f0741

Browse files
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. (cherry picked from commit 2e0760b) Co-authored-by: Irit Katriel <[email protected]>
1 parent 40b92f1 commit d1f0741

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
@@ -864,7 +864,12 @@ def findsource(object):
864864
lnum = object.co_firstlineno - 1
865865
pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
866866
while lnum > 0:
867-
if pat.match(lines[lnum]): break
867+
try:
868+
line = lines[lnum]
869+
except IndexError:
870+
raise OSError('lineno is out of bounds')
871+
if pat.match(line):
872+
break
868873
lnum = lnum - 1
869874
return lines, lnum
870875
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
@@ -710,6 +710,17 @@ def test_findsource_without_filename(self):
710710
self.assertRaises(IOError, inspect.findsource, co)
711711
self.assertRaises(IOError, inspect.getsource, co)
712712

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

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)