#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.
This commit is contained in:
R. David Murray 2010-06-17 01:36:52 +00:00
parent a3e3e36de7
commit df1cf301c2
3 changed files with 24 additions and 1 deletions

View file

@ -440,7 +440,9 @@ def getmodulename(path):
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 string.lower(filename[-4:]) in ('.pyc', '.pyo'):
filename = filename[:-4] + '.py'
@ -453,6 +455,9 @@ def getsourcefile(object):
# 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.