[vtk-developers] RFC: PATCH for exception handling in vtkWrapPython.c

Charl P. Botha c.p.botha at ewi.tudelft.nl
Mon Mar 1 09:02:23 EST 2004

Dear developers,

Something which has become acutely obvious especially with ITK/VTK
integration in Python is the fact that the Python VTK wrappings do not
handle exceptions.  For example, if I make the following pipeline in Python:

VTK reader -> ConnectVTKITK -> ITK processing -> ConnectITKVTK -> VTK
visualisation (see Insight/Examples/Visualization/Canny*.py for a more
concrete example)

and I call Update() on the last VTK object in the pipeline, and any of the
ITK objects flag an error (ITK does this by throwing an exception), my whole
application is unceremoniously dumped to the command-line with no error
message whatsoever.  That's just rude. :)

This happens because there is no exception handling in the VTK Python

In the light of this, I have modified vtkWrapPython() with try...catch
blocks around ALL wrapped code method invocations.  Any exception of class
std::exception is caught and passed through to Python as a RuntimeError.
Anything else is rethrown.  This means that if *any* C++ code throws an
exception-derived exception and this code was executed as a result of a
wrapped VTK Python call, Python will see the exception and the program won't
be unceremoniously dumped.

Please check and comment on the attached patch.  My only remaning problem is
the "#include <exception>" I had to add to all Python wrapped code.  If
anyone could give any advice about the portability of the <exception>
include on all platforms that VTK builds on, I would be grateful.

I would like to apply this patch at some stage.  It could obviously also be
made conditional.


charl p. botha http://cpbotha.net/ http://visualisation.tudelft.nl/
-------------- next part --------------
Index: vtkWrapPython.c
RCS file: /vtk/cvsroot/VTK/Wrapping/vtkWrapPython.c,v
retrieving revision 1.69
diff -u -r1.69 vtkWrapPython.c
--- vtkWrapPython.c	17 Feb 2004 15:31:24 -0000	1.69
+++ vtkWrapPython.c	1 Mar 2004 13:07:14 -0000
@@ -735,17 +735,20 @@
               fprintf(fp,"    else {\n");
+            /* start of try around method invocation */
+            fprintf(fp, "     try {\n");
             switch (currentFunction->ReturnType%1000)
               case 2:
-                fprintf(fp,"      %s(",methodname);
+                fprintf(fp,"        %s(",methodname);
               case 109:
-                fprintf(fp,"      temp%i = &%s(",MAX_ARGS,methodname);
+                fprintf(fp,"        temp%i = &%s(",MAX_ARGS,methodname);
-                fprintf(fp,"      temp%i = %s(",MAX_ARGS,methodname);
+                fprintf(fp,"        temp%i = %s(",MAX_ARGS,methodname);
             for (i = 0; i < currentFunction->NumberOfArguments; i++)
@@ -769,6 +772,16 @@
+            /* catch clause for any possible exceptions */
+            fprintf(fp, "     }\n");
+            fprintf(fp, "     catch (std::exception &_e) {\n");
+            fprintf(fp, "       PyErr_SetString(PyExc_RuntimeError, _e.what());\n");
+            fprintf(fp, "       return NULL;\n");
+            fprintf(fp, "     }\n");
+            fprintf(fp, "     catch (...) {\n");
+            fprintf(fp, "       throw;\n");
+            fprintf(fp, "     }\n");
             if (currentFunction->NumberOfArguments == 1 
                 && currentFunction->ArgTypes[0] == 5000)
@@ -1037,6 +1050,7 @@
   #if defined(__APPLE__)
   fprintf(fp,"#include \"vtkPythonUtil.h\"\n");
+  fprintf(fp,"#include <exception>\n");
   fprintf(fp,"#if defined(WIN32)\n");
   fprintf(fp,"extern \"C\" { __declspec( dllexport ) PyObject *PyVTKClass_%sNew(char *); }\n",

More information about the vtk-developers mailing list