[PyQt] SIP PyList of SIP Wrapped Objects

Phil Thompson phil at riverbankcomputing.com
Wed Oct 5 22:21:46 BST 2016


On 5 Oct 2016, at 10:13 pm, Jay L. <jlaura at asu.edu> wrote:
> 
> I have a struct with an associated SIP specification file:
> 
> class SiftPoint{
> 
>     %TypeHeaderCode
>       #include "cudaSift.h"
>       #include "numpy/arrayobject.h"
>     %End
> 
>     %TypeCode
>       PyObject *makearray(float array[], size_t size)
>       {
>       import_array();
>       npy_intp dim = size;
>       return PyArray_SimpleNewFromData(1, &dim, NPY_FLOAT32, (void *)array);
>       }
>     %End
> 
>     public:
>       float xpos;
>       float ypos;
>       float scale;
> 
>       //float data[128];
>       float get_descriptor();
>       %MethodCode
>         return makearray(sipCpp->data, 128);
>       %End
> 
> I have been able to work with the array data by using method code and passing out to a numpy array (which is working well).
> 
> I then have a second struct also exposed as a class that contains a pointer to some number of SiftPoint instances.
> 
>   //SiftData
>   class SiftData{
> 
>   %TypeHeaderCode
>     #include "cudaSift.h"
>   %End
> 
>   public:
>     int numPts;
>     int maxPts;
> 
>     //SiftPoint *h_data;
>     SIP_PYLIST host_data();
>     %MethodCode
>       size_t size = sipCpp->numPts;
>       PyObject *l = PyList_New(sipCpp->numPts);
>       for (size_t i = 0; i != size; i++){
> 
>         SiftPoint sp = sipCpp->h_data[i];
>         printf("%f, %f\n", sp.xpos, sp.ypos);
>         PyObject *p = sipConvertFromType((void*)(&sp),sipType_SiftPoint, NULL);
>         PyList_SetItem(l, i, p);
>       }
>       return l;
>     %End
>     //Not exposed via Python
>     //SiftPoint *d_data;
>   };
> 
> Here, I am trying to expose a PyList of SiftPoint instances.  My understanding is that I need to use sipConvertFromType to convert the already mapped type to a python type before entry into a PyList.  The above code is compiling without issue, and I can see all of the SiftPoint class attributes.  Unfortunately, all of the attributes are returning NaN (as opposed to some float value).
> 
> Am I missing something in the conversion of an already mapped type?  

The SiftPoint object you are wrapping is on the stack. You need a copy on the heap...

    SiftPoint *sp = new SiftPoint(sipCpp->h_data[i]);

Phil


More information about the PyQt mailing list