[vtkusers] VTK: Python error messages vs. exceptions

David Gobbi david.gobbi at gmail.com
Fri Jun 1 15:52:05 EDT 2012


Hi Nico,

The python wrappers are intended to follow the behavior of the
underlying VTK code.  VTK simply prints the error messages in C++,
and it does the same in Python.  It's possible for python to use VTK's
event observers to catch ErrorEvents, which might fulfil your needs.
I wouldn't want to make the python wrappers automatically catch error
events from all VTK objects, though, because that would add a fair bit
of extra weight (i.e. memory usage, processing time) to the wrappers.

Here is one example of how to make an error observer in python.  Most
of the code is straightforward.  The __call__ method is needed because
VTK-Python observers must be callable objects.  The only messy part is
the "CallDataType", which is needed because in C++ the VTK object
passes the error message as a "void *" instead of as a properly typed
object, so the python observer needs a hint that the "void *" is a
"char *".

==== python code ====
import vtk

class ErrorObserver:

   def __init__(self):
       self.__ErrorOccurred = False
       self.__ErrorMessage = None
       self.CallDataType = 'string0'

   def __call__(self, obj, event, message):
       self.__ErrorOccurred = True
       self.__ErrorMessage = message

   def ErrorOccurred(self):
       occ = self.__ErrorOccurred
       self.__ErrorOccurred = False
       return occ

   def ErrorMessage(self):
       return self.__ErrorMessage

e = ErrorObserver()

a = vtk.vtkImageReader()
a.AddObserver('ErrorEvent', e)

print "doing update"
a.Update()
if e.ErrorOccurred():
   print e.ErrorMessage()
==== python code ====

It might also be possible to re-implement the vtkOutputWindow in
Python, and use it to catch VTK errors, but I've never tried.

 - David


On Fri, Jun 1, 2012 at 1:25 PM, Nico Schlömer <nico.schloemer at gmail.com> wrote:
>
> Hi all,
>
> when using VTK's Python interface, I often notice that if things don't
> go as expected, e.g., when vtkXMLUnstructuredFileReader is instructed
> to read a nonexistent file, the code doesn't raise exceptions, but
> prints error messages to the screen and continues as normal.
>
> Try, for example:
>
> from vtk import vtkXMLUnstructuredGridReader
> reader = vtkXMLUnstructuredGridReader()
> reader.SetFileName( 'mynonexistentfile.vtu' )
> reader.Update()
>
> The output will be
>
> ================ *snip* ================
> ERROR: In /build/buildd/vtk-5.8.0/IO/vtkXMLReader.cxx, line 219
> vtkXMLUnstructuredGridReader (0x3344e90): Error opening file
> mynonexistentfile.vtu
>
> ERROR: In /build/buildd/vtk-5.8.0/Filtering/vtkExecutive.cxx, line 756
> vtkStreamingDemandDrivenPipeline (0x33478d0): Algorithm
> vtkXMLUnstructuredGridReader(0x3344e90) returned failure for request:
> vtkInformation (0x334e000)
>  Debug: Off
>  Modified Time: 69
>  Reference Count: 1
>  Registered Events: (none)
>  Request: REQUEST_INFORMATION
>  FORWARD_DIRECTION: 0
>  ALGORITHM_AFTER_FORWARD: 1
> ================ *snap* ================
>
> Due to this behavior, I get exceptions messages at the weirdest places
> in my code (namely where the grid data is tried to be used).
>
> Question: Is there any way to have VTK raise RuntimeError()s (or
> similar) instead of just printing a message to the screen?
> Alternatively, would there be any way to determine if an operation
> went alright? (So far I haven't found return codes or similar.)
>
> Cheers,
> Nico
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers



More information about the vtkusers mailing list