Hi,

I am trying to define a c struct and wrap it in a python object, but 
fail to make cython accept my .pyx files.

My intention is to define c structs that are passed to cdefs (for more 
efficient function calls) and wrap them in extension types that allow 
for greater flexibility and higher level programming.



I use three files:

vector.pxd and vector.pyx define the cdef struct and wrapper extension type

vector_test.pyx defines a variable whose type is the extension type,
extracts the c struct, and attempts an operation on it.

My files contain:

File vector.pxd:
================
cdef struct vector_c:
     double x, y, z

cdef class vector:
     cdef:
         vector_c data
         vector_c get_data(self)
================

File vector.pyx
================
cdef class vector:
     def __init__(self, double x, double y, double z):
         self.data.x = x
         self.data.y = y
         self.data.z = z

     cdef vector_c get_data(vector self):
         return self.data

     def __getitem__(self, ix):
         if ix==0:
             return self.data.x
         elif ix == 1:
             return self.data.y
         elif ix == 2:
             return self.data.z
         else:
             raise "Error"
==============================


File vector_test.pyx
==============================
import vector
cimport vector

v = vector.vector(1.0, 1.0, 0.0)
cdef:
     vector.vector_c w = (v.get_data())

     double x = v[0]
     double xx = w.x

print x
print xx
==============================



The first two files can be converted to c-code by issuing "cython 
vector.pyx". When running "cython vector_test.pyx", I get the error message

=============================================
Error converting Pyrex file to C:
------------------------------------------------------------
...
import vector
cimport vector

v = vector.vector(1.0, 1.0, 0.0)
cdef:
     vector.vector_c w = (v.get_data())
                                   ^
------------------------------------------------------------

/home/johan/Work/Elasticity/Program/Python/vector_test.pyx:6:35: Cannot 
convert Python object to 'vector_c'
=============================================

I am surprised by this message, as I think that no conversion should be 
necessary. When I look at the previously generated vector.c I see that 
the function get_data should return a pure c struct and not a PyObject.

Grateful for any help.

/ johan

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

Reply via email to