Exposing cython extension class to c++

152 views
Skip to first unread message

Casper van Elteren

unread,
Nov 13, 2020, 10:56:12 AM11/13/20
to cython-users

I have several cython extension classes that I wish to expose to c++. Apparantly there are some not well documented keywords such as `api` and `public` that can be used to automatically create headers for exposure.

I have wrote some trivial classes

```cython
# file: A.pyx
cdef public class A [object PyA, type PyA_t]:
    cdef public int a
    cdef public double test(self, double x):
        return x
```

that produces a header `A.h` that exposes a struct in which I can see that it exposes the variable correctly. A `vtab` is additionally created. However I am not sure how I access the class methods directly. In my simple cpp code it gives me errors when I try it:


``` cpp
//file main.cpp

#include <iostream>
#include "Python.h"
#include "A.h"
using namespace std;

int main(){
  auto b = PyA();
  b.a = 5;  //works
  double c = 1.;
  auto test = PyA();
  test->test(c);

}
```

da-woods

unread,
Nov 13, 2020, 2:56:49 PM11/13/20
to cython...@googlegroups.com
I don't think the `vtab` or the class methods in it are exported in a usable way, unfortunately. That's probably a bug/missing feature.

I think the easiest thing to do would be to create global functions that call your class methods:

cdef public double A_test(A a_instance, double x):
    return a_instance.test(x)

A simple wrapper like this will cost almost no performance.
--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/251d7ab6-de02-4e9a-ba8d-89d2e738cb02n%40googlegroups.com.


Casper van Elteren

unread,
Nov 18, 2020, 3:20:18 PM11/18/20
to cython-users
Ah ok so the feature is MIA at the moment. 
Providing bindings that way seems a bit overkill tbh. How come the vtable is not accessible in the usual way?

da-woods

unread,
Nov 19, 2020, 3:58:22 PM11/19/20
to cython...@googlegroups.com

> How come the vtable is not accessible in the usual way?

I don't think there's a good reason why it couldn't/shouldn't be accessible. I think embedding Cython into C is not a widely used feature and probably most people expose a fairly small interface (maybe mainly a "start_my_cython_routine" function).  Obviously knowing this doesn't help you...


Casper van Elteren

unread,
Nov 20, 2020, 3:57:43 AM11/20/20
to cython...@googlegroups.com
Indeed. I understand my workflow is a bit niche as the code base has undergone many changes from native python with numpy to cython, now I wish to write the rest of the codebase that interacts with these objects in c++/c. This unfortunately puts me in the position to either (a) write wrappers as proposed above or (b) rewrite the entire project in c++. I hope the cython dev team puts putting c++ class apis on their radar.

Thanks anyway ;-)

You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/sX4le0-GpQ0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/e9b5529c-3d76-829e-660b-76f089c91cc4%40d-woods.co.uk.


--

  • Checkout my website for contact info and current projects!
Reply all
Reply to author
Forward
0 new messages