<div dir="ltr">Phil,<div><br></div><div>Thanks for the pointers. Here is the resultant code.</div><div><br></div><div>I have added the check for length and am now allocating the h_data array on the heap. I have not added error check for any previous array, but the point is appreciated.</div><div><br></div><div>In the below code, I am segfaulting on the sipConvertToType call and am not sure why. The sipTypePoint definition is the lowest code block. I do not have any TypeCode defined for the SiftPoint, but all of the types are standard so I think custom code is not needed there?</div><div><br></div><div><br></div><div>The small debug block (commented out) is working as a replacement for the sipConvertToType call.</div><div><br></div><div>Thanks for any insight, </div><div>J</div><div><br></div><div><br></div><div><div><font face="monospace, monospace"> // HOST DATA</font></div><div><font face="monospace, monospace"> SiftPoint *h_data</font></div><div><font face="monospace, monospace"> {</font></div><div><font face="monospace, monospace"> %GetCode</font></div><div><font face="monospace, monospace"> size_t size = sipCpp->numPts;</font></div><div><font face="monospace, monospace"> PyObject *l = PyList_New(sipCpp->numPts);</font></div><div><font face="monospace, monospace"> for (size_t i = 0; i < size; ++i){</font></div><div><font face="monospace, monospace"> SiftPoint *sp = new SiftPoint(sipCpp->h_data[i]);</font></div><div><font face="monospace, monospace"> PyObject *p = sipConvertFromType((void*)(sp)<wbr>, sipType_SiftPoint, NULL);</font></div><div><font face="monospace, monospace"> PyList_SetItem(l, i, p);</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"> return l;</font></div><div><font face="monospace, monospace"> %End</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> %SetCode</font></div><div><font face="monospace, monospace"> size_t length = PyList_Size(sipPy);</font></div><div><font face="monospace, monospace"> //Check that a PyList has been passed in.</font></div><div><font face="monospace, monospace"> if (length == -1){</font></div><div><font face="monospace, monospace"> PyErr_SetString(PyExc_<wbr>TypeError,</font></div><div><font face="monospace, monospace"> "A non-empty list is of SiftPoints is required.");</font></div><div><font face="monospace, monospace"> sipErr = -1;</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> SiftPoint *array_of_points = (SiftPoint *)malloc(sizeof(SiftPoint) * length);</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> //Check that all of the elements of the list are convertable.</font></div><div><font face="monospace, monospace"> for (size_t i=0; i < length; ++i){</font></div><div><font face="monospace, monospace"> if (!sipCanConvertToType(PyList_<wbr>GET_ITEM(sipPy, i),</font></div><div><font face="monospace, monospace"> sipType_SiftPoint, SIP_NOT_NONE)){</font></div><div><font face="monospace, monospace"> PyErr_SetString(PyExc_<wbr>ValueError,</font></div><div><font face="monospace, monospace"> "Unable to convert element to SiftPoint object.");</font></div><div><font face="monospace, monospace"> sipErr = -1;</font></div><div><font face="monospace, monospace"> }else{</font></div><div><font face="monospace, monospace"> printf("Processing point: %i\n", i);</font></div><div><font face="monospace, monospace"> PyObject *sp = PyList_GetItem(sipPy, i);</font></div><div><font face="monospace, monospace"> SiftPoint *siftpoint = (SiftPoint *)sipConvertToType(sp,</font></div><div><font face="monospace, monospace"> sipType_SiftPoint,</font></div><div><font face="monospace, monospace"> NULL,</font></div><div><font face="monospace, monospace"> SIP_NOT_NONE,</font></div><div><font face="monospace, monospace"> NULL,</font></div><div><font face="monospace, monospace"> 0);</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> //Debug to create a new SiftPoint</font></div><div><font face="monospace, monospace"> //SiftPoint *siftpoint = new SiftPoint;</font></div><div><font face="monospace, monospace"> //siftpoint->xpos = i;</font></div><div><font face="monospace, monospace"> array_of_points[i] = *siftpoint;</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> for (int i = 0;i < length;++i){</font></div><div><font face="monospace, monospace"> printf("XPOS: %f\n", array_of_points[i].xpos);</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"> return 0;</font></div><div><font face="monospace, monospace"> %End</font></div><div><font face="monospace, monospace"> };</font></div></div><div><br></div><div><br></div><div><div><font face="monospace, monospace">// HOST DATA</font></div><div><font face="monospace, monospace"> SiftPoint *h_data</font></div><div><font face="monospace, monospace"> {</font></div><div><font face="monospace, monospace"> %GetCode</font></div><div><font face="monospace, monospace"> size_t size = sipCpp->numPts;</font></div><div><font face="monospace, monospace"> PyObject *l = PyList_New(sipCpp->numPts);</font></div><div><font face="monospace, monospace"> for (size_t i = 0; i < size; ++i){</font></div><div><font face="monospace, monospace"> SiftPoint *sp = new SiftPoint(sipCpp->h_data[i]);</font></div><div><font face="monospace, monospace"> PyObject *p = sipConvertFromType((void*)(sp), sipType_SiftPoint, NULL);</font></div><div><font face="monospace, monospace"> PyList_SetItem(l, i, p);</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"> return l;</font></div><div><font face="monospace, monospace"> %End</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> %SetCode</font></div><div><font face="monospace, monospace"> size_t length = PyList_Size(sipPy);</font></div><div><font face="monospace, monospace"> //Check that a PyList has been passed in.</font></div><div><font face="monospace, monospace"> if (length == -1){</font></div><div><font face="monospace, monospace"> PyErr_SetString(PyExc_TypeError,</font></div><div><font face="monospace, monospace"> "A non-empty list is of SiftPoints is required.");</font></div><div><font face="monospace, monospace"> sipErr = -1;</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> SiftPoint *array_of_points = (SiftPoint *)malloc(sizeof(SiftPoint) * length);</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> //Check that all of the elements of the list are convertable.</font></div><div><font face="monospace, monospace"> for (size_t i=0; i < length; ++i){</font></div><div><font face="monospace, monospace"> if (!sipCanConvertToType(PyList_GET_ITEM(sipPy, i),</font></div><div><font face="monospace, monospace"> sipType_SiftPoint, SIP_NOT_NONE)){</font></div><div><font face="monospace, monospace"> PyErr_SetString(PyExc_ValueError,</font></div><div><font face="monospace, monospace"> "Unable to convert element to SiftPoint object.");</font></div><div><font face="monospace, monospace"> sipErr = -1;</font></div><div><font face="monospace, monospace"> }else{</font></div><div><font face="monospace, monospace"> printf("Processing point: %i\n", i);</font></div><div><font face="monospace, monospace"> PyObject *sp = PyList_GetItem(sipPy, i);</font></div><div><font face="monospace, monospace"> SiftPoint *siftpoint = (SiftPoint *)sipConvertToType(sp,</font></div><div><font face="monospace, monospace"> sipType_SiftPoint,</font></div><div><font face="monospace, monospace"> NULL,</font></div><div><font face="monospace, monospace"> SIP_NOT_NONE,</font></div><div><font face="monospace, monospace"> NULL,</font></div><div><font face="monospace, monospace"> 0);</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> //Debug to create a new SiftPoint</font></div><div><font face="monospace, monospace"> //SiftPoint *siftpoint = new SiftPoint;</font></div><div><font face="monospace, monospace"> //siftpoint->xpos = i;</font></div><div><font face="monospace, monospace"> array_of_points[i] = *siftpoint;</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> for (int i = 0;i < length;++i){</font></div><div><font face="monospace, monospace"> printf("XPOS: %f\n", array_of_points[i].xpos);</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"> return 0;</font></div><div><font face="monospace, monospace"> %End</font></div><div><font face="monospace, monospace"> };</font></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 26, 2016 at 1:21 AM, Phil Thompson <span dir="ltr"><<a href="mailto:phil@riverbankcomputing.com" target="_blank">phil@riverbankcomputing.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 26 Oct 2016, at 5:32 am, Jay L. <<a href="mailto:jlaura@asu.edu">jlaura@asu.edu</a>> wrote:<br>
><br>
> Made a bit more progress I think. The below is building but crashing when trying to convert from the Python Obj to a SiftPoint.<br>
<br>
</span>...to an array of SiftPoints. This wasn't clear in your original post.<br>
<span class=""><br>
> 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.<br>
><br>
> Within the %GetCode, I do not believe I have access to sipTransferObj or sipIsErr.<br>
><br>
> Two questions:<br>
><br>
> 1) Is this the intended use of %GetCode and %SetCode? Should I be using another directive?<br>
><br>
> 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?<br>
><br>
> Thanks!<br>
><br>
> SiftPoint *h_data<br>
> {<br>
> %GetCode<br>
> size_t size = sipCpp->numPts;<br>
> PyObject *l = PyList_New(sipCpp->numPts);<br>
> for (size_t i = 0; i < size; ++i){<br>
> SiftPoint *sp = new SiftPoint(sipCpp->h_data[i]);<br>
> PyObject *p = sipConvertFromType((void*)(sp)<wbr>, sipType_SiftPoint, NULL);<br>
> PyList_SetItem(l, i, p);<br>
> }<br>
> return l;<br>
> %End<br>
><br>
> %SetCode<br>
> size_t length = PyList_GET_SIZE(sipPy);<br>
<br>
</span>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.<br>
<span class=""><br>
> for (int i=0; i < length; ++i){<br>
><br>
> if (!sipCanConvertToType(PyList_<wbr>GET_ITEM(sipPy, i),<br>
> sipType_SiftPoint, SIP_NOT_NONE))<br>
> return 0;<br>
> }<br>
><br>
> printf("So far so good\n");<br>
> sipCpp->numPts = length;<br>
><br>
> // Set the length of the h_data<br>
> SiftPoint h_data[length];<br>
<br>
</span>The array must be on the heap.<br>
<br>
> sipCpp->h_data = h_data;<br>
<br>
You should consider what happens to any previous array.<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> for (size_t i=0; i < length; ++i){<br>
> PyObject *sp = PyList_GetItem(sipPy, i);<br>
><br>
> SiftPoint *sif = reinterpret_cast<SiftPoint *>(sipConvertToType(sp,<br>
> sipType_SiftPoint,<br>
> NULL,<br>
> SIP_NOT_NONE,<br>
> NULL, 0));<br>
> h_data[i] = *sif;<br>
> }<br>
> %End<br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">Phil</font></span></blockquote></div><br></div>