Wrapping Python class for C++

349 views
Skip to first unread message

Dan Hulman

unread,
Aug 31, 2021, 1:55:27 AM8/31/21
to cython-users

There's plenty of examples of wrapping a C++ class to expose it to Python, but what about the other way around? I want to accept a Python IO type and hand it over to a C++ function that writes to an std::ostream. Is there a way to do this with Cython or will I need manually wrap it by using the Python API directly?

Casper van Elteren

unread,
Sep 28, 2021, 12:27:02 PM9/28/21
to cython-users
You can checkout pybind11 or the c-python to achieve this.

Stefan Behnel

unread,
Sep 29, 2021, 7:59:43 AM9/29/21
to cython...@googlegroups.com
Dan Hulman schrieb am 30.08.21 um 21:42:
I admit that this is highly underdocumented, but you can implement C++
classes in Cython. See the examples in this file:

https://github.com/cython/cython/blob/master/tests/run/cpp_classes_def.pyx

Look for "cdef cppclass". Inside of the C++ methods, you can then use any
Cython/Python code.

Stefan

Casper van Elteren

unread,
Sep 29, 2021, 3:34:30 PM9/29/21
to cython-users
@Stefan I believe OP means exposing python classes  to C++. Wrapping a C++ class in cython is pretty well documented in the docs. Exposing a python class to C++ directly can be achieved with CPython directly.

Daniele Nicolodi

unread,
Sep 29, 2021, 4:00:41 PM9/29/21
to cython...@googlegroups.com
On 29/09/2021 20:57, Casper van Elteren wrote:
> @Stefan I believe OP means exposing python classes  to C++. Wrapping a
> C++ class in cython is pretty well documented in the docs. Exposing a
> python class to C++ directly can be achieved with CPython directly.

Stefan is saying that you can write C++ classes in Cython and that in
the class implementation you can call Python. I think that this would
solve the OP problem (if we neglect that implementing the std::ostream
interface is tedious at best).

Cheers,
Dan

>
> On Wednesday, September 29, 2021 at 1:59:43 PM UTC+2 Stefan Behnel wrote:
>
> Dan Hulman schrieb am 30.08.21 um 21:42:
> >
> > There's plenty of examples of wrapping a C++ class to expose it to
> Python,
> > but what about the other way around? I want to accept a Python IO
> type and
> > hand it over to a C++ function that writes to an std::ostream. Is
> there a
> > way to do this with Cython or will I need manually wrap it by
> using the
> > Python API directly?
>
> I admit that this is highly underdocumented, but you can implement C++
> classes in Cython. See the examples in this file:
>
> https://github.com/cython/cython/blob/master/tests/run/cpp_classes_def.pyx
> <https://github.com/cython/cython/blob/master/tests/run/cpp_classes_def.pyx>
>
>
> Look for "cdef cppclass". Inside of the C++ methods, you can then
> use any
> Cython/Python code.
>
> Stefan
>
> --
>
> ---
> 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
> <mailto:cython-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/cython-users/0d909417-3da3-494a-8667-e9cce4cffd30n%40googlegroups.com
> <https://groups.google.com/d/msgid/cython-users/0d909417-3da3-494a-8667-e9cce4cffd30n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Source

unread,
Feb 19, 2022, 1:02:53 AM2/19/22
to cython-users
You can implement C++ classes in Cython, the question is how do you call that class from C++.

I.e. test.pyx calls cpp_test() from "mytest.cpp" and void cpp_test() in mytest.cpp calls a Cython class object.

It might be possible to call static class methods, I haven't tried this, but I'm struggling with calling an object of a Cython class from a .cpp file.

I found out you a while back that you can do this for functions by declaring them as extern.

And it seems you can can technically declare Cython objects extern too, but everything I've tried so far has failed.

If I just declare the object in C++ extern testclass test, then Cython complains: error: ‘testclass’ does not name a type.
If I create a class header in C++ that mimics that in Cython and declare the object an extern testclass test I get a linker error: undefined reference to `test`.
If I declare the object in C++ extern testclass test and the same in Cython I get a redefinition error.
And you also can't declare the C++ class itself extern:  error: a storage class can only be specified for objects and functions.
Reply all
Reply to author
Forward
0 new messages