From b51e6d110010d522e59bc800f3401f0f9ef51429 Mon Sep 17 00:00:00 2001 From: Boyu Dai Date: Sun, 4 Jan 2026 18:48:24 +1100 Subject: [PATCH 1/3] Log warning during ..plot to sphinx logger --- lib/matplotlib/sphinxext/plot_directive.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/sphinxext/plot_directive.py b/lib/matplotlib/sphinxext/plot_directive.py index dbf5072e2c93..b56f3879b166 100644 --- a/lib/matplotlib/sphinxext/plot_directive.py +++ b/lib/matplotlib/sphinxext/plot_directive.py @@ -196,6 +196,7 @@ import sys import textwrap import traceback +import warnings from docutils.parsers.rst import directives, Directive from docutils.parsers.rst.directives.images import Image @@ -584,9 +585,10 @@ def _run_code(code, code_path, ns=None, function_name=None): dirname = os.path.abspath(os.path.dirname(code_path)) os.chdir(dirname) - with cbook._setattr_cm( + with (warnings.catch_warnings() as caught_warnings, + cbook._setattr_cm( sys, argv=[code_path], path=[os.getcwd(), *sys.path]), \ - contextlib.redirect_stdout(StringIO()): + contextlib.redirect_stdout(StringIO())): try: if ns is None: ns = {} @@ -609,6 +611,12 @@ def _run_code(code, code_path, ns=None, function_name=None): raise PlotError(traceback.format_exc()) from err finally: os.chdir(pwd) + for warn in caught_warnings: + _log.warning( + "[plot] Python warning during plot execution: %s (%s)", + warn.message, + warn.category.__name__ + ) return ns From bf7a2debd8c45403acc4e7e8d2ab77323811c50b Mon Sep 17 00:00:00 2001 From: Boyu Dai Date: Sun, 4 Jan 2026 19:24:38 +1100 Subject: [PATCH 2/3] Correct python warning manager parameter --- lib/matplotlib/sphinxext/plot_directive.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/sphinxext/plot_directive.py b/lib/matplotlib/sphinxext/plot_directive.py index b56f3879b166..398469eec2e8 100644 --- a/lib/matplotlib/sphinxext/plot_directive.py +++ b/lib/matplotlib/sphinxext/plot_directive.py @@ -585,7 +585,7 @@ def _run_code(code, code_path, ns=None, function_name=None): dirname = os.path.abspath(os.path.dirname(code_path)) os.chdir(dirname) - with (warnings.catch_warnings() as caught_warnings, + with (warnings.catch_warnings(record=True) as caught_warnings, cbook._setattr_cm( sys, argv=[code_path], path=[os.getcwd(), *sys.path]), \ contextlib.redirect_stdout(StringIO())): @@ -611,6 +611,7 @@ def _run_code(code, code_path, ns=None, function_name=None): raise PlotError(traceback.format_exc()) from err finally: os.chdir(pwd) + for warn in caught_warnings: _log.warning( "[plot] Python warning during plot execution: %s (%s)", From 6583c55d2d24c230ab4ba188387798ea032070f0 Mon Sep 17 00:00:00 2001 From: Caleb Broman Date: Sat, 18 Jul 2026 14:08:36 -0500 Subject: [PATCH 3/3] TST: add test: warnings propagate to sphinx Co-authored-by: Glen Sanabria --- lib/matplotlib/tests/test_sphinxext.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/matplotlib/tests/test_sphinxext.py b/lib/matplotlib/tests/test_sphinxext.py index 9192cdfb906b..62b9d335c403 100644 --- a/lib/matplotlib/tests/test_sphinxext.py +++ b/lib/matplotlib/tests/test_sphinxext.py @@ -59,6 +59,24 @@ def test_plot_directive_exception_fails_build(tmp_path): assert "RuntimeError: plot directive failure" in proc.stderr +def test_plot_directive_warning_propagation(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:: + + import warnings + warnings.warn("A warning occurred") + +""") + + proc = build_sphinx_html( + tmp_path, tmp_path / 'doctrees', tmp_path / '_build' / 'html', + expected_returncode=1) + + assert "Python warning during plot execution: A warning occurred (UserWarning)" in proc.stderr + + def test_tinypages(tmp_path): shutil.copytree(tinypages, tmp_path, dirs_exist_ok=True, ignore=shutil.ignore_patterns('_build', 'doctrees',