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 | |
@@ -557,15 +558,20 b' class EmbeddedSphinxShell(object):' | |||||
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 | |
559 |
|
560 | |||
|
561 | # Use sphinx logger for warnings | |||
|
562 | logger = logging.getLogger(__name__) | |||
|
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 ( | |
563 | s = "\nException in %s at block ending on line %s\n" % (filename, lineno) |
|
567 | ("Traceback" in processed_output) or ("SyntaxError" in processed_output) | |
|
568 | ): | |||
|
569 | s = "\n>>>" + ("-" * 73) + "\n" | |||
|
570 | 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" |
|
571 | s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n" | |
565 | sys.stdout.write('\n\n>>>' + ('-' * 73)) |
|
572 | s += processed_output + "\n" | |
566 | sys.stdout.write(s) |
|
573 | s += "<<<" + ("-" * 73) | |
567 | sys.stdout.write(processed_output) |
|
574 | logger.warning(s) | |
568 | sys.stdout.write('<<<' + ('-' * 73) + '\n\n') |
|
|||
569 | if self.warning_is_error: |
|
575 | if self.warning_is_error: | |
570 | raise RuntimeError('Non Expected exception in `{}` line {}'.format(filename, lineno)) |
|
576 | raise RuntimeError('Non Expected exception in `{}` line {}'.format(filename, lineno)) | |
571 |
|
577 | |||
@@ -573,15 +579,15 b' class EmbeddedSphinxShell(object):' | |||||
573 | # unless :okwarning: has been specified. |
|
579 | # unless :okwarning: has been specified. | |
574 | if not is_okwarning: |
|
580 | if not is_okwarning: | |
575 | for w in ws: |
|
581 | for w in ws: | |
576 | s = "\nWarning in %s at block ending on line %s\n" % (filename, lineno) |
|
582 | s = "\n>>>" + ("-" * 73) + "\n" | |
|
583 | 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" |
|
584 | s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n" | |
578 | sys.stdout.write('\n\n>>>' + ('-' * 73)) |
|
585 | s += ("-" * 76) + "\n" | |
579 |
s |
|
586 | s += warnings.formatwarning( | |
580 | sys.stdout.write(('-' * 76) + '\n') |
|
587 | w.message, w.category, w.filename, w.lineno, w.line | |
581 | s=warnings.formatwarning(w.message, w.category, |
|
588 | ) | |
582 | w.filename, w.lineno, w.line) |
|
589 | s += "<<<" + ("-" * 73) | |
583 |
|
|
590 | logger.warning(s) | |
584 | sys.stdout.write('<<<' + ('-' * 73) + '\n') |
|
|||
585 | if self.warning_is_error: |
|
591 | if self.warning_is_error: | |
586 | raise RuntimeError('Non Expected warning in `{}` line {}'.format(filename, lineno)) |
|
592 | raise RuntimeError('Non Expected warning in `{}` line {}'.format(filename, lineno)) | |
587 |
|
593 | |||
@@ -1002,6 +1008,9 b' class IPythonDirective(Directive):' | |||||
1002 | lines = ['.. code-block:: ipython', ''] |
|
1008 | lines = ['.. code-block:: ipython', ''] | |
1003 | figures = [] |
|
1009 | figures = [] | |
1004 |
|
1010 | |||
|
1011 | # Use sphinx logger for warnings | |||
|
1012 | logger = logging.getLogger(__name__) | |||
|
1013 | ||||
1005 | for part in parts: |
|
1014 | for part in parts: | |
1006 | block = block_parser(part, rgxin, rgxout, promptin, promptout) |
|
1015 | block = block_parser(part, rgxin, rgxout, promptin, promptout) | |
1007 | if len(block): |
|
1016 | if len(block): | |
@@ -1020,7 +1029,7 b' class IPythonDirective(Directive):' | |||||
1020 | if self.shell.warning_is_error: |
|
1029 | if self.shell.warning_is_error: | |
1021 | raise RuntimeError(message) |
|
1030 | raise RuntimeError(message) | |
1022 | else: |
|
1031 | else: | |
1023 |
|
|
1032 | logger.warning(message) | |
1024 |
|
1033 | |||
1025 | for figure in figures: |
|
1034 | for figure in figures: | |
1026 | lines.append('') |
|
1035 | lines.append('') |
General Comments 0
You need to be logged in to leave comments.
Login now