Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/matplotlib/sphinxext/plot_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@

from sphinx.environment.collectors import EnvironmentCollector
from sphinx.errors import ExtensionError
from sphinx.util import logging

import matplotlib
from matplotlib.backend_bases import FigureManagerBase
Expand All @@ -213,6 +214,8 @@

__version__ = 2

_log = logging.getLogger(__name__)


# -----------------------------------------------------------------------------
# Registration hook
Expand Down Expand Up @@ -947,11 +950,11 @@ def run(arguments, content, options, state_machine, state, lineno):
code_includes=source_file_includes)
errors = []
except PlotError as err:
message = "Exception occurred in plotting {}\n from {}:\n{}".format(
output_base, source_file_name, err)
_log.warning(message, location=(source_file_name, lineno))
reporter = state.memo.reporter
sm = reporter.system_message(
2, "Exception occurred in plotting {}\n from {}:\n{}".format(
output_base, source_file_name, err),
line=lineno)
sm = reporter.system_message(2, message, line=lineno)
results = [(code, [])]
errors = [sm]

Expand Down
30 changes: 25 additions & 5 deletions lib/matplotlib/tests/test_sphinxext.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
tinypages = Path(__file__).parent / 'data/tinypages'


def build_sphinx_html(source_dir, doctree_dir, html_dir, extra_args=None):
# Build the pages with warnings turned into errors
def build_sphinx_html(
source_dir, doctree_dir, html_dir, extra_args=None, expected_returncode=0):
# Build the pages with warnings turned into errors.
extra_args = [] if extra_args is None else extra_args
cmd = [sys.executable, '-msphinx', '-W', '-b', 'html',
'-d', str(doctree_dir), str(source_dir), str(html_dir), *extra_args]
Expand All @@ -31,12 +32,31 @@ def build_sphinx_html(source_dir, doctree_dir, html_dir, extra_args=None):
out = proc.stdout
err = proc.stderr

assert proc.returncode == 0, \
assert proc.returncode == expected_returncode, \
f"sphinx build failed with stdout:\n{out}\nstderr:\n{err}\n"
if err:
if expected_returncode == 0 and err:
pytest.fail(f"sphinx build emitted the following warnings:\n{err}")

assert html_dir.is_dir()
if expected_returncode == 0:
assert html_dir.is_dir()
return proc


def test_plot_directive_exception_fails_build(tmp_path):
shutil.copyfile(tinypages / 'conf.py', tmp_path / 'conf.py')
shutil.copytree(tinypages / '_static', tmp_path / '_static')
(tmp_path / 'index.rst').write_text("""
.. plot::

raise RuntimeError("plot directive failure")
""")

proc = build_sphinx_html(
tmp_path, tmp_path / 'doctrees', tmp_path / '_build' / 'html',
expected_returncode=1)

assert "Exception occurred in plotting index-1" in proc.stderr
assert "RuntimeError: plot directive failure" in proc.stderr


def test_tinypages(tmp_path):
Expand Down
Loading