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

Skip to content

Commit 703f7c4

Browse files
committed
merge 3.2 (#9284)
2 parents 3ae4272 + 9620cc0 commit 703f7c4

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

Lib/inspect.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,13 @@ def findsource(object):
518518
or code object. The source code is returned as a list of all the lines
519519
in the file and the line number indexes a line in that list. An IOError
520520
is raised if the source code cannot be retrieved."""
521-
file = getsourcefile(object)
522-
if not file:
521+
522+
file = getfile(object)
523+
sourcefile = getsourcefile(object)
524+
if not sourcefile and file[0] + file[-1] != '<>':
523525
raise IOError('source code not available')
526+
file = sourcefile if sourcefile else file
527+
524528
module = getmodule(object, file)
525529
if module:
526530
lines = linecache.getlines(file, module.__dict__)

Lib/test/test_inspect.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,23 @@ def test_getmodule_recursion(self):
298298
del sys.modules[name]
299299
inspect.getmodule(compile('a=10','','single'))
300300

301+
def test_proceed_with_fake_filename(self):
302+
'''doctest monkeypatches linecache to enable inspection'''
303+
fn, source = '<test>', 'def x(): pass\n'
304+
getlines = linecache.getlines
305+
def monkey(filename, module_globals=None):
306+
if filename == fn:
307+
return source.splitlines(True)
308+
else:
309+
return getlines(filename, module_globals)
310+
linecache.getlines = monkey
311+
try:
312+
ns = {}
313+
exec(compile(source, fn, 'single'), ns)
314+
inspect.getsource(ns["x"])
315+
finally:
316+
linecache.getlines = getlines
317+
301318
class TestDecorators(GetSourceBase):
302319
fodderModule = mod2
303320

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ Library
190190
- Issue #12240: Allow multiple setup hooks in packaging's setup.cfg files.
191191
Original patch by Erik Bray.
192192

193+
- Issue #9284: Allow inspect.findsource() to find the source of doctest
194+
functions.
195+
193196
- Issue #11595: Fix assorted bugs in packaging.util.cfg_to_args, a
194197
compatibility helper for the distutils-packaging transition. Original patch
195198
by Erik Bray.

0 commit comments

Comments
 (0)