Skip to content

Commit bd81dd1

Browse files
author
y-p
committed
BLD/DOC: add context info on exception during sphinx run
1 parent 3f2b58c commit bd81dd1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

doc/sphinxext/ipython_directive.py

Lines changed: 14 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
@@ -441,7 +443,17 @@ def process_input(self, data, input_prompt, lineno):
441443
# output any exceptions raised during execution to stdout
442444
# unless :okexcept: has been specified.
443445
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)
444455
sys.stdout.write(output)
456+
sys.stdout.write('<<<' + '-'*73+'\n\n')
445457

446458
self.cout.truncate(0)
447459
return (ret, input_lines, output, is_doctest, decorator, image_file,
@@ -737,7 +749,7 @@ def setup(self):
737749

738750
# Must be called after (potentially) importing matplotlib and
739751
# setting its backend since exec_lines might import pylab.
740-
self.shell = EmbeddedSphinxShell(exec_lines)
752+
self.shell = EmbeddedSphinxShell(exec_lines, self.state)
741753

742754
# Store IPython directive to enable better error messages
743755
self.shell.directive = self

0 commit comments

Comments
 (0)