# HG changeset patch # User Dirkjan Ochtman # Date 1307440055 -7200 # Branch 2.7 # Node ID ef44d59c71d4311b7d0c1969ca47069c0acbefd0 # Parent 6e7a98cfcfabfd9e24291a1502f6e7b18c8442bb 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_doctest.py b/Lib/test/test_doctest.py --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -5,7 +5,7 @@ import sys from test import test_support -import doctest +import doctest, inspect # NOTE: There are some additional tests relating to interaction with # zipimport in the test_zipimport_support test module. @@ -2578,6 +2578,13 @@ TestResults(failed=0, attempted=4) """ +def test_inspect(): + ''' + >>> def x(): pass + >>> inspect.getsource(x) + 'def x(): pass\\n' + ''' + ###################################################################### ## Main ######################################################################