On Mar 13, 2009, at 12:53 AM, Stefan Behnel wrote: > > Santiago Aguiar wrote: >> 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 ;). > > That's the way to do it, though. You only go through that once for > each > struct you use (and your code that uses the struct is likely longer > than > the struct itself), so it's not too much work. > > Note that you do not need to declare the complete struct. Just > write down > the fields you need. Cython doesn't need to know the others and the C > compiler sees the struct anyway. That also allows you to ignore any > externally referenced complex types that you do not use yourself. > > >> 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) > > Why don't you reference the struct itself here? Do you really need > these > fields to be public? Some methods for setting up common use cases > are often > a lot better than just having users set everything by hand, > especially when > there are non-trivial type conversions involved, or when there are > interdependencies between fields that are better checked for > validity. For > example, Cython cannot check arbitrary integer values for you that > the user > writes into an enum type attribute.
I would second this--you'll have to make properties for each of the fields, but your internal code will be much easier if you can just pass structs around. >> 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) > > There is some special support for struct assignments from dicts, > you can > try that. I have no idea where that's documented, though. Robert? It's not except for at http://hg.cython.org/cython-devel/file/ f2a2d6e68de2/tests/run/struct_conversion.pyx Simply put, if one has cdef struct Point: double x double y One can write "Point(x=1, y=2)" to get a literal struct, and conversion from a struct to a Python object creates a dict. I intended to write conversion the other direction too (from a dict to a struct in the obvious way) but haven't gotten around to it yet. Personally, I would write a simple script that auto-generates this kind of stuff for you in your specific case. - Robert _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
