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 2e0760bb2e)

Co-authored-by: Irit Katriel <iritkatriel@yahoo.com>
This commit is contained in:
Miss Islington (bot) 2020-12-04 13:44:53 -08:00 committed by GitHub
parent 3b14f18205
commit a4e7d5f750
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View File

@ -837,7 +837,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')

View File

@ -713,6 +713,17 @@ class TestBuggyCases(GetSourceBase):
self.assertRaises(IOError, inspect.findsource, co)
self.assertRaises(IOError, inspect.getsource, co)
def test_findsource_with_out_of_bounds_lineno(self):
mod_len = len(inspect.getsource(mod))
src = '\n' * 2* mod_len + "def f(): pass"
co = compile(src, mod.__file__, "exec")
g, l = {}, {}
eval(co, g, l)
func = l['f']
self.assertEqual(func.__code__.co_firstlineno, 1+2*mod_len)
with self.assertRaisesRegex(IOError, "lineno is out of bounds"):
inspect.findsource(func)
def test_getsource_on_method(self):
self.assertSourceEqual(mod2.ClassWithMethod.method, 118, 119)

View File

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