PySide Shiboken Type Converters
PySide Shiboken Type Converters
In the process of creating Python bindings of a C++ library, most of the C++ classes will have wrappers representing them in Python land. But there may be other classes that are very simple and/or have a Python type as a direct counter part. (Example: a “Complex” class, that represents complex numbers, has a Python equivalent in the “complex” type.) Such classes, instead of getting a Python wrapper, normally have conversions rules, from Python to C++ and vice-versa.
For the user defined conversion code to be inserted in the proper places, the “<conversion-rule>” tag must be used.
The details will be given later, but the gist of it are the tags <native-to-target>, which has only one conversion from C++ to Python, and <target-to-native>, that may define the conversion of multiple Python types to C++’s “Complex” type.
Shiboken expects the code for <native-to-target>, to directly return the Python result of the conversion, and the added conversions inside the <target-to-native> must attribute the Python to C++ conversion result to the %out variable.
Expanding on the last example, if the binding developer want a Python 2-tuple of numbers to be accepted by wrapped C++ functions with “Complex” arguments, an <add-conversion> tag and a custom check must be added. Here’s how to do it:
Container Conversions
Converters for <container-type> are pretty much the same as for other type, except that they make use of the type system variables %INTYPE_# and %OUTTYPE_#. Shiboken combines the conversion code for containers with the conversion defined (or automatically generated) for the containees.
Variables & Functions
%in
Variable replaced by the C++ input variable.
%out
Variable replaced by the C++ output variable. Needed to convey the result of a Python to C++ conversion.
%INTYPE
Used in Python to C++ conversions. It is replaced by the name of type for which the conversion is being defined. Don’t use the type’s name directly.
%INTYPE_#
Replaced by the name of the #th type used in a container.
%OUTTYPE
Used in Python to C++ conversions. It is replaced by the name of type for which the conversion is being defined. Don’t use the type’s name directly.
%OUTTYPE_#
Replaced by the name of the #th type used in a container.
%CHECKTYPE[CPPTYPE]
Replaced by a Shiboken type checking function for a Python variable. The C++ type is indicated by CPPTYPE.
Converting The Old Converters
If you use Shiboken for your bindings, and has defined some type conversions using the Shiboken::Converter template, then you must update your converters to the new scheme.
Previously your conversion rules were declared in one line, like this:
And implemented in a separate C++ file, like this:
In this case, the parts of the implementation that will be used in the new conversion-rule are the ones in the two last method static inline PyObject* toPython(const Complex& cpx) and static inline Complex toCpp(PyObject* pyobj). The isConvertible method is gone, and the checkType is now an attribute of the <add-conversion> tag. Refer back to the first example in this page and you will be able to correlate the above template with the new scheme of conversion rule definition.