[Cython] Accessing C attributes from Python functions in the interpreter
Johannes Wienke
languitar at semipol.de
Sun Jun 8 11:10:08 CEST 2008
Am 06/08/2008 01:48 AM schrieb Dag Sverre Seljebotn:
> 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.
>
> Can you provide a full running session of what you try to do?
Ok, I have found the problem but have absolutely no clue how to solve this:
My code has to run inside Reinteract
(http://fishsoup.net/software/reinteract/). At the bottom of the site
the author explains the system Reinteract uses: "However, if reinteract
detects that a statement modifies a variable, then it makes a shallow
copy of the variable using copy.copy()"
This call to copy seems to be the problem because C data structures seem
to be forgotten while copying. Here's a little test case:
cdef extern from "string.h":
char *strcpy(char *dest, char *src)
long strlen(char *s)
cdef extern from "stdlib.h":
void *calloc(long nmemb, long size)
cpdef class Foo:
cdef char *arg
def fillArg(self, string):
s = string
self.arg = <char *>calloc(strlen(s), sizeof(char))
strcpy(self.arg, s)
def tellArg(self):
if self.arg == NULL:
print "I don't have an arg"
else:
print self.arg
>>> from ship import test
>>> f = test.Foo()
>>> f.fillArg("Hey")
>>> f.tellArg()
Hey
>>> import copy
>>> fCopy = copy.copy(f)
>>> fCopy.tellArg()
I don't have an arg
In my case I simply didn't check that arg was not NULL and that resulted
in the segfault.
But is there a way to convince copy to copy also the C declarations?
Thanks in advance
Johannes
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
Url : http://codespeak.net/pipermail/cython-dev/attachments/20080608/c3caae25/attachment-0001.pgp
More information about the Cython-dev
mailing list