Hi all,

would anyone mind if I commit the attached patch?

It's 100% backwards compatible and allows for turning plot directive
errors into fatal exceptions easily, so one can make sure that docs
either build correctly or not at all.  This is useful for having
examples be a kind of test suite, in addition to any unit tests a
codebase may have.

Thanks,

f
Index: lib/matplotlib/sphinxext/plot_directive.py
===================================================================
--- lib/matplotlib/sphinxext/plot_directive.py	(revision 8097)
+++ lib/matplotlib/sphinxext/plot_directive.py	(working copy)
@@ -76,6 +76,21 @@
 from matplotlib import _pylab_helpers
 from matplotlib.sphinxext import only_directives
 
+
+class PlotWarning(Warning):
+    """Warning category for all warnings generated by this directive.
+
+    By printing our warnings with this category, it becomes possible to turn
+    them into errors by using in your conf.py::
+
+      warnings.simplefilter('error', plot_directive.PlotWarning)
+
+    This way, you can ensure that your docs only build if all your examples
+    actually run successfully.
+    """
+    pass
+
+
 # os.path.relpath is new in Python 2.6
 if hasattr(os.path, 'relpath'):
     relpath = os.path.relpath
@@ -208,7 +223,7 @@
                 figman.canvas.figure.savefig(outpath, dpi=dpi)
             except:
                 s = cbook.exception_to_str("Exception saving plot %s" % plot_path)
-                warnings.warn(s)
+                warnings.warn(s, PlotWarning)
                 return 0
             if j > 0:
                 shutil.copyfile(outpath, os.path.join(destdir, outname))
@@ -270,7 +285,7 @@
         run_code(plot_path, function_name, plot_code)
     except:
         s = cbook.exception_to_str("Exception running plot %s" % plot_path)
-        warnings.warn(s)
+        warnings.warn(s, PlotWarning)
         return 0
 
     num_figs = run_savefig(plot_path, basename, tmpdir, destdir, formats)
------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to