File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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__ )
Original file line number Diff line number Diff 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+
301318class TestDecorators (GetSourceBase ):
302319 fodderModule = mod2
303320
Original file line number Diff line number Diff line change @@ -22,6 +22,9 @@ Core and Builtins
2222Library
2323-------
2424
25+ - Issue #9284: Allow inspect.findsource() to find the source of doctest
26+ functions.
27+
2528- Issue #12009: Fixed regression in netrc file comment handling.
2629
2730- Issue #10694: zipfile now ignores garbage at the end of a zipfile.
You can’t perform that action at this time.
0 commit comments