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 @@ -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.
You can’t perform that action at this time.
0 commit comments