##// 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 110 import sys
111 111 import tempfile
112 112 import ast
113 import warnings
113 114
114 115 # To keep compatibility with various python versions
115 116 try:
@@ -245,13 +246,15 b' def block_parser(part, rgxin, rgxout, fmtin, fmtout):'
245 246 class EmbeddedSphinxShell(object):
246 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 251 self.cout = StringIO()
251 252
252 253 if exec_lines is None:
253 254 exec_lines = []
254 255
256 self.state = state
257
255 258 # Create config object for IPython
256 259 config = Config()
257 260 config.InteractiveShell.autocall = False
@@ -362,6 +365,8 b' class EmbeddedSphinxShell(object):'
362 365 is_doctest = (decorator is not None and \
363 366 decorator.startswith('@doctest')) or self.is_doctest
364 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 370 is_savefig = decorator is not None and \
366 371 decorator.startswith('@savefig')
367 372
@@ -385,28 +390,30 b' class EmbeddedSphinxShell(object):'
385 390 else:
386 391 store_history = True
387 392
388 for i, line in enumerate(input_lines):
389 if line.endswith(';'):
390 is_semicolon = True
391
392 if i == 0:
393 # process the first input line
394 if is_verbatim:
395 self.process_input_line('')
396 self.IP.execution_count += 1 # increment it anyway
393 # Note: catch_warnings is not thread safe
394 with warnings.catch_warnings(record=True) as ws:
395 for i, line in enumerate(input_lines):
396 if line.endswith(';'):
397 is_semicolon = True
398
399 if i == 0:
400 # process the first input line
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 408 else:
398 # only submit the line in non-verbatim mode
399 self.process_input_line(line, store_history=store_history)
400 formatted_line = '%s %s'%(input_prompt, line)
401 else:
402 # process a continuation line
403 if not is_verbatim:
404 self.process_input_line(line, store_history=store_history)
409 # process a continuation line
410 if not is_verbatim:
411 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:
409 ret.append(formatted_line)
415 if not is_suppress:
416 ret.append(formatted_line)
410 417
411 418 if not is_suppress and len(rest.strip()) and is_verbatim:
412 419 # the "rest" is the standard output of the
@@ -421,6 +428,34 b' class EmbeddedSphinxShell(object):'
421 428 elif is_semicolon: # get spacing right
422 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 459 self.cout.truncate(0)
425 460 return (ret, input_lines, output, is_doctest, decorator, image_file,
426 461 image_directive)
@@ -662,6 +697,8 b' class IPythonDirective(Directive):'
662 697 'suppress' : directives.flag,
663 698 'verbatim' : directives.flag,
664 699 'doctest' : directives.flag,
700 'okexcept': directives.flag,
701 'okwarning': directives.flag
665 702 }
666 703
667 704 shell = None
@@ -712,7 +749,7 b' class IPythonDirective(Directive):'
712 749
713 750 # Must be called after (potentially) importing matplotlib and
714 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 754 # Store IPython directive to enable better error messages
718 755 self.shell.directive = self
@@ -759,6 +796,8 b' class IPythonDirective(Directive):'
759 796 self.shell.is_suppress = 'suppress' in options
760 797 self.shell.is_doctest = 'doctest' in options
761 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 802 # handle pure python code
764 803 if 'python' in self.arguments:
General Comments 0
You need to be logged in to leave comments. Login now