Search code examples
pythoncpython-c-apipybind11abstract-base-class

Is it possible in pybind11 to use py::cast to access an abstract base class?


I have include a minimal working example below - it can be compiled using the typical pybind11 instructions (I use cmake).

I have an abstract base class, Abstract, which is pure virtual. I can easily wrap this in pybind11 using a "trampoline class" (this is well documented by pybind11).

Further, I have a concrete implementation of Abstract, ToBeWrapped, that is also wrapped using pybind11.

My issue is that I have some client code which accepts an arbitrary PyObject* (or, in the case of this example, pybind11's wrapper py::object) and expects to cast this to Abstract*.

However, as illustrated in my example, I am unable to cast the py::object to Abstract*.

I have no problem casting to ToBeWrapped* and then storing that as an Abstract*', however this would require my client code to know ahead of time what kind ofAbstract*` the python interpreter is sending, which defeats the purpose of the abstract base class.

TL;DR

Is it possible to modify this code such that the client accessMethod is able to arbitrarily handle an Abstract* passed from the python interpreter?

#include <pybind11/pybind11.h>
#include <iostream>

namespace py = pybind11;

// abstract base class - cannot be instantiated on its own
class Abstract
{
    public:
        virtual ~Abstract() = 0;
        virtual std::string print() const = 0;
};

Abstract::~Abstract(){}

// concrete implementation of Abstract
class ToBeWrapped : public Abstract
{
    public:
        ToBeWrapped(const std::string& msg = "heh?")
            : myMessage(msg){};
        std::string print() const override
        {
            return myMessage;
        }

    private:
        const std::string myMessage;
};

// We need a trampoline class in order to wrap this with pybind11
class AbstractPy : public Abstract
{
    public:
        using Abstract::Abstract;

        std::string print() const override
        {
            PYBIND11_OVERLOAD_PURE(
                std::string, // return type
                Abstract, // parent class
                print, // name of the function
                // arguments (if any)
            );
        }
};

// I have client code that accepts a raw PyObject* - this client code base implements its
// own python interpreter, and calls this "accessMethod" expecting to convert the python
// object to its c++ type.
//
// Rather than mocking up the raw PyObject* method (which would be trivial) I elected to
// keep this minimal example 100% pybind11
void accessMethod(py::object obj)
{
    // runtime error: py::cast_error
    //Abstract* casted = obj.cast<Abstract*>();

    // this works
    Abstract* casted = obj.cast<ToBeWrapped*>();
}

PYBIND11_MODULE(PyMod, m)
{
    m.doc() = R"pbdoc(
    This is a python module
    )pbdoc";

    py::class_<Abstract, AbstractPy>(m, "Abstract")
        .def("print", &Abstract::print)
    ;

    py::class_<ToBeWrapped>(m, "WrappedClass")
        .def(py::init<const std::string&>())
    ;
    m.def("access", &accessMethod, "This method will attempt to access the wrapped type");
}

Solution

  • You need to declare the hierarchy relationship, so this:

    py::class_<ToBeWrapped>(m, "WrappedClass")
    

    should be:

    py::class_<ToBeWrapped, Abstract>(m, "WrappedClass")