##// 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,6 +390,8 b' class EmbeddedSphinxShell(object):'
385 else:
390 else:
386 store_history = True
391 store_history = True
387
392
393 # Note: catch_warnings is not thread safe
394 with warnings.catch_warnings(record=True) as ws:
388 for i, line in enumerate(input_lines):
395 for i, line in enumerate(input_lines):
389 if line.endswith(';'):
396 if line.endswith(';'):
390 is_semicolon = True
397 is_semicolon = True
@@ -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