[PyQt] SIP Pointer Confusion
Jay L.
jlaura at asu.edu
Tue Oct 4 05:47:11 BST 2016
I have made some progress on this. My .sip file now includes:
13 void Allocate(int width, int height, int pitch, bool withHost,
float *devMem /In/ = NULL, SIP_PYOBJECT hostMem /In/ = NULL);
14 %MethodCode
15 Py_BEGIN_ALLOW_THREADS
16 long length = a0 * a1;
17 a5 = PyFloat_AsDouble(sipConvertToArray(a5, "f", length, 0));
18 sipCpp->Allocate(a0, a1, a2, a3, NULL, a5);
19 Py_END_ALLOW_THREADS
20 %End
This is, I believe the method that I should be using to grab the arguments,
operate on the buffer and then pass the arguments on to the C++ method.
As Phil suggested, sipConvertToArray looks like way to go here. I pass in
a memoryview (buffer) using ndarray.data. Using just sipConvertToArray
results in an error saying that no known conversion from Py_OBJECT* to
float* is known.
Above, I am trying to use PyFloat_AsDouble to make the conversion (that I
believe needs to be made). This though, results in the same error. I have
checked PyQt, PyKDE, PyQwt, and the QGIS project to see if those projects
have examples of passing a numpy array in, but must be missing any examples.
Any additional suggestions much appreciated for getting numpy array data
passed over to the C++ side.
On Thu, Sep 29, 2016 at 6:58 PM, Jay L. <jlaura at asu.edu> wrote:
> Phil,
>
> Super helpful and gets me rolling again. I definitely was not groking
> many of the annotations so that point is awesome.
>
> Thanks!
> J
>
> On Thu, Sep 29, 2016 at 5:58 PM, Phil Thompson <
> phil at riverbankcomputing.com> wrote:
>
>> On 30 Sep 2016, at 1:43 am, Jay L. <jlaura at asu.edu> wrote:
>> >
>> > Just starting to dig into SIP. I have a .h file that contains the
>> following public method:
>> >
>> > void Allocate(int width, int height, int pitch, bool withHost, float
>> *devMem = NULL, float* hostMem = NULL);
>> >
>> > have created a corresponding .sip file:
>> >
>> > 1 //SIP wrapper for CUDA Image
>> > 2
>> > 3 class CudaImage
>> > 4 {
>> > 5 %TypeHeaderCode
>> > 6 #include "cudaImage.h"
>> > 7 %End
>> > 8
>> > 9 public:
>> > 10 CudaImage();
>> > 11
>> > 12 void Allocate(int width, int height, int pitch, bool withHost, float
>> *devMem=NULL, float *hostMem=NULL) /KeywordArgs="All"/;
>> > 13 double Download();
>> > 14
>> > 15
>> > 16 int width;
>> > 17 int height;
>> > 18 int pitch;
>> > 19 };
>> >
>> > Using CMake, I have the build working, can import the module into
>> Python and can call the constructor (so limited success). I can also call
>> the Allocate method. Unfortunately, on the Python side, I can not get the
>> float *devMem=NULL or float *hostMem=Nullarguments exposed. I have been
>> over the SIP documentation and no Annotations are leaping out as missing.
>> >
>> > The ultimate goal is to pass a numpy array (.data attribute I believe)
>> to the hostMemargument.
>> >
>> > How does one go about exposing pointers in SIP? What about pointers
>> with a default, NULL, argument?
>>
>> Pointers to fundamental types are (by default) treated as a way to return
>> a value from the method and not pass a value to it - see the /In/
>> annotation.
>>
>> You should then hit the problem that you will have to provide
>> hand-written code to handle those arguments because SIP doesn't know how to
>> convert them to a Python object. You probably need to declare the types as
>> SIP_PYOBJECT and handle the conversion in %MethodCode. Alternatively if
>> that is a common situation in your API then consider defined a mapped type.
>>
>> If numpy arrays support the buffer protocol then you should look at
>> exploiting that. You should also look at sip.array and sipConvertToArray().
>>
>> Phil
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20161003/b671178a/attachment.html>
More information about the PyQt
mailing list