[Pyrex] public extension type to replace a complex structure variable

Daehyok Shin sdhyok at gmail.com
Thu Feb 17 05:34:04 CET 2005


I want to expose a variable to Python using PyRex public extension type.
But, the problem is that the variable has quite complex structure.
Like big_object in the appended program code, the variable contains
pointers indicating other structure variables (here, small_object).
I want to replace the original definitions of structures with ones
generated from public extension type.

I tried the appended PyRex code, but got the horrible "segment failure" error.
It seems I made a mistake in using C functions to construct the
structure variables?
(In fact, the structure of original variable is so complex that I have
to call C functions to construct the variables. I do not include the
initialization code)

Would you tell me how I can access to the members of the variables
transparently from Python (pls look at the last program segment)?

Thanks.

>>>>>>>>>>>>>>>>>>> C program <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
struct big_object {
	int ID;
	small_object** sobjs;
}

struct small_object {
	int ID;
}

big_object* construct_big_object(int ID) {
	int i;
	big_object* bobj;

	bobj = (big_object*) malloc(sizeof(struct big_object));
	bobj.ID = ID;
	for (i=0; i<10; i++) {
		bobj.sobjs[i] = construct_small_object(i);
	}
	return bobj;
}

small_object* construct_small_object(int ID) {
	small_object* sobj;

	sobj = (small_object*) malloc(sizeof(stuct small_object));
	return sobj;
}

>>>>>>>>>>>>>>>>>>> PyRex Extension Types <<<<<<<<<<<<<<<<<<<<<<<<<<
# here, extern declaration inserted.
cdef public class SmallObject [type SmallObject_Type, object small_object]:
    cdef public int ID
    
cdef public class BigObject [type BigObject_Type, object big_object]:
    cdef public int ID
    cdef small_object** sobjs

    cdef __new__(self, ID):
           # Something should be done here.

>>>>>>>>>>>>>>>>>>>> Python Command Shell <<<<<<<<<<<<<<<<<<<<<<<<

>>> b=mymodule.BigObject(10)
>>> print b.sobjs[5].ID

Daehyok Shin



More information about the Pyrex mailing list