#17526: fix an IndexError raised while passing code without filename to inspect.finds...
authorEzio Melotti <ezio.melotti@gmail.com>
Sat, 30 Mar 2013 03:10:28 +0000 (05:10 +0200)
committerEzio Melotti <ezio.melotti@gmail.com>
Sat, 30 Mar 2013 03:10:28 +0000 (05:10 +0200)
Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS

index 66d51865734ab0b6bf534d9ea36d561ffa3755e4..9e928b294f9e507ec369f7df99ae3e39cb3f86a2 100644 (file)
@@ -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
 
index 30b1556cbfbb1908dcb2763a57ba6c1491e3cf3f..04dcfe992a491d79c1c250d0d1108ac378813b72 100644 (file)
@@ -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 ['', '<string>']:
+            co = compile('x=1', fname, "exec")
+            self.assertRaises(IOError, inspect.findsource, co)
+            self.assertRaises(IOError, inspect.getsource, co)
+
 
 class _BrokenDataDescriptor(object):
     """
index 37802d8750bbbd7006876526caf91814be4ae52a..ecdf606f7bdae0185e88d6ac005f029fb20a90bd 100644 (file)
--- 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?
 ===========================