##// END OF EJS Templates
ipython_directive, report except/warn in block and add :okexcept: :okwarning: options to suppress
y-p -
Show More
@@ -110,6 +110,7 b' import re'
110 import sys
110 import sys
111 import tempfile
111 import tempfile
112 import ast
112 import ast
113 import warnings
113
114
114 # To keep compatibility with various python versions
115 # To keep compatibility with various python versions
115 try:
116 try:
@@ -245,13 +246,15 b' def block_parser(part, rgxin, rgxout, fmtin, fmtout):'
245 class EmbeddedSphinxShell(object):
246 class EmbeddedSphinxShell(object):
246 """An embedded IPython instance to run inside Sphinx"""
247 """An embedded IPython instance to run inside Sphinx"""
247
248
248 def __init__(self, exec_lines=None):
249 def __init__(self, exec_lines=None,state=None):
249
250
250 self.cout = StringIO()
251 self.cout = StringIO()
251
252
252 if exec_lines is None:
253 if exec_lines is None:
253 exec_lines = []
254 exec_lines = []
254
255
256 self.state = state
257
255 # Create config object for IPython
258 # Create config object for IPython
256 config = Config()
259 config = Config()
257 config.InteractiveShell.autocall = False
260 config.InteractiveShell.autocall = False
@@ -362,6 +365,8 b' class EmbeddedSphinxShell(object):'
362 is_doctest = (decorator is not None and \
365 is_doctest = (decorator is not None and \
363 decorator.startswith('@doctest')) or self.is_doctest
366 decorator.startswith('@doctest')) or self.is_doctest
364 is_suppress = decorator=='@suppress' or self.is_suppress
367 is_suppress = decorator=='@suppress' or self.is_suppress
368 is_okexcept = decorator=='@okexcept' or self.is_okexcept
369 is_okwarning = decorator=='@okwarning' or self.is_okwarning
365 is_savefig = decorator is not None and \
370 is_savefig = decorator is not None and \
366 decorator.startswith('@savefig')
371 decorator.startswith('@savefig')
367
372
@@ -385,28 +390,30 b' class EmbeddedSphinxShell(object):'
385 else:
390 else:
386 store_history = True
391 store_history = True
387
392
388 for i, line in enumerate(input_lines):
393 # Note: catch_warnings is not thread safe
389 if line.endswith(';'):
394 with warnings.catch_warnings(record=True) as ws:
390 is_semicolon = True
395 for i, line in enumerate(input_lines):
391
396 if line.endswith(';'):
392 if i == 0:
397 is_semicolon = True
393 # process the first input line
398
394 if is_verbatim:
399 if i == 0:
395 self.process_input_line('')
400 # process the first input line
396 self.IP.execution_count += 1 # increment it anyway
401 if is_verbatim:
402 self.process_input_line('')
403 self.IP.execution_count += 1 # increment it anyway
404 else:
405 # only submit the line in non-verbatim mode
406 self.process_input_line(line, store_history=store_history)
407 formatted_line = '%s %s'%(input_prompt, line)
397 else:
408 else:
398 # only submit the line in non-verbatim mode
409 # process a continuation line
399 self.process_input_line(line, store_history=store_history)
410 if not is_verbatim:
400 formatted_line = '%s %s'%(input_prompt, line)
411 self.process_input_line(line, store_history=store_history)
401 else:
402 # process a continuation line
403 if not is_verbatim:
404 self.process_input_line(line, store_history=store_history)
405
412
406 formatted_line = '%s %s'%(continuation, line)
413 formatted_line = '%s %s'%(continuation, line)
407
414
408 if not is_suppress:
415 if not is_suppress:
409 ret.append(formatted_line)
416 ret.append(formatted_line)
410
417
411 if not is_suppress and len(rest.strip()) and is_verbatim:
418 if not is_suppress and len(rest.strip()) and is_verbatim:
412 # the "rest" is the standard output of the
419 # the "rest" is the standard output of the
@@ -421,6 +428,34 b' class EmbeddedSphinxShell(object):'
421 elif is_semicolon: # get spacing right
428 elif is_semicolon: # get spacing right
422 ret.append('')
429 ret.append('')
423
430
431 # context information
432 filename = self.state.document.current_source
433 lineno = self.state.document.current_line
434
435 # output any exceptions raised during execution to stdout
436 # unless :okexcept: has been specified.
437 if not is_okexcept and "Traceback" in output:
438 s = "\nException in %s at block ending on line %s\n" % (filename, lineno)
439 s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n"
440 sys.stdout.write('\n\n>>>' + ('-' * 73))
441 sys.stdout.write(s)
442 sys.stdout.write(output)
443 sys.stdout.write('<<<' + ('-' * 73) + '\n\n')
444
445 # output any warning raised during execution to stdout
446 # unless :okwarning: has been specified.
447 if not is_okwarning:
448 for w in ws:
449 s = "\nWarning in %s at block ending on line %s\n" % (filename, lineno)
450 s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n"
451 sys.stdout.write('\n\n>>>' + ('-' * 73))
452 sys.stdout.write(s)
453 sys.stdout.write(('-' * 76) + '\n')
454 s=warnings.formatwarning(w.message, w.category,
455 w.filename, w.lineno, w.line)
456 sys.stdout.write(s)
457 sys.stdout.write('<<<' + ('-' * 73) + '\n')
458
424 self.cout.truncate(0)
459 self.cout.truncate(0)
425 return (ret, input_lines, output, is_doctest, decorator, image_file,
460 return (ret, input_lines, output, is_doctest, decorator, image_file,
426 image_directive)
461 image_directive)
@@ -662,6 +697,8 b' class IPythonDirective(Directive):'
662 'suppress' : directives.flag,
697 'suppress' : directives.flag,
663 'verbatim' : directives.flag,
698 'verbatim' : directives.flag,
664 'doctest' : directives.flag,
699 'doctest' : directives.flag,
700 'okexcept': directives.flag,
701 'okwarning': directives.flag
665 }
702 }
666
703
667 shell = None
704 shell = None
@@ -712,7 +749,7 b' class IPythonDirective(Directive):'
712
749
713 # Must be called after (potentially) importing matplotlib and
750 # Must be called after (potentially) importing matplotlib and
714 # setting its backend since exec_lines might import pylab.
751 # setting its backend since exec_lines might import pylab.
715 self.shell = EmbeddedSphinxShell(exec_lines)
752 self.shell = EmbeddedSphinxShell(exec_lines, self.state)
716
753
717 # Store IPython directive to enable better error messages
754 # Store IPython directive to enable better error messages
718 self.shell.directive = self
755 self.shell.directive = self
@@ -759,6 +796,8 b' class IPythonDirective(Directive):'
759 self.shell.is_suppress = 'suppress' in options
796 self.shell.is_suppress = 'suppress' in options
760 self.shell.is_doctest = 'doctest' in options
797 self.shell.is_doctest = 'doctest' in options
761 self.shell.is_verbatim = 'verbatim' in options
798 self.shell.is_verbatim = 'verbatim' in options
799 self.shell.is_okexcept = 'okexcept' in options
800 self.shell.is_okwarning = 'okwarning' in options
762
801
763 # handle pure python code
802 # handle pure python code
764 if 'python' in self.arguments:
803 if 'python' in self.arguments:
General Comments 0
You need to be logged in to leave comments. Login now