[Pyrex] Segfault when trying to set attribute of another class

Armin Bauer armin.bauer at desscon.com
Sat Mar 20 19:08:40 CET 2004


Hi,

i want to do the following

i have to classes defined.

i want to call in python:

x = mymodule.CLASS2
y = x.get_class1

y is then supposed to be of type CLASS2 with its struct
the pyrex code for this looks like this:

cdef extern from "foo.h"
	typedef struct STRUCT1
	typedef struct STRUCT2

cdef class CLASS1:
	cdef STRUCT1 *_struct1
	
	def _set_struct1(self, struct1)
		self._struct1 = <STRUCT1*>struct1
		print "test1"

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


The struct included from foo.h are incomplete types.
the complete STRUCT2 would look like this:

struct STRUCT2 {
	...
	STRUCT1 *somename;
	...
}

The get_struct1_from_struct2 function just returns the pointer to
somename:

STRUCT1 *get_struct1_from_struct2(STRUCT2 *me)
{
	return me->somename;
}

The pyrex code compiles correctly but i get segfaults when i try to use
it. The last thing it prints is "test1", "test2" is never reached.

What is the correct way to achieve this behaviour? Sorry if my question
seems stupid or redundant.

Best regards, Armin Bauer





More information about the Pyrex mailing list