[Cython] Cannot convert pointer to Python Object
Richard Clarke
rsclarke at gmail.com
Thu Jul 23 15:29:25 CEST 2009
Thank you Robert, the explanation on how and where to use cdef was
most helpful. I think it has just clicked(with a flag waving
'idiot').
The PyCObject concept is a nice idea, removing cdef foo_t *foo from
outside of __init__ ? However, I see implications in memory
deallocation. How does cython currently handle memory deallocation?
I assume the best practice would be for users to deallocate memory in
the class __del__ method (calling the appropriate wrapped c library
destructor). Otherwise does cython simply free(foo) or interprets the
language to build a depth first deallocation method? I guess it
should do the simplest free(foo), if the developer was lazy not to
deallocate memory, they can suffer with memory leaks. I think I might
have answered my own question here...
Richard
On Thu, Jul 23, 2009 at 12:43 PM, Robert
Bradshaw<robertwb at math.washington.edu> wrote:
> On Jul 23, 2009, at 3:39 AM, Richard Clarke wrote:
>
>> Hi,
>>
>> Can anyone point out where I am going wrong?
>
> Sure. There is no way convert a pointer to a Python object. (Perhaps
> we could support this via http://www.python.org/doc/2.6/c-api/
> cobject.html , but certainly that'll take a lot of thought).
>
> You need to make Bar into a cdef class for it to have a foo_t*
> member. Also, if a return type is foo*, you need to make it a cdef
> function. I would recommend reading http://docs.cython.org/ .
>
>> // foo.pyx
>>
>> cdef extern from "foo.h":
>> ctypedef struct foo_t:
>> int fd
>>
>> foo_t* init()
>>
>> class Bar:
>
> > cdef class Bar:
> > cdef foo* __foo
>
>> def __init__(self):
>> self.__foo = self.__open()
>>
>> def __open(self):
>
> > cdef foo* open(self):
>
>> cdef foo_t *foo
>> foo = init()
>> return foo
>>
>>
>> Results in the error:
>> /test/foo.pyx:14:12: Cannot convert 'foo_t *' to Python object
>>
>> Cython version 0.9.6.14
>>
>> Thank you,
>> Richard.
>> _______________________________________________
>> Cython-dev mailing list
>> Cython-dev at codespeak.net
>> http://codespeak.net/mailman/listinfo/cython-dev
>
> _______________________________________________
> Cython-dev mailing list
> Cython-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/cython-dev
>
More information about the Cython-dev
mailing list