diff options
author | Benjamin Peterson <benjamin@python.org> | 2011-06-11 20:53:11 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2011-06-11 20:53:11 (GMT) |
commit | 9620cc04634e720d1d016cce4bf02e0c27607e64 (patch) | |
tree | 2ec667b21d599bada69991c37907a65bb8398417 /Lib/test/test_inspect.py | |
parent | 9fbfe15c86a079f03f54b20106cdefa402325330 (diff) | |
download | cpython-9620cc04634e720d1d016cce4bf02e0c27607e64.zip cpython-9620cc04634e720d1d016cce4bf02e0c27607e64.tar.gz cpython-9620cc04634e720d1d016cce4bf02e0c27607e64.tar.bz2 |
allow "fake" filenames in findsource (closes #9284)
This allows findsource() to work in doctests.
A patch from Dirkjan Ochtman.
Diffstat (limited to 'Lib/test/test_inspect.py')
-rw-r--r-- | Lib/test/test_inspect.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index f5dff1e..7666fe4 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 = '<test>', '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 |