[Cython] Auto-generation of wrapper types

Stefan Behnel stefan_ml at behnel.de
Sun Mar 15 04:14:08 EDT 2020


da-woods schrieb am 12.03.20 um 16:11:
> The process of wrapping a C struct or C++ class in an extension type often
> has the user doing a pretty mechanical duplication of attributes/functions
> that Cython already knows about. I'm looking at doing:
> 
> cdef struct S:
>     int a
>     # etc.
> 
> then `cython.autowrap[S]` would create an extension type that wraps S by
> value.

Sounds like a good idea in general. Questions:

What makes C structs and C++ classes special enough to make this a separate
language feature? That should be part of a motivation somewhere (probably
in a ticket).

How does this relate to the support for @dataclass in GH-2903? Is the only
difference that you would write "x.member" instead of "x.structattr.member"?

https://github.com/cython/cython/issues/2903

Why "autowrap[S]" and not "autowrap(S)"? I'm asking also because there
should probably be a way for this to work in Python files, for which a
function call seems more natural.

It also feels like a @decorator would somehow fit quite well here. Even
when defined in a .pxd file, the struct would still only be exported in the
corresponding module (with the same name). Something like this:

    @cclass
    cdef struct S:
        int x

Not sure if reusing @cclass is a good idea, but it doesn't seem wrong.

Defining this in a .pxd file could then even allow cimporting modules to
understand the extension type wrapper as well, by simply reconstructing the
type representation on their side.

Stefan


More information about the cython-devel mailing list