diff -r 4c07b9c49b75 Lib/inspect.py --- a/Lib/inspect.py Mon Jun 18 15:37:31 2012 +0100 +++ b/Lib/inspect.py Thu Jun 21 14:34:51 2012 -0700 @@ -577,7 +577,10 @@ def findsource(object): lnum = object.co_firstlineno - 1 pat = re.compile(r'^(\s*def\s)|(.*(? 0: - if pat.match(lines[lnum]): break + try: + if pat.match(lines[lnum]): break + except IndexError: + raise IOError('could not get source code; invalid line number %d' % (lnum,)) lnum = lnum - 1 return lines, lnum raise IOError('could not find code object') diff -r 4c07b9c49b75 Lib/test/inspect_fodder3.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/test/inspect_fodder3.py Thu Jun 21 14:34:51 2012 -0700 @@ -0,0 +1,1 @@ +# this file has one line diff -r 4c07b9c49b75 Lib/test/test_inspect.py --- a/Lib/test/test_inspect.py Mon Jun 18 15:37:31 2012 +0100 +++ b/Lib/test/test_inspect.py Thu Jun 21 14:34:51 2012 -0700 @@ -15,6 +15,7 @@ with check_py3k_warnings( quiet=True): from test import inspect_fodder as mod from test import inspect_fodder2 as mod2 + from test import inspect_fodder3 as mod3 # C module for test_findsource_binary import unicodedata @@ -404,6 +405,14 @@ class TestBuggyCases(GetSourceBase): self.assertEqual(inspect.findsource(co), (lines,0)) self.assertEqual(inspect.getsource(co), lines[0]) + def test_findsource_code_with_line_number_out_of_range(self): + # mod3 is only a single line, but our source has multiple lines. + source = '\n\n1 // 0' + co = compile(source, mod3.__file__, "exec") + self.assertRaises(IOError, inspect.getsource, co) + self.assertRaises(IOError, inspect.findsource, co) + + class _BrokenDataDescriptor(object): """ diff -r 4c07b9c49b75 Misc/ACKS --- a/Misc/ACKS Mon Jun 18 15:37:31 2012 +0100 +++ b/Misc/ACKS Thu Jun 21 14:34:51 2012 -0700 @@ -867,6 +867,7 @@ Laurence Tratt John Tromp Jason Trowbridge Anthony Tuininga +Kevin Turner Stephen Turner Theodore Turocy Bill Tutt