Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2b4f66

Browse files
committed
Add a more informative error message for IPython directive.
When a bad block is detected, show more information.
1 parent db282c7 commit e2b4f66

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

IPython/sphinxext/ipython_directive.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
def block_parser(part, rgxin, rgxout, fmtin, fmtout):
173173
"""
174174
part is a string of ipython text, comprised of at most one
175-
input, one ouput, comments, and blank lines. The block parser
175+
input, one output, comments, and blank lines. The block parser
176176
parses the text into a list of::
177177
178178
blocks = [ (TOKEN0, data0), (TOKEN1, data1), ...]
@@ -297,8 +297,8 @@ def __init__(self, exec_lines=None):
297297

298298
# Create and initialize global ipython, but don't start its mainloop.
299299
# This will persist across different EmbededSphinxShell instances.
300-
atexit.register(self.cleanup)
301300
IP = InteractiveShell.instance(config=config, profile_dir=profile)
301+
atexit.register(self.cleanup)
302302

303303
# io.stdout redirect must be done after instantiating InteractiveShell
304304
io.stdout = self.cout
@@ -657,14 +657,41 @@ def process_block(self, block):
657657
image_file = None
658658
image_directive = None
659659

660+
found_input = False
660661
for token, data in block:
661662
if token == COMMENT:
662663
out_data = self.process_comment(data)
663664
elif token == INPUT:
665+
found_input = True
664666
(out_data, input_lines, output, is_doctest,
665667
decorator, image_file, image_directive) = \
666668
self.process_input(data, input_prompt, lineno)
667669
elif token == OUTPUT:
670+
if not found_input:
671+
672+
TAB = ' ' * 4
673+
linenumber = 0
674+
source = 'Unavailable'
675+
content = 'Unavailable'
676+
if self.directive:
677+
linenumber = self.directive.state.document.current_line
678+
source = self.directive.state.document.current_source
679+
content = self.directive.content
680+
# Add tabs and join into a single string.
681+
content = '\n'.join([TAB + line for line in content])
682+
683+
e = ('\n\nInvalid block: Block contains an output prompt '
684+
'without an input prompt.\n\n'
685+
'Document source: {0}\n\n'
686+
'Content begins at line {1}: \n\n{2}\n\n'
687+
'Problematic block within content: \n\n{TAB}{3}\n\n')
688+
e = e.format(source, linenumber, content, block, TAB=TAB)
689+
690+
# Write, rather than include in exception, since Sphinx
691+
# will truncate tracebacks.
692+
sys.stdout.write(e)
693+
raise RuntimeError('An invalid block was detected.')
694+
668695
out_data = \
669696
self.process_output(data, output_prompt, input_lines,
670697
output, is_doctest, decorator,
@@ -923,6 +950,8 @@ def run(self):
923950
content = self.content
924951
self.content = self.shell.process_pure_python(content)
925952

953+
# parts consists of all text within the ipython-block.
954+
# Each part is an input/output block.
926955
parts = '\n'.join(self.content).split('\n\n')
927956

928957
lines = ['.. code-block:: ipython', '']

0 commit comments

Comments
 (0)