[vtk-developers] Safer item access in Python wrapped VTK arrays

Andras Lasso lasso at queensu.ca
Fri Aug 4 13:11:52 EDT 2017


Thank you, it would be awesome to have this (and the other proposed hints, too).

Andras

From: David Gobbi [mailto:david.gobbi at gmail.com]
Sent: Friday, August 4, 2017 1:06 PM
To: Andras Lasso <lasso at queensu.ca>
Cc: VTK Developers <vtk-developers at vtk.org>
Subject: Re: Safer item access in Python wrapped VTK arrays

Hi Andras,

Yes, this would be possible.  It could be hard-coded into the wrappers, or even better, in the header file a generic hint could be added to the index parameter so that the index would be checked against the size of the array:

  float GetValue(vtkIdType idx VTK_RANGECHECK(0,GetNumberOfValues()));

I have a list of wrapper hints at http://www.vtk.org/Wiki/VTK/Wrapping_hints<https://na01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.vtk.org%2FWiki%2FVTK%2FWrapping_hints&data=02%7C01%7Classo%40queensu.ca%7C6b328e9098d3473fb04408d4db5b2a7f%7Cd61ecb3b38b142d582c4efb2838b925c%7C1%7C0%7C636374631984021366&sdata=zFV%2By8tHBbAW%2FZbScngLl1qPfsgemLkAxVfPsKRPNZI%3D&reserved=0>, and I can add this "RANGECHECK" hint to the "Proposed hints" section.

I'm hoping to find time to implement more of these wrapper hints over the next few weeks.

 - David


On Fri, Aug 4, 2017 at 9:53 AM, Andras Lasso <lasso at queensu.ca<mailto:lasso at queensu.ca>> wrote:
Hi David,

Users who mostly use VTK via Python wrapping are sometimes surprised how easily they can crash the application by accessing out-of-bounds elements in arrays. For example, this crashes an application:

>>> a=vtk.vtkStringArray()
>>> print(a.GetValue(0))

I first thought that it should be handled by documentation and training, but it would be nicer if the wrappers could handle this.

Would it be feasible to add bounds check to VTK array accessors in Python wrappers, so that in case of an out of bounds access, a Python exception would be raised instead of crashing the application?
Andras

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20170804/a7db18b0/attachment-0001.html>


More information about the vtk-developers mailing list