[Python-checkins] r82042 - in python/branches/release31-maint: Lib/inspect.py Lib/test/test_inspect.py Misc/NEWS

r.david.murray python-checkins at python.org
Thu Jun 17 04:06:12 CEST 2010


Author: r.david.murray
Date: Thu Jun 17 04:06:12 2010
New Revision: 82042

Log:
Merged revisions 82041 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r82041 | r.david.murray | 2010-06-16 22:04:29 -0400 (Wed, 16 Jun 2010) | 16 lines
  
  Merged revisions 82039 via svnmerge from 
  svn+ssh://pythondev@svn.python.org/python/trunk
  
  ........
    r82039 | r.david.murray | 2010-06-16 21:36:52 -0400 (Wed, 16 Jun 2010) | 10 lines
    
    #8720: fix inspect regression by teaching getsourcefile about linecache.
    
    The fix for issue 4050 caused a regression:  before that fix, source
    lines in the linecache would eventually be found by inspect.  After the
    fix inspect reports an error earlier, and the source isn't found.
    The fix for the fix is to have getsourcefile look in the linecache for
    the file and return the psuedo-filename if the source is there, just as
    it already returns it if there is a PEP 302 loader.
  ........
................


Modified:
   python/branches/release31-maint/   (props changed)
   python/branches/release31-maint/Lib/inspect.py
   python/branches/release31-maint/Lib/test/test_inspect.py
   python/branches/release31-maint/Misc/NEWS

Modified: python/branches/release31-maint/Lib/inspect.py
==============================================================================
--- python/branches/release31-maint/Lib/inspect.py	(original)
+++ python/branches/release31-maint/Lib/inspect.py	Thu Jun 17 04:06:12 2010
@@ -435,7 +435,9 @@
     if info: return info[0]
 
 def getsourcefile(object):
-    """Return the Python source file an object was defined in, if it exists."""
+    """Return the filename that can be used to locate an object's source.
+    Return None if no way can be identified to get the source.
+    """
     filename = getfile(object)
     if filename[-4:].lower() in ('.pyc', '.pyo'):
         filename = filename[:-4] + '.py'
@@ -448,6 +450,9 @@
     # only return a non-existent filename if the module has a PEP 302 loader
     if hasattr(getmodule(object, filename), '__loader__'):
         return filename
+    # or it is in the linecache
+    if filename in linecache.cache:
+        return filename
 
 def getabsfile(object, _filename=None):
     """Return an absolute path to the source or compiled file for an object.

Modified: python/branches/release31-maint/Lib/test/test_inspect.py
==============================================================================
--- python/branches/release31-maint/Lib/test/test_inspect.py	(original)
+++ python/branches/release31-maint/Lib/test/test_inspect.py	Thu Jun 17 04:06:12 2010
@@ -2,6 +2,7 @@
 import types
 import unittest
 import inspect
+import linecache
 import datetime
 import collections
 from os.path import normcase
@@ -272,6 +273,11 @@
     def test_getsourcefile(self):
         self.assertEqual(normcase(inspect.getsourcefile(mod.spam)), modfile)
         self.assertEqual(normcase(inspect.getsourcefile(git.abuse)), modfile)
+        fn = "_non_existing_filename_used_for_sourcefile_test.py"
+        co = compile("None", fn, "exec")
+        self.assertEqual(normcase(inspect.getsourcefile(co)), None)
+        linecache.cache[co.co_filename] = (1, None, "None", co.co_filename)
+        self.assertEqual(normcase(inspect.getsourcefile(co)), fn)
 
     def test_getfile(self):
         self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
@@ -370,6 +376,15 @@
         self.assertRaises(IOError, inspect.getsource, unicodedata)
         self.assertRaises(IOError, inspect.findsource, unicodedata)
 
+    def test_findsource_code_in_linecache(self):
+        lines = ["x=1"]
+        co = compile(lines[0], "_dynamically_created_file", "exec")
+        self.assertRaises(IOError, inspect.findsource, co)
+        self.assertRaises(IOError, inspect.getsource, co)
+        linecache.cache[co.co_filename] = (1, None, lines, co.co_filename)
+        self.assertEquals(inspect.findsource(co), (lines,0))
+        self.assertEquals(inspect.getsource(co), lines[0])
+
 # Helper for testing classify_class_attrs.
 def attrs_wo_objs(cls):
     return [t[:3] for t in inspect.classify_class_attrs(cls)]

Modified: python/branches/release31-maint/Misc/NEWS
==============================================================================
--- python/branches/release31-maint/Misc/NEWS	(original)
+++ python/branches/release31-maint/Misc/NEWS	Thu Jun 17 04:06:12 2010
@@ -61,6 +61,9 @@
 Library
 -------
 
+- Issue #8720: fix regression caused by fix for #4050 by making getsourcefile
+  smart enough to find source files in the linecache.
+
 - Issue #5610: feedparser no longer eats extra characters at the end of
   a body part if the body part ends with a \r\n.
 


More information about the Python-checkins mailing list