[Cython] Ctypes object <-> pointer conversion.

Robert Bradshaw robertwb at math.washington.edu
Fri Aug 5 01:31:35 CEST 2011

This is getting a bit OT but is worth discussing, so I'm starting a new thread.

On Thu, Aug 4, 2011 at 2:51 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Dag Sverre Seljebotn wrote:
>>  - One idea is coercion of C pointers to ctypes Python objects and back
>> again.
> Some way of requesting this manually might be useful, but
> I don't think I'd like it to happen automatically. Slinging
> raw pointers around in Python isn't something to be done
> lightly -- even if all the code that dereferences it is
> in Cython, there are problems with managing the lifetime
> of whatever it references.

You know, C has the same problem with slinging around raw pointers and
managing their lifetimes :). Of course, with C, the user is all to
painfully aware of the situation.

I think this would be most useful for providing data from Python (even
an interactive prompt) to a Cython module. I agree about it being
dangerous to do implicitly, but something explicit like

def entry_point(ctypes.CData x):
    cdef double* result = func(<int*>x)
    return <ctypes.CData>result   # or cytpes.CData(result,
option=...) to control deallocation

could be really nice. Maybe CData could even be parameterized.

- Robert

More information about the cython-devel mailing list