[Pyrex] Segfault when trying to set attribute of another class
Greg Ewing
greg at cosc.canterbury.ac.nz
Mon Mar 22 00:56:42 CET 2004
Armin Bauer <armin.bauer at desscon.com>:
> cdef class CLASS2:
> cdef STRUCT2 *_struct2
>
> def get_class1(self):
> s = CLASS1()
> s._set_struct1(<object>get_struct1_from_struct2(self._struct2))
> print "test2"
> return s
It looks like you're trying to smuggle a non-Python pointer into the
_set_struct1 method by pretending it's a Python object. That won't
work.
You should NEVER cast a pointer to a Python type unless it really and
truly points to a Python object. Crashing and burning is guaranteed to
result.
There are two things you could do here:
(1) Assign the pointer directly, not bothering with a _set_struct1
method.
cdef class CLASS2:
...
def get_class1(self):
cdef CLASS1 *s # <--- note type declaration
s = CLASS1()
s._struct1 = get_struct1_from_struct2(self._struct2)
...
(2) If you particularly want to set it via an accessor method, use
a C method (introduced in Pyrex 0.9):
cdef class CLASS1:
...
# --- Note the following header ---
cdef void _set_struct1(self, STRUCT1 *s1):
self._struct1 = s1
cdef class CLASS2:
...
def get_class1(self):
cdef CLASS1 *s # <--- note type declaration
s = CLASS1()
s._set_struct1(get_struct1_from_struct2(self._struct2))
...
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury, | A citizen of NewZealandCorp, a |
Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. |
greg at cosc.canterbury.ac.nz +--------------------------------------+
More information about the Pyrex
mailing list