[vtkusers] Re: [vtk-developers] patch for turning (almost) all VTK errors into Python exceptions, IMPROVED

David Gobbi dgobbi at atamai.com
Tue Aug 8 10:12:10 EDT 2006


Charl P. Botha wrote:
> Dear all,
>
> On 8/5/06, Prabhu Ramachandran <prabhu_r at users.sf.net> wrote:
>> >>>>> "Charl" == Charl P Botha <cpbotha at cpbotha.net> writes:
>>
>>     Charl> Hi there (Python)-gang, I've made a patch for VTK 5.0 which
>>     Charl> will turn almost all VTK errors into Python exceptions.  It
>>     Charl> works around the exception non-safety of VTK.  Have a look
>>     Charl> here:
>>
>>     Charl> 
>> http://visualisation.tudelft.nl/~cpbotha/thingies/python_exceptions_vtk5.0.diff 
>>
>>
>> [...]
>>     Charl> What do VTK/Python people think?  This definitely fills a
>>     Charl> niche, and it could be conditionally included in VTK.  The
>>     Charl> only drawback is the extra PyErr_Occurred() call after
>>     Charl> every VTK method call, and of course the extra line or two
>>     Charl> of Python code to set up the new vtkEventOutputWindow).
>>
>> This is nice, thanks.  However, does this work for errors generated by
>> VTK objects that were not instantiated in Python?  Do exceptions
>> "leak" in this case?  From what I can tell of the patch, the answer is
>> yes.  If so is this a problem, and how big is it?
>
> I've made a new and improved solution, see here:
> http://visualisation.tudelft.nl/~cpbotha/thingies/python_exceptions_vtk5.0_owsol.diff 
>
>
> This is much simpler than the previous attempt, but works better.  I
> made a vtkPythonOutputWindow that turns Errors and Warnings into
> RuntimeError and RuntimeWarning Python exceptions, and outputs Text
> and DebugText using the Python print command.  vtkPythonUtil sets this
> OutputWindow as the singleton instance.
>
> The wrapping code still has the check for PyErr_Occurred(), as this is
> necessary as the final step of turning the error into an exception.
>
> With this new solution, when VTK is being used from Python, ALL VTK
> objects, directly created in Python or not, will generate Python
> exceptions when they make use of any of the VTK error or warning
> macros.  In addition, one can revert to the old behaviour with a
> Python one-liner.
>
> This one is definitely commitable to VTK HEAD.
>
> Any comments?

I took a look at the patch, I would say that this is definitely the best 
solution.

My only question is whether raising a python exception is going to cause
a vtkInteractor-based application to halt when you really only want it to
print the error and continue.  Let me expand on this a bit:

If you've ever written an application with Tkinter, PyQt or wxPython you've
noticed that when a python exception occurs, the exception is printed 
but then
the app returns to its main loop and continues.

I just want to make sure that an app that uses VTK's built-in main loop 
(i.e. that
doesn't use a separate GUI toolkit) will also return to the main loop 
after an
exception.

 - David



More information about the vtkusers mailing list