Hello,

I've started to use Cython just yesterday, and I'm facing some 
difficulties ;). I'm sorry to ask this on the dev list, but I didn't 
found a user specific one...

I have a struct-dense C source code that defines the PDU of a network 
protocol and functions to read/write from a buffer struct.

First, I don't know if I'm doing this the right way, but I had to define 
by hand all my structs again on Cython to use them. I think it's the way 
to do it, but I did feel like repeating myself over and over ;). Is 
there any tool to create mappings like these automatically? (and handle 
common cases like wrapping structs with inner structs, uint8_t *, 
defining macros like #define VAL 1 as dummy enums, etc.). I saw some for 
C++ (using gcc-xml) but I'm stuck with pure C. My main objective on 
doing these bindings are to reuse the C code, having an efficient 
implementation is secondary.

Second, at this extremely un-proficient time, for each of the structs on 
my pdu.pyx I have something like this:

cdef class Msg1(Message):
    cdef public ... (one declaration for each field present in the struct)
    def __init__(self, ... (one keyword parameter for each field 
provided by the struct):
       self.fieldN = ... (this repeated for every field)
    cdef void from_c(self, msg1_c_struct *msg1):
       self.fieldN = msg1.fieldN (repeated for every field, with some 
additional mappings in case the field is a struct)

And then I have a message parser that uses the C functions to read the 
PDU and wrap it on Msg1:

class Parser:
    def read_pdu(self, str_data):
       cdef msg_struct msg
       ....
       pdu_read(data, &msg)
       ....
       if msg.header.type == MSG_1_TYPE:
          return Msg1().from_c(&msg.data.msg_1)

Where msg.data.msg_1 is of type msg1_c_struct.

On that code, I get an error message saying that: 'Cannot convert 
'msg1_c_struct *' to Python object' where I do the Msg1().from_c.

I don't understand why I'm getting the error. If I call a cdef function 
instead of a cdef method, it works OK; but shouldn't I be able to call a 
cdef method and passing a C type instead of a Python type? Otherwise, I 
will need to add an external cdef function for each class to create the 
object from the struct... I must be doing something wrong :).

Thanks, and sorry for the basic questions!

Best regards,
Santiago.

_______________________________________________
Cython-dev mailing list
Cython-dev@codespeak.net
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to