Skip to content

Commit afed652

Browse files
author
y-p
committed
Merge pull request #6075 from y-p/PR_ipython_directive_context
Pr ipython directive context
2 parents 984aa8e + bd81dd1 commit afed652

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

doc/sphinxext/ipython_directive.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,15 @@ def block_parser(part, rgxin, rgxout, fmtin, fmtout):
247247
class EmbeddedSphinxShell(object):
248248
"""An embedded IPython instance to run inside Sphinx"""
249249

250-
def __init__(self, exec_lines=None):
250+
def __init__(self, exec_lines=None,state=None):
251251

252252
self.cout = StringIO()
253253

254254
if exec_lines is None:
255255
exec_lines = []
256256

257+
self.state = state
258+
257259
# Create config object for IPython
258260
config = Config()
259261
config.InteractiveShell.autocall = False
@@ -438,8 +440,20 @@ def process_input(self, data, input_prompt, lineno):
438440
elif is_semicolon: # get spacing right
439441
ret.append('')
440442

443+
# output any exceptions raised during execution to stdout
444+
# unless :okexcept: has been specified.
441445
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 = "\nException in %s at line %s:\n" % (filename, lineno)
453+
sys.stdout.write('\n\n>>>'+'-'*73)
454+
sys.stdout.write(s)
442455
sys.stdout.write(output)
456+
sys.stdout.write('<<<' + '-'*73+'\n\n')
443457

444458
self.cout.truncate(0)
445459
return (ret, input_lines, output, is_doctest, decorator, image_file,
@@ -735,7 +749,7 @@ def setup(self):
735749

736750
# Must be called after (potentially) importing matplotlib and
737751
# setting its backend since exec_lines might import pylab.
738-
self.shell = EmbeddedSphinxShell(exec_lines)
752+
self.shell = EmbeddedSphinxShell(exec_lines, self.state)
739753

740754
# Store IPython directive to enable better error messages
741755
self.shell.directive = self

0 commit comments

Comments
 (0)