# HG changeset patch # User Dirkjan Ochtman # Date 1307636821 -7200 # Branch 2.7 # Node ID 14dc1b7a1d143c5bb50a01fe2f844dc1448cc60c # Parent fe813f5711a50057e45e720454caddff2c614d6c Issue #9284: fix regression in doctest inspection diff --git a/Lib/inspect.py b/Lib/inspect.py --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -524,9 +524,13 @@ or code object. The source code is returned as a list of all the lines in the file and the line number indexes a line in that list. An IOError is raised if the source code cannot be retrieved.""" - file = getsourcefile(object) - if not file: + + file = getfile(object) + sourcefile = getsourcefile(object) + if not sourcefile and file[0] + file[-1] != '<>': raise IOError('source code not available') + file = sourcefile if sourcefile else file + module = getmodule(object, file) if module: lines = linecache.getlines(file, module.__dict__) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -295,6 +295,20 @@ del sys.modules[name] inspect.getmodule(compile('a=10','','single')) + def test_doctest(self): + '''doctest monkeypatches linecache to enable inspection''' + fn, source = '', 'def x(): pass\n' + getlines = linecache.getlines + def monkey(filename, module_globals=None): + if filename == fn: + return source.splitlines(True) + else: + return getlines(filename, module_globals) + linecache.getlines = monkey + exec compile(source, fn, 'single') in globals() + inspect.getsource(x) + linecache.getlines = getlines + class TestDecorators(GetSourceBase): fodderFile = mod2