Hi!

I am sure that the following problem has a solution that is well-known
- but not well-known to me, and also I did not succeed in googling it.

Background is #12029.

In the following, self is a ClonableIntArray, but I guess the problem
is more general. self._list is some "int *" array of known length
self._len. The aim is to convert self._list into a Python list, as
quickly as possible.

Is there a ready-made function that would do such conversion?

I did this, which works fine:
        cdef int i
        cdef list L = []
        for  i from 0<=i<self._len:
            L.append(self._list[i])
        return L

But Florent was suggesting that it might be faster to create an
"empty" list of length self._len using PyList_New, and to put the
items onto that list by PyList_SetItem, rather than appending them.

First question: Could PyList_SET_ITEM (for a list of known length)
really be faster than appending to the list?

Second question: I know that PyList_SET_ITEM is only borrowing a
reference. Hence, I am not surprised that I got segfaults in the first
place. But why am I still getting segfaults with the following code,
where I manually add a reference?
        cdef int i, o
        cdef list L = PyList_New(self._len)
        for  i from 0<=i<self._len:
            o = self._list[i]
            Py_INCREF(o)
            PyList_SET_ITEM(L,i,o)
        return L

More precisely, it works (or seems to work) on ClonableIntArrays of
size 100, but not on a list of size 500.

Best regards,
Simon

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to