[Cython] Accessing C attributes from Python functions in the interpreter
Robert Bradshaw
robertwb at math.washington.edu
Sun Jun 8 02:00:41 CEST 2008
On Jun 7, 2008, at 4:24 PM, Johannes Wienke wrote:
> Hi,
>
> maybe I've just got a lack of understanding or I don't know...
>
> I've got a class that in general looks like this:
>
> cpdef class Foo:
> cdef char* myData
>
> cdef void setData(Foo self, char *data)
> self.myData = data
>
> cpdef doSomething(Foo self)
> print self.myData
>
> The problem is that working with myData directly from Cython is no
> problem but calling doSomething from the python interpreter causes a
> segfault because self.myData then points to NULL. I've also observed
> that self in setData points to a different address than self in
> doSomething if it is called from the interpreter.
>
> What's the problem with this approach?
Where are you setting your myData? If you do
Foo().doSomething()
then it will segfault because the data isn't set yet. If you have
def set_data_from_python(self, py_string):
self.myData = py_string
Then the conversion will happen, but myData is set to point to the
inside of py_string (i.e. no copying is actually done) and so the
instant py_string gets deallocated the actual char* gets reclaimed.
Hopefully this helps, though the code snippet above doesn't crash by
itself.
- Robert
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : http://codespeak.net/pipermail/cython-dev/attachments/20080607/1de1ce27/attachment.pgp
More information about the Cython-dev
mailing list