From 9620cc04634e720d1d016cce4bf02e0c27607e64 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 11 Jun 2011 15:53:11 -0500 Subject: [PATCH] allow "fake" filenames in findsource (closes #9284) This allows findsource() to work in doctests. A patch from Dirkjan Ochtman. --- Lib/inspect.py | 8 ++++++-- Lib/test/test_inspect.py | 17 +++++++++++++++++ Misc/NEWS | 3 +++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 4899cbf9f0..bb46ea6f85 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -518,9 +518,13 @@ def findsource(object): 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 index f5dff1e543..7666fe43a7 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -298,6 +298,23 @@ class TestRetrievingSourceCode(GetSourceBase): del sys.modules[name] inspect.getmodule(compile('a=10','','single')) + def test_proceed_with_fake_filename(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 + try: + ns = {} + exec(compile(source, fn, 'single'), ns) + inspect.getsource(ns["x"]) + finally: + linecache.getlines = getlines + class TestDecorators(GetSourceBase): fodderModule = mod2 diff --git a/Misc/NEWS b/Misc/NEWS index d3496b0f2d..de1c531025 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -22,6 +22,9 @@ Core and Builtins Library ------- +- Issue #9284: Allow inspect.findsource() to find the source of doctest + functions. + - Issue #12009: Fixed regression in netrc file comment handling. - Issue #10694: zipfile now ignores garbage at the end of a zipfile. -- 2.40.0