Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a1b3740

Browse files
committed
Merged revisions 82039 via svnmerge from
svn+ssh://[email protected]/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. ........
1 parent 2bdab24 commit a1b3740

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

Lib/inspect.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,9 @@ def getmodulename(path):
437437
if info: return info[0]
438438

439439
def getsourcefile(object):
440-
"""Return the Python source file an object was defined in, if it exists."""
440+
"""Return the filename that can be used to locate an object's source.
441+
Return None if no way can be identified to get the source.
442+
"""
441443
filename = getfile(object)
442444
if filename[-4:].lower() in ('.pyc', '.pyo'):
443445
filename = filename[:-4] + '.py'
@@ -450,6 +452,9 @@ def getsourcefile(object):
450452
# only return a non-existent filename if the module has a PEP 302 loader
451453
if hasattr(getmodule(object, filename), '__loader__'):
452454
return filename
455+
# or it is in the linecache
456+
if filename in linecache.cache:
457+
return filename
453458

454459
def getabsfile(object, _filename=None):
455460
"""Return an absolute path to the source or compiled file for an object.

Lib/test/test_inspect.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import types
44
import unittest
55
import inspect
6+
import linecache
67
import datetime
78
import collections
89
from os.path import normcase
@@ -275,6 +276,11 @@ def test_getsource(self):
275276
def test_getsourcefile(self):
276277
self.assertEqual(normcase(inspect.getsourcefile(mod.spam)), modfile)
277278
self.assertEqual(normcase(inspect.getsourcefile(git.abuse)), modfile)
279+
fn = "_non_existing_filename_used_for_sourcefile_test.py"
280+
co = compile("None", fn, "exec")
281+
self.assertEqual(normcase(inspect.getsourcefile(co)), None)
282+
linecache.cache[co.co_filename] = (1, None, "None", co.co_filename)
283+
self.assertEqual(normcase(inspect.getsourcefile(co)), fn)
278284

279285
def test_getfile(self):
280286
self.assertEqual(inspect.getfile(mod.StupidGit), mod.__file__)
@@ -373,6 +379,15 @@ def test_findsource_binary(self):
373379
self.assertRaises(IOError, inspect.getsource, unicodedata)
374380
self.assertRaises(IOError, inspect.findsource, unicodedata)
375381

382+
def test_findsource_code_in_linecache(self):
383+
lines = ["x=1"]
384+
co = compile(lines[0], "_dynamically_created_file", "exec")
385+
self.assertRaises(IOError, inspect.findsource, co)
386+
self.assertRaises(IOError, inspect.getsource, co)
387+
linecache.cache[co.co_filename] = (1, None, lines, co.co_filename)
388+
self.assertEquals(inspect.findsource(co), (lines,0))
389+
self.assertEquals(inspect.getsource(co), lines[0])
390+
376391
# Helper for testing classify_class_attrs.
377392
def attrs_wo_objs(cls):
378393
return [t[:3] for t in inspect.classify_class_attrs(cls)]

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ C-API
437437
Library
438438
-------
439439

440+
- Issue #8720: fix regression caused by fix for #4050 by making getsourcefile
441+
smart enough to find source files in the linecache.
442+
440443
- Issue #5610: feedparser no longer eats extra characters at the end of
441444
a body part if the body part ends with a \r\n.
442445

0 commit comments

Comments
 (0)