Opened 5 years ago
Support for borrowed references
|Reported by:||scoder||Owned by:||somebody|
This should be supported:
cdef borrowed object bo1 = x.some_attr # no INCREF! cdef borrowed object bo2 = bo1 # no INCREF! cdef object o = bo2 # INCREF!
Similarly, defining borrowed references as function return type:
cdef borrowed object PyTuple_GetItem(object t, Py_ssize_t index) cdef object x = PyTuple_GetItem( (1,2,3), 1 ) # INCREF!
It does not make sense to support this:
cdef object o = <borrowed object>x
so the 'borrowed' modifier would only ever appear after a 'cdef'.
A nice side-effect of borrowed references is that they do not require the GIL.
Note that borrowed references differ from supporting stolen references in that they only avoid the INCREF on assignments, which cannot easily be prevented otherwise. Stolen references can be used with an additional INCREF and do not require compiler support.