Wrapping an abstract class

432 views
Skip to first unread message

Shriramana Sharma

unread,
May 8, 2013, 1:34:06 PM5/8/13
to cython...@googlegroups.com
The Cython docs don't say anything about wrapping C++ abstract base
classes, i.e. those with at least one member function of the type
virtual fn(args) = 0. It is desired that from Python one would create
a sub-class of such a class implementing all those methods declared
pure virtual in the base. Is this possible via Cython and is there
anything particular I should know about handling such classes?

--
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा

Robert Bradshaw

unread,
May 8, 2013, 2:57:47 PM5/8/13
to cython...@googlegroups.com
To simply wrap an abstract C++ class, you have to store a pointer
(just as you would when writing a C++ wrapper of a C++ abstract
class).

There's *experimental* support for extending C++ classes in Cython
with another C++ class:
https://github.com/cython/cython/blob/master/tests/run/cpp_classes_def.pyx
. You can't (yet?) actually extend a C++ class as a cdef class
accessible/further extendable from Python. (You can create a cdef
class that the C++ wraps and delegates all methods to manually
though.)
> --
>
> ---
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Shriramana Sharma

unread,
May 17, 2013, 11:26:47 PM5/17/13
to cython...@googlegroups.com
On Thu, May 9, 2013 at 12:27 AM, Robert Bradshaw <robe...@gmail.com> wrote:
> . You can't (yet?) actually extend a C++ class as a cdef class
> accessible/further extendable from Python. (You can create a cdef
> class that the C++ wraps and delegates all methods to manually
> though.)

Hi and thanks for your reply. Do I then understand correctly that it
is (currently) not possible to *subclass* a C++ class within Cython,
and the workaround is to write a cdef class that *wraps* all its
members?

Robert Bradshaw

unread,
May 19, 2013, 2:51:24 AM5/19/13
to cython...@googlegroups.com
On Fri, May 17, 2013 at 8:26 PM, Shriramana Sharma <sam...@gmail.com> wrote:
> On Thu, May 9, 2013 at 12:27 AM, Robert Bradshaw <robe...@gmail.com> wrote:
>> . You can't (yet?) actually extend a C++ class as a cdef class
>> accessible/further extendable from Python. (You can create a cdef
>> class that the C++ wraps and delegates all methods to manually
>> though.)
>
> Hi and thanks for your reply. Do I then understand correctly that it
> is (currently) not possible to *subclass* a C++ class within Cython,

You can, see the above example, but you end up with another C++ class
that's not accessible from Python.

> and the workaround is to write a cdef class that *wraps* all its members?

If you want it to be visible from Python, yes. Cython is not a
wrapper-generator (though a great too to build one on).

- Robert
Reply all
Reply to author
Forward
0 new messages