[Pyrex] access to c data class members

ahalda at cs.mcgill.ca ahalda at cs.mcgill.ca
Sun Aug 27 17:30:39 UTC 2006


Hi,

I am having trouble accessing c-data in a pyrex class. My class has a pointer
to some c data (in a struct). It looks like some people have recently
mentioned similar problems on the list, but I wasn't clear on the answer.

The goal is to have a non-member function modify the data and return a
(new) python object. The tricky thing is that the data does not map to a
python type. (It works otherwise)

EG:
------------------------------------
cdef class MyClass:
	cdef OBJSTRUCT *data      #pointer to a struct

def load_File():
	newObj = MyClass()               #alternately, 'cdef Module newMod'
	newObj.data = c_mod.getData() #returns pointer to the new object
	return newObj
------------------------------------

If I create a python object by 'newObj = MyClass()', when I modify the data
by newObj.data, i get : "Cannot convert 'OBJSTRUCT (*)' to Python object"
on the second line of the fuction.

If instead I do 'cdef Module newMod' as declaration, it compiles, and the
data gets loaded correctly into the *data field. However, the returned
object has type 'NoneType'.

Both results make general sense to me: The python object cannot touch
c-data, and the c-object has no type. But is there a way around this?

The workaround I use is to make load_file a member function of MyClass,
and then call it from a new object created in python. Then I correctly get
an object of type 'MyClass' and can load the data. But it forces me to
change my class organization.

So, is there a way to access all c-data of a python object from within
pyrex? Or alternately, to turn the c-object into a python object?

Thanks! pyrex is great by the way. It's so easy!

Allan




More information about the Pyrex mailing list