[Cython] Stealing/borrowed references semantics
Stefan Behnel
stefan_ml at behnel.de
Wed Jul 8 10:05:17 CEST 2009
Robert Bradshaw wrote:
> Of course, there may already be code out there that counts on the
> true PyList_SET_ITEM semantics.
Certainly.
> Another option:
>
> cdef extern from *:
> ctypedef void stolen_object "PyObject*"
>
> cdef inline stolen_object steal(object):
> Py_INCREF(o)
> return <stolen_object>o
>
> cdef extern void PyList_SET_ITEM(object, Py_ssize_t, stolen_object)
>
>
> One would then call it as
>
> PyList_SET_ITEM(L, 0, steal(item))
>
> (Of course, PyList_SET_ITEM is worse than this, as one needs to
> manually (x)decref the original spot in the list as well... This
> makes me lean towards not providing it as a friendly function taking
> ordinary objects).
That's because it's a macro. It's a different situation for functions -
although I can't currently find any in the C-API that really steal
references. If users actually have to special case stolen references
anyway, maybe this whole "stolen" business is really a non-problem...
Stefan
More information about the Cython-dev
mailing list