From 2e0760bb2edb595050aff82f236cd32b44d3dfb3 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 4 Dec 2020 21:22:03 +0000 Subject: [PATCH] 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. --- Lib/inspect.py | 7 ++++++- Lib/test/test_inspect.py | 11 +++++++++++ .../Library/2020-12-03-22-22-24.bpo-17735.Qsaaue.rst | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-12-03-22-22-24.bpo-17735.Qsaaue.rst diff --git a/Lib/inspect.py b/Lib/inspect.py index 073a79d97ac..9150ac104dc 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)|(.*(? 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') diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 172e6bf6cd8..c81d828b57e 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -712,6 +712,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) diff --git a/Misc/NEWS.d/next/Library/2020-12-03-22-22-24.bpo-17735.Qsaaue.rst b/Misc/NEWS.d/next/Library/2020-12-03-22-22-24.bpo-17735.Qsaaue.rst new file mode 100644 index 00000000000..655781e3d2e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-12-03-22-22-24.bpo-17735.Qsaaue.rst @@ -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.