[PyQt] Pointer Attribute

Phil Thompson phil at riverbankcomputing.com
Wed Oct 26 09:21:57 BST 2016


On 26 Oct 2016, at 5:32 am, Jay L. <jlaura at asu.edu> wrote:
> 
> Made a bit more progress I think.  The below is building but crashing when trying to convert from the Python Obj to a SiftPoint.  

...to an array of SiftPoints. This wasn't clear in your original post.

> The %GetCode returns what I am expecting - a Python list of SiftPoint objects.  The SiftPoint has been wrapped.  The %SetCode is seg. faulting on sipConvertToType.
> 
> Within the %GetCode, I do not believe I have access to sipTransferObj or sipIsErr.
> 
> Two questions:
> 
> 1) Is this the intended use of %GetCode and %SetCode?  Should I be using another directive?
> 
> 2) In the SetCode block, how does one go about converting a list of class instances into a C++ pointer?  Is this code heading down the right path?
> 
> Thanks! 
> 
>     SiftPoint *h_data
>     {
>         %GetCode
>             size_t size = sipCpp->numPts;
>             PyObject *l = PyList_New(sipCpp->numPts);
>             for (size_t i = 0; i < size; ++i){
>                 SiftPoint *sp = new SiftPoint(sipCpp->h_data[i]);
>                 PyObject *p = sipConvertFromType((void*)(sp), sipType_SiftPoint, NULL);
>                 PyList_SetItem(l, i, p);
>             }
>             return l;
>         %End
> 
>         %SetCode
>         size_t length = PyList_GET_SIZE(sipPy);

You can't assume that you will get a list. You should use PyList_Size() and raise a TypeError if it returns a negative length.

>         for (int i=0; i < length; ++i){
> 
>         if (!sipCanConvertToType(PyList_GET_ITEM(sipPy, i),
>                                  sipType_SiftPoint, SIP_NOT_NONE))
>             return 0;
>         }
> 
>         printf("So far so good\n");
>         sipCpp->numPts = length;
> 
>         // Set the length of the h_data
>         SiftPoint h_data[length];

The array must be on the heap.

>         sipCpp->h_data = h_data;

You should consider what happens to any previous array.

> 
>         for (size_t i=0; i < length; ++i){
>             PyObject *sp  = PyList_GetItem(sipPy, i);
> 
>             SiftPoint *sif = reinterpret_cast<SiftPoint *>(sipConvertToType(sp,
>                                                                             sipType_SiftPoint,
>                                                                             NULL,
>                                                                             SIP_NOT_NONE,
>                                                                             NULL, 0));
>             h_data[i] = *sif;
>         }
>         %End

Phil


More information about the PyQt mailing list