Skip to content

Commit

Permalink
Merge pull request #13047 from meeseeksmachine/auto-backport-of-pr-13…
Browse files Browse the repository at this point in the history
…030-on-7.x

Backport PR #13030 on branch 7.x (Use Sphinx logging in IPython directive)
  • Loading branch information
Carreau committed Jul 8, 2021
2 parents ff3e835 + 17a8fe1 commit 9c34b61
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
39 changes: 24 additions & 15 deletions IPython/sphinxext/ipython_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
# Third-party
from docutils.parsers.rst import directives
from docutils.parsers.rst import Directive
from sphinx.util import logging

# Our own
from traitlets.config import Config
Expand Down Expand Up @@ -557,31 +558,36 @@ def process_input(self, data, input_prompt, lineno):
filename = self.directive.state.document.current_source
lineno = self.directive.state.document.current_line

# Use sphinx logger for warnings
logger = logging.getLogger(__name__)

# output any exceptions raised during execution to stdout
# unless :okexcept: has been specified.
if not is_okexcept and (("Traceback" in processed_output) or ("SyntaxError" in processed_output)):
s = "\nException in %s at block ending on line %s\n" % (filename, lineno)
if not is_okexcept and (
("Traceback" in processed_output) or ("SyntaxError" in processed_output)
):
s = "\n>>>" + ("-" * 73) + "\n"
s += "Exception in %s at block ending on line %s\n" % (filename, lineno)
s += "Specify :okexcept: as an option in the ipython:: block to suppress this message\n"
sys.stdout.write('\n\n>>>' + ('-' * 73))
sys.stdout.write(s)
sys.stdout.write(processed_output)
sys.stdout.write('<<<' + ('-' * 73) + '\n\n')
s += processed_output + "\n"
s += "<<<" + ("-" * 73)
logger.warning(s)
if self.warning_is_error:
raise RuntimeError('Non Expected exception in `{}` line {}'.format(filename, lineno))

# output any warning raised during execution to stdout
# unless :okwarning: has been specified.
if not is_okwarning:
for w in ws:
s = "\nWarning in %s at block ending on line %s\n" % (filename, lineno)
s = "\n>>>" + ("-" * 73) + "\n"
s += "Warning in %s at block ending on line %s\n" % (filename, lineno)
s += "Specify :okwarning: as an option in the ipython:: block to suppress this message\n"
sys.stdout.write('\n\n>>>' + ('-' * 73))
sys.stdout.write(s)
sys.stdout.write(('-' * 76) + '\n')
s=warnings.formatwarning(w.message, w.category,
w.filename, w.lineno, w.line)
sys.stdout.write(s)
sys.stdout.write('<<<' + ('-' * 73) + '\n')
s += ("-" * 76) + "\n"
s += warnings.formatwarning(
w.message, w.category, w.filename, w.lineno, w.line
)
s += "<<<" + ("-" * 73)
logger.warning(s)
if self.warning_is_error:
raise RuntimeError('Non Expected warning in `{}` line {}'.format(filename, lineno))

Expand Down Expand Up @@ -1002,6 +1008,9 @@ def run(self):
lines = ['.. code-block:: ipython', '']
figures = []

# Use sphinx logger for warnings
logger = logging.getLogger(__name__)

for part in parts:
block = block_parser(part, rgxin, rgxout, promptin, promptout)
if len(block):
Expand All @@ -1020,7 +1029,7 @@ def run(self):
if self.shell.warning_is_error:
raise RuntimeError(message)
else:
warnings.warn(message)
logger.warning(message)

for figure in figures:
lines.append('')
Expand Down
4 changes: 4 additions & 0 deletions docs/source/whatsnew/pr/ipython_directive_warnings.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
IPython directive warnings
--------------------------

The IPython directive now uses Sphinx logging for warnings.

0 comments on commit 9c34b61

Please sign in to comment.