Wrapping `struct`s
Sometimes it can be handy to wrap C-style structs for use with Python. These match pretty closely to Python's dataclass, which Cython recently added an implementation of in master ( https://github.com/cython/cython/pull/3400 ). Alternatively they could work as normal extension types. Curious on people's thoughts about wrapping these 🙂
Hi @jakirkham !
Yes, sure, that is definitely something that we would consider. What functionality are you expecting from autowrapping them? What can Cython do already internally?
Thanks for your reply Julianus! 😄
Maybe some kind of Python object that can easily move between Python/C with attribute access covered via propertys in Python and struct field in C.
I guess C++ works with structs as if they were C++ classes except by default things declared in them are public as opposed to private. Maybe this in some ways makes them closer to what is already done here for classes with some tweaks?
AFAIK Cython doesn't do any wrapping on its own in this area. There was some discussion in issue ( https://github.com/cython/cython/issues/3434 ) that seemed relevant.
A starting point might be extension types. For example something like this...
cdef class MyStruct:
cdef double d
cdef long i
...would get translated into something like this, which is pretty close.
struct __pyx_obj_<...>_MyStruct {
PyObject_HEAD
double d;
long i;
};
The one tricky thing with this kind of approach is this is effectively declaring a new struct though wrapped code would already define a struct. It is also adding in another member.
So maybe one could make an extension type that holds a struct. Then one would need to generate some code to add property access of the fields.
There are probably other options I'm not thinking of
Do you have any thoughts on this @jpfeuffer? 🙂
Hi! So I thought a bit more about this.
The problem is a bit, that we have no experience at all with wrapping C in autowrap (it might work to some extend). Was your question about structs in C or C-style structs without member functions in C++?
For C++ I do not yet see any benefits over just using cdef cppclass for a struct (based on the usecases we had so far) but you might have more experience.
The way autowrap wraps classes/structs is to create a Python class that has a member inst which holds a shared pointer
to the actual C++ object. Attribute access and functions are just forwarded to the underlying object then.
The nice thing about this is, that if this Python object is passed to a function, there is no overhead in copying data (if the original C++ function was expecting references or pointers).
We are also using and forwarding whatever constructor is provided in C(++). So the special init methods that python generates for dataclasses does not add any advantage either.
@jakirkham Does that help? We just recently adopted this project and we are still learning about the internals of Cython. So feel free to correct us or suggest something different.