[vtkusers] python exceptions patch needed for vtk 5.6

Lic. José M. Rodriguez Bacallao jmrbcu at gmail.com
Tue Jun 1 11:33:36 EDT 2010


well, I can confirm that Charl's patch work very well. The only thing
to worry is from raising an exception within an observer in python but
for me this is not a major problem. Right now, I am using this patch
with vtk 5.4.2 because is nice to see python exceptions and for my
threading issues (the last is a big issue for me), this is the only
patch I have seen for vtk python bindings that address the GIL issue.
I have tested this patch in linux and soon will be doing the same with
windows because my application must support windows/linux and possible
MacOSX.

David, I like your way of catching errors but ins this code:

==== 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 ====

where do you use self.CallDataType?

On Sun, May 30, 2010 at 9:03 PM, David Gobbi <david.gobbi at gmail.com> wrote:
> Hi Florian,
>
> Here is an 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 ====
>
>
>
> On Sun, May 30, 2010 at 3:47 AM, Florian Bruckner <e0425375 at gmail.com> wrote:
>> OK, for me this means that I will use vtk observers, since I need a portable
>> solution that runs on a default vtk installation.
>>
>> @gobbi: could you please send me a short example, of how you use these
>> observers in python?
>>
>> nevertheless it would be nice to see this patch in some future vtk releases.
>> If the only problem are these unreachable code warnings in windows, why
>> can't you simply add some helper-functions that execute this otherwise
>> unreachable code? Since I only use Linux I will not be able to test it for
>> myself, but I can't imagine that none of the windows programmers could find
>> a solution for this. perhaps you should give it another try and repost the
>> patch to the developers list?
>>
>> thanks to all for your help
>> I wish you what
>> FloB
>>
>>
>> On Sat, May 29, 2010 at 4:36 AM, David Gobbi <david.gobbi at gmail.com> wrote:
>>>
>>> Hi Charl,
>>>
>>> I've looked over the patch and I have one question.  What happens when
>>> an python exception is thrown in a python observer callback?  I didn't
>>> see any modifications to vtkPythonCommand, so I'm guessing that the
>>> error will just be printed (like it is now) and not fed back to the
>>> original caller.
>>>
>>>   David
>>>
>>>
>>> On Fri, May 28, 2010 at 4:43 PM, Charl Botha <c.p.botha at tudelft.nl> wrote:
>>> > Dear Florian and David,
>>> >
>>> > On 28 May 2010 21:07, Florian Bruckner <e0425375 at gmail.com> wrote:
>>> >>
>>> >> is there a reason why the patch was not merged into the official vtk
>>> >> release?
>>> >>
>>> >> i'm just wondering because that patch is fairly old and for me this
>>> >> seems to be a very good solution for a general problem.
>>> >
>>> > The patch has a permanent home here:
>>> >
>>> > http://code.google.com/p/devide/source/browse/trunk/johannes/patches/pyvtk_tryexcept_and_pyexceptions.diff
>>> >
>>> > Of course I think it's a good patch. :) It does the following:
>>> > 1. Turns all VTK errors into Python exceptions.
>>> > 2. Carefully releases the GIL during all calls into VTK (grabbing it
>>> > back for observers) so that you can successfully explicitly
>>> > multi-thread VTK pipelines from Python.
>>> > 3. Tries to trap bad_alloc.
>>> >
>>> > I've been working on this for years, but due to Windows unreachable
>>> > code warnings that I have not been able to solve, I've never committed
>>> > this to VTK (it works, just gives many warnings during compilation).
>>> > My own binaries all have this patch integrated (DeVIDE [1] and VTK
>>> > [2]).
>>> >
>>> > The current version is for 5.4.2, I'd like to update for 5.6 as soon
>>> > as I can figure out how to check out a clean 5.6 with git (until very
>>> > recently there was no tag yet, I see that it's there now!
>>> > http://vtk.org/gitweb?p=VTK.git;a=summary )
>>> >
>>> > I'm also considering to make a wiki page on devide.googlecode.com that
>>> > documents the patch.
>>> >
>>> > See you,
>>> > Charl
>>> >
>>> > [1] http://graphics.tudelft.nl/Projects/DeVIDE
>>> > [2] http://cpbotha.net/software/latest-vtk-windows-binaries/
>>> >
>>> >
>>> >>
>>> >> On Fri, May 28, 2010 at 4:07 PM, David Gobbi <david.gobbi at gmail.com>
>>> >> wrote:
>>> >>>
>>> >>> Hi Florian,
>>> >>>
>>> >>> I haven't seen mention of the patch here on the list, and I know
>>> >>> that it isn't in VTK 5.6 or in VTK devel.  For my own apps, I use
>>> >>> VTK observers to catch reader errors.
>>> >>>
>>> >>>   David
>>> >>>
>>> >>>
>>> >>> On Fri, May 28, 2010 at 12:52 AM, Florian Bruckner
>>> >>> <e0425375 at gmail.com> wrote:
>>> >>> > hi,
>>> >>> >
>>> >>> > I just figured out the problem that no python exceptions are raised
>>> >>> > if
>>> >>> > my reader produces an error. I use vtk-5.6.
>>> >>> >
>>> >>> > after searching the net and the mailing list I found that there are
>>> >>> > some
>>> >>> > patches available
>>> >>> > (turn-vtk-errors-into-python-exceptions-and-catch-bad-malloc patch)
>>> >>> > which should solve this problem. Unfortunately I could neither find
>>> >>> > such
>>> >>> > a patch for vtk-5.6 nor any actual posts about this topic.
>>> >>> >
>>> >>> > So should this patch allready be included in the new vtk-5.6? or
>>> >>> > where
>>> >>> > can I get it otherwise? are there any reasons why the patch is not
>>> >>> > included in the official release? or is there another work around
>>> >>> > that
>>> >>> > is proposed for this kind of problems?
>>> >>> >
>>> >>> > thanks
>>> >>> > FloB
>>> >>
>>> >>
>>> >> _______________________________________________
>>> >> 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
>>> >>
>>> >
>>> >
>>> >
>>> > --
>>> > dr. charl p. botha :: http://graphics.tudelft.nl/CharlBotha
>>> > work: cpbotha at medvis.org personal: cpbotha at cpbotha.net
>>> >
>>> > See our latest papers on shape spaces, brain connectivity, shoulder
>>> > segmentation, human motion analysis, voxel classification and eye fat
>>> > deformation! http://graphics.tudelft.nl/Publications/2010
>>> >
>>
>>
> _______________________________________________
> 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
>



-- 
Lic. José M. Rodriguez Bacallao
Centro de Biofisica Medica
-----------------------------------------------------------------
Todos somos muy ignorantes, lo que ocurre es que no todos ignoramos lo mismo.

Recuerda: El arca de Noe fue construida por aficionados, el titanic
por profesionales
-----------------------------------------------------------------



More information about the vtkusers mailing list