cpdef struct -- get/set elements of a struct at Python-level and C-level

171 views
Skip to first unread message

Nathann Cohen

unread,
Jan 19, 2013, 9:37:53 AM1/19/13
to cython...@googlegroups.com
Helloooooooooo everybody !!

I have a problem. I have a veeeeeeeery big C struct that I would need to use at both Python level and C level, and it has 1000 differents fields.

Right now, it is a "ctypedef struct" in a .pxd file, but the only way I have in mind to access those fields at Python level would be to create a class for each one of my struct, then define for each field a get function and a set function.

This looks like work that should not have to be done, soo.... Would there be a magical way to get this automatically in Cython ? 

Thank you very much :-)

Nathann

Robert Bradshaw

unread,
Jan 21, 2013, 11:00:07 PM1/21/13
to cython...@googlegroups.com
There isn't now, but it'd be really nice. In the meantime, I bet you
could us ctypes to access the struct at the Python level, and then get
a pointer to the underlying data in Cython (haven't looked into this
myself, but it's what I would try if I weren't going to write a
code-generator or modify Cython itself).

- Robert

Chris Barker - NOAA Federal

unread,
Jan 22, 2013, 1:40:53 PM1/22/13
to cython...@googlegroups.com
On Mon, Jan 21, 2013 at 8:00 PM, Robert Bradshaw <robe...@gmail.com> wrote:

>> in mind to access those fields at Python level would be to create a class
>> for each one of my struct, then define for each field a get function and a
>> set function.
>>
>> This looks like work that should not have to be done, soo.... Would there be
>> a magical way to get this automatically in Cython ?

> There isn't now, but it'd be really nice.

indeed.

> if I weren't going to write a
> code-generator or modify Cython itself).

I'd probably write a code generator for this -- particularly if you
have more than a couple of these monster structs, and/or they ar under
active development and changing a lot.

It should be pretty straightforward (at least for your specific use),
and would be generally useful.

-Chris



--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

Nathann Cohen

unread,
Jan 23, 2013, 5:42:01 AM1/23/13
to cython...@googlegroups.com
Hellooooooooooo !!!


> I'd probably write a code generator for this -- particularly if you
> have more than a couple of these monster structs, and/or they ar under
> active development and changing a lot.
>
> It should be pretty straightforward (at least for your specific use),
> and would be generally useful.

Well, I've been wrapping a lot of C struct this way, and it'd be real great to be able to do it autmatically.... Thank you very much for agreeing on that, and if you happen to write it eventually, could I ask you to post a message on this thead ? :-)

Thaaaaaank you very much, and for Cython too :-)

Nathann

Stefan Behnel

unread,
Jan 23, 2013, 8:32:57 AM1/23/13
to cython...@googlegroups.com
Nathann Cohen, 23.01.2013 11:42:
Well, you can help by coming up with test code and adding a ticket to the
tracker that describes your feature request:

http://trac.cython.org/cython_trac/report/1?asc=0&sort=ticket

For writing test modules, read this:

http://wiki.cython.org/HackerGuide#Thetestsuite

Stefan

Reply all
Reply to author
Forward
0 new messages