Skip to content
Open
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
13 changes: 11 additions & 2 deletions lib/matplotlib/sphinxext/plot_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(record=True) 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 = {}
Expand All @@ -609,6 +611,13 @@ 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


Expand Down
Loading