question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto-generation of wrappers for C structs/C++ classes

See original GitHub issue

cdef extern gives Cython access to existing C/C++ libraries. To provide Python-level access generally requires the user to write a wrapper in Cython. For functions and enums Cython can automatically do this by defining them as cpdef. For C structs and C++ classes that feature doesn’t currently exist (although Cython does generate a C struct <-> dict conversion).

Cython has most of the information to generate a basic wrapper - it know what attributes and functions the type has and whether they’re convertable to a Python representation. Therefore it seems reasonable for Cython to create this wrapper rather than forcing the user to do it (which is largely just duplicating the existing definition in an extension class, forwarding to self.obj).

This issue suggests adding that feature.

Link to cython-devel thread.

A few notes:

  • Syntax is up for debate. Some suggestions
    • cython.autowrap[c_type, **keyword_options] - probably rejected because the square brackets make it less easily translated into pure-python mode.
    • cython.autowrap(c_type, **keyword_options)
    • @cclass decorator on struct/c++ class. Disadvantage is that it’d hide the name of the underlying C type (which would be inconvenient for writing Cython code around it). One option might be that @cython.cclass hides the name but it could also be used as a call to give a different name, e.g. ext_class_wrapper = cython.cclass(c_type) ctypedef cython.cclass(c_type) ext_class_wrapper
    • cpdef for consistency with automatic wrapping of functions/structs. Disadvantage is that it hides the name and there isn’t an easy workaround.
  • It isn’t important to handle everything. The user can always inherit from the created extension type to define anything that’s missed.
  • How to handle C++ functions that return references is always tricky (because the chances are something owns it, but who knows what). I’d propose not to handle this, and only deal with “return by value”.
  • I’m not proposing to use these types for automatic coercion between C/C++ objects and Python objects. It’d be an “opt-in” feature.
    • partly not to break existing struct<->dict conversion and vector<->list (etc.) conversions.
    • partly because it’s probably possible to end up with the generated wrappers not compiling in C++ occasionally, and having them opt-in gives the user a chance of fixing that.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
scodercommented, Jan 20, 2022

IIUC, autowrap uses Cython’s own parse tree for the generation, and then generates Cython code for the result.

That should make it possible to turn it into a tree transformation, generate Cython utility code, and then merge that into the current module. Tree transforms represent the different steps in the compiler pipeline, and the rest is a similar process to the way the Python wrapper for memory views is currently implemented.

It seems ok to leave it as an external tool for now, but I agree that it could tie more into the way Cython works internally. It could just become an optional dependency.

1reaction
poshulcommented, Jan 20, 2022

@da-woods I represent OpenMS, who have just taken over the development of Autowrap. We would be very happy to see this tool integrated more deeply into Cython.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to automatically generate the wrappers of .h classes ...
i'm working on a project where i download the memory dump from a device through an url, and then i copy the dumped...
Read more >
Create automatic C wrapper for C++ library?
translates into C functions that are invoked in my library using generated C ABI. I understand that C++ is fairly complicated language and...
Read more >
C++ Tutorial: Classes and Structs - 2020
C++ Tutorial:Class. ... The struct is a carry-over from the C. In C++, classes are generally used. ... default constructor Wrapper default constructor....
Read more >
23 SWIG and Lua
The generated C source file contains the low-level wrappers that need to be compiled ... Class data members are accessed in the same...
Read more >
Code interface packaging - MATLAB & Simulink
Select the packaging for the generated C or C++ code interface. ... data has a storage class other than Auto , global data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found