mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
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.
This commit is contained in:
parent
8d4f57dbd1
commit
2e0760bb2e
3 changed files with 21 additions and 1 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue