VTK/Python Wrapper Enhancement: Difference between revisions
Line 18: | Line 18: | ||
Also: see [[VTK/WrapHierarchy]] for a tool that provides the entire hierarchy to the wrappers at compile-time | Also: see [[VTK/WrapHierarchy]] for a tool that provides the entire hierarchy to the wrappers at compile-time | ||
====Enums and constants=== | ====Enums and constants==== | ||
The new vtkParse will parse enums and #define constants. | The new vtkParse will parse enums and #define constants. |
Revision as of 15:15, 31 May 2010
This project will improve the python wrappers bit-by-bit, with the goal of making the Python interface as close as possible to the original C++ interface.
Improvements to Python wrappers
- Allow hierarchies of special types
- Enums and other constants
- Operator support
- Wrapping of templated types - will always be limited to selected types, but can still be very useful
- Wrapping of default-value arguments
- Wrapping of pointers args - requires a better hinting system
- Wrapping of reference args for returning values - would be easy
Hierarchies of special types in Python wrappers
If each special type had its own PyTypeObject struct (to be generated by vtkWrapPython.c) then:
- Types could have a hierarchy via python's subclass system
- Type-specific protocols (number, sequence, buffer, etc) could be supported, this would require proper parsing of operators
Also: see VTK/WrapHierarchy for a tool that provides the entire hierarchy to the wrappers at compile-time
Enums and constants
The new vtkParse will parse enums and #define constants.
- The FileInfo struct must be expanded to hold these constants
- Constant values should be stored as strings to simplify typing*
- The wrappers will have to automatically add the class scope to enum constants.
* The strings can be written literally into the wrapper .cxx files where they will be evaluated.
Operator support
The new vtkParse provides information about operator methods for the VTK classes. These operator methods can be mirrored in Python by:
- defining the appropriate "protocols" for special type objects
- defining the proper methods e.g. __setitem__() for vtkObjectBase objects
Each VTK special type will have its own python "type" object, and can thus support its own set of protocols which will automatically be inherited by "subtypes". All vtkObjectBase objects have the same python "type" object so protocols cannot be used, the less-efficient technique of using underscore methods can be applied.
Templated type handling in Python
Should be made to look similar to numpy, e.g. vtkValue(1, 'f') would create a vtkValue<float>. To python, the templated type would look like a variadic type. It would be necessary to change vtkParse so that it recognized templates.
Default value arguments
Default argument value support would require the following:
- vtkParse must store the default value in the FunctionInfo struct as a string*
- vtkWrapPython.c must use these default values to initialize parameter values
- vtkWrapPython.c must place a bar "|" before the default args in the ParseTuple format string
- some other small changes would be needed
* the default value must be stored as a string to accommodate all types and to accommodate simple mathematical expressions, e.g. the default might be SomeMethod(int param = VTK_CONST1 - VTK_CONST2). The string "VTK_CONST1 - VTK_CONST2" can be dropped directly into the wrapper CXX code where it will be evaluated.
Pointer arg wrapping
The "count" for pointer args should be hinted so that they can be properly wrapped. E.g.
- vtkVariant::ToInt(bool *vtkSingleValue(valid))
- vtkVariant::ToInt(bool *vtkOptionalSingleValue(valid)) - can be safely set to NULL
- vtkDataArray::SetTuple(double *vtkMultiValue(tuple, GetNumberOfComponents))
In the latter, the name of the method to get the count is supplied in the hint. Recognizing these macros in vtkParse would be easy.
Reference arg wrapping: &arg
This is trivial to add, only a few lines would have to be added to vtkWrapPython. For the reference arg, the user would have to pass a container object that supported both the sequence protocol and the number protocol, e.g. like a numpy array. For example, the user could make an array([0], 'f') and pass it, and after the call the result would be stored in the array.