Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b61448c

Browse files
committed
Make plot_directive use a custom PlotWarning category.
svn path=/trunk/matplotlib/; revision=8110
1 parent 6b1c845 commit b61448c

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2010-02-03 Made plot_directive use a custom PlotWarning category, so that
2+
warnings can be turned into fatal errors easily if desired. - FP
3+
14
2010-01-29 Added draggable method to Legend to allow mouse drag
25
placement. Thanks Adam Fraser. JDH
36

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@
4444
4545
The set of file formats to generate can be specified with the
4646
`plot_formats` configuration variable.
47+
48+
49+
Error handling:
50+
51+
Any errors generated during the running of the code are emitted as warnings
52+
using the Python `warnings` module, using a custom category called
53+
`PlotWarning`. To turn the warnings into fatal errors that stop the
54+
documentation build, after adjusting your `sys.path` in your `conf.py` Sphinx
55+
configuration file, use::
56+
57+
import plot_directive
58+
warnings.simplefilter('error', plot_directive.PlotWarning)
4759
"""
4860

4961
import sys, os, shutil, imp, warnings, cStringIO, re
@@ -76,6 +88,21 @@
7688
from matplotlib import _pylab_helpers
7789
from matplotlib.sphinxext import only_directives
7890

91+
92+
class PlotWarning(Warning):
93+
"""Warning category for all warnings generated by this directive.
94+
95+
By printing our warnings with this category, it becomes possible to turn
96+
them into errors by using in your conf.py::
97+
98+
warnings.simplefilter('error', plot_directive.PlotWarning)
99+
100+
This way, you can ensure that your docs only build if all your examples
101+
actually run successfully.
102+
"""
103+
pass
104+
105+
79106
# os.path.relpath is new in Python 2.6
80107
if hasattr(os.path, 'relpath'):
81108
relpath = os.path.relpath
@@ -208,7 +235,7 @@ def run_savefig(plot_path, basename, tmpdir, destdir, formats):
208235
figman.canvas.figure.savefig(outpath, dpi=dpi)
209236
except:
210237
s = cbook.exception_to_str("Exception saving plot %s" % plot_path)
211-
warnings.warn(s)
238+
warnings.warn(s, PlotWarning)
212239
return 0
213240
if j > 0:
214241
shutil.copyfile(outpath, os.path.join(destdir, outname))
@@ -270,7 +297,7 @@ def render_figures(plot_path, function_name, plot_code, tmpdir, destdir,
270297
run_code(plot_path, function_name, plot_code)
271298
except:
272299
s = cbook.exception_to_str("Exception running plot %s" % plot_path)
273-
warnings.warn(s)
300+
warnings.warn(s, PlotWarning)
274301
return 0
275302

276303
num_figs = run_savefig(plot_path, basename, tmpdir, destdir, formats)

0 commit comments

Comments
 (0)