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

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to