Index: Lib/inspect.py =================================================================== --- Lib/inspect.py (revision 81388) +++ Lib/inspect.py (working copy) @@ -437,7 +437,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' @@ -450,6 +452,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. Index: Lib/test/test_inspect.py =================================================================== --- Lib/test/test_inspect.py (revision 81388) +++ Lib/test/test_inspect.py (working copy) @@ -3,6 +3,7 @@ import types import unittest import inspect +import linecache import datetime import collections from os.path import normcase @@ -275,6 +276,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__) @@ -373,6 +379,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)]