From: Ezio Melotti Date: Sat, 30 Mar 2013 03:10:28 +0000 (+0200) Subject: #17526: fix an IndexError raised while passing code without filename to inspect.finds... X-Git-Tag: v2.7.5~127 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e66e7de5d69542d9f10010a83787ef4b87a70bf8;p=python #17526: fix an IndexError raised while passing code without filename to inspect.findsource(). Initial patch by Tyler Doyle. --- diff --git a/Lib/inspect.py b/Lib/inspect.py index 66d5186573..9e928b294f 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -525,7 +525,7 @@ def findsource(object): file = getfile(object) sourcefile = getsourcefile(object) - if not sourcefile and file[0] + file[-1] != '<>': + if not sourcefile and file[:1] + file[-1:] != '<>': raise IOError('source code not available') file = sourcefile if sourcefile else file diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 30b1556cbf..04dcfe992a 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -404,6 +404,12 @@ class TestBuggyCases(GetSourceBase): self.assertEqual(inspect.findsource(co), (lines,0)) self.assertEqual(inspect.getsource(co), lines[0]) + def test_findsource_without_filename(self): + for fname in ['', '']: + co = compile('x=1', fname, "exec") + self.assertRaises(IOError, inspect.findsource, co) + self.assertRaises(IOError, inspect.getsource, co) + class _BrokenDataDescriptor(object): """ diff --git a/Misc/NEWS b/Misc/NEWS index 37802d8750..ecdf606f7b 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,9 @@ Core and Builtins Library ------- +- Issue #17526: fix an IndexError raised while passing code without filename to + inspect.findsource(). Initial patch by Tyler Doyle. + What's New in Python 2.7.4? ===========================