@@ -247,13 +247,15 @@ def block_parser(part, rgxin, rgxout, fmtin, fmtout):
247
247
class EmbeddedSphinxShell (object ):
248
248
"""An embedded IPython instance to run inside Sphinx"""
249
249
250
- def __init__ (self , exec_lines = None ):
250
+ def __init__ (self , exec_lines = None , state = None ):
251
251
252
252
self .cout = StringIO ()
253
253
254
254
if exec_lines is None :
255
255
exec_lines = []
256
256
257
+ self .state = state
258
+
257
259
# Create config object for IPython
258
260
config = Config ()
259
261
config .InteractiveShell .autocall = False
@@ -438,8 +440,20 @@ def process_input(self, data, input_prompt, lineno):
438
440
elif is_semicolon : # get spacing right
439
441
ret .append ('' )
440
442
443
+ # output any exceptions raised during execution to stdout
444
+ # unless :okexcept: has been specified.
441
445
if not is_okexcept and "Traceback" in output :
446
+ filename = self .state .document .current_source
447
+ lineno = self .state .document .current_line
448
+ try :
449
+ lineno = int (lineno ) - 1
450
+ except :
451
+ pass
452
+ s = "\n Exception in %s at line %s:\n " % (filename , lineno )
453
+ sys .stdout .write ('\n \n >>>' + '-' * 73 )
454
+ sys .stdout .write (s )
442
455
sys .stdout .write (output )
456
+ sys .stdout .write ('<<<' + '-' * 73 + '\n \n ' )
443
457
444
458
self .cout .truncate (0 )
445
459
return (ret , input_lines , output , is_doctest , decorator , image_file ,
@@ -735,7 +749,7 @@ def setup(self):
735
749
736
750
# Must be called after (potentially) importing matplotlib and
737
751
# setting its backend since exec_lines might import pylab.
738
- self .shell = EmbeddedSphinxShell (exec_lines )
752
+ self .shell = EmbeddedSphinxShell (exec_lines , self . state )
739
753
740
754
# Store IPython directive to enable better error messages
741
755
self .shell .directive = self
0 commit comments