##// END OF EJS Templates
Use Sphinx logging in IPython directive
Richard Shadrach -
Show More
@@ -0,0 +1,4 b''
1 IPython directive warnings
2 --------------------------
3
4 The IPython directive now uses Sphinx logging for warnings.
@@ -200,6 +200,7 b' from io import StringIO'
200 # Third-party
200 # Third-party
201 from docutils.parsers.rst import directives
201 from docutils.parsers.rst import directives
202 from docutils.parsers.rst import Directive
202 from docutils.parsers.rst import Directive
203 from sphinx.util import logging
203
204
204 # Our own
205 # Our own
205 from traitlets.config import Config
206 from traitlets.config import Config
@@ -556,16 +557,19 b' class EmbeddedSphinxShell(object):'
556 if self.directive.state:
557 if self.directive.state:
557 filename = self.directive.state.document.current_source
558 filename = self.directive.state.document.current_source
558 lineno = self.directive.state.document.current_line
559 lineno = self.directive.state.document.current_line
560
561 # Use sphinx logger for warnings
562 logger = logging.getLogger(__name__)
559
563
560 # output any exceptions raised during execution to stdout
564 # output any exceptions raised during execution to stdout
561 # unless :okexcept: has been specified.
565 # unless :okexcept: has been specified.
562 if not is_okexcept and (("Traceback" in processed_output) or ("SyntaxError" in processed_output)):
566 if not is_okexcept and (("Traceback" in processed_output) or ("SyntaxError" in processed_output)):
563 s = "\nException in %s at block ending on line %s\n" % (filename, lineno)
567 s = '\n>>>' + ('-' * 73) + '\n'
568 s += "Exception in %s at block ending on line %s\n" % (filename, lineno)
564 s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n"
569 s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n"
565 sys.stdout.write('\n\n>>>' + ('-' * 73))
570 s += processed_output + "\n"
566 sys.stdout.write(s)
571 s += "<<<" + ("-" * 73)
567 sys.stdout.write(processed_output)
572 logger.warning(s)
568 sys.stdout.write('<<<' + ('-' * 73) + '\n\n')
569 if self.warning_is_error:
573 if self.warning_is_error:
570 raise RuntimeError('Non Expected exception in `{}` line {}'.format(filename, lineno))
574 raise RuntimeError('Non Expected exception in `{}` line {}'.format(filename, lineno))
571
575
@@ -573,15 +577,14 b' class EmbeddedSphinxShell(object):'
573 # unless :okwarning: has been specified.
577 # unless :okwarning: has been specified.
574 if not is_okwarning:
578 if not is_okwarning:
575 for w in ws:
579 for w in ws:
576 s = "\nWarning in %s at block ending on line %s\n" % (filename, lineno)
580 s = '\n>>>' + ('-' * 73) + '\n'
581 s += "Warning in %s at block ending on line %s\n" % (filename, lineno)
577 s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n"
582 s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n"
578 sys.stdout.write('\n\n>>>' + ('-' * 73))
583 s += ('-' * 76) + '\n'
579 sys.stdout.write(s)
584 s += warnings.formatwarning(w.message, w.category,
580 sys.stdout.write(('-' * 76) + '\n')
585 w.filename, w.lineno, w.line)
581 s=warnings.formatwarning(w.message, w.category,
586 s += '<<<' + ('-' * 73)
582 w.filename, w.lineno, w.line)
587 logger.warning(s)
583 sys.stdout.write(s)
584 sys.stdout.write('<<<' + ('-' * 73) + '\n')
585 if self.warning_is_error:
588 if self.warning_is_error:
586 raise RuntimeError('Non Expected warning in `{}` line {}'.format(filename, lineno))
589 raise RuntimeError('Non Expected warning in `{}` line {}'.format(filename, lineno))
587
590
@@ -1002,6 +1005,9 b' class IPythonDirective(Directive):'
1002 lines = ['.. code-block:: ipython', '']
1005 lines = ['.. code-block:: ipython', '']
1003 figures = []
1006 figures = []
1004
1007
1008 # Use sphinx logger for warnings
1009 logger = logging.getLogger(__name__)
1010
1005 for part in parts:
1011 for part in parts:
1006 block = block_parser(part, rgxin, rgxout, promptin, promptout)
1012 block = block_parser(part, rgxin, rgxout, promptin, promptout)
1007 if len(block):
1013 if len(block):
@@ -1020,7 +1026,7 b' class IPythonDirective(Directive):'
1020 if self.shell.warning_is_error:
1026 if self.shell.warning_is_error:
1021 raise RuntimeError(message)
1027 raise RuntimeError(message)
1022 else:
1028 else:
1023 warnings.warn(message)
1029 logger.warning(message)
1024
1030
1025 for figure in figures:
1031 for figure in figures:
1026 lines.append('')
1032 lines.append('')
General Comments 0
You need to be logged in to leave comments. Login now