Skip to content

Inheritance with non-exposed base class #10

Closed
@YannickJadoul

Description

@YannickJadoul

I came across this project, yesterday, and I'd like to say, I really like the project, improving on Boost.Python's environment and API quirks, and I'd like to use it in my C++-to-Python wrapper.

But as I was looking, yesterday, I came across a problem. I was trying to expose a leaf class of a large hierarchy, and encountered a problem when calling a method of the base class that wasn't overridden in the exposed class.

Here is the stripped down example of what I was trying to do:

#include <iostream>

class A
{
public:
    A() : x(1) {}
    virtual void f() { std::cout << "A::f" << std::endl; }
    virtual void g() { std::cout << "A::g" << std::endl; }
            void h() { std::cout << "A::h" << std::endl; }
            void i() { std::cout << "A::i" << std::endl; }

    int x;
};

class B : public A
{
public:
    virtual void f() { std::cout << "B::f" << std::endl; }
            void h() { std::cout << "B::h" << std::endl; }
};

#include "pybind11/pybind11.h"

namespace py = pybind11;

PYBIND11_PLUGIN(test)
{
    py::module m("test");

//  py::class_<A> a(m, "A");

    py::class_<B>(m, "B"/*, a*/)
        .def(py::init<>())
        .def("f", &B::f)
        .def("g", &B::g)
        .def("h", &B::h)
        .def("i", &B::i)
        .def_readwrite("x", &B::x)
    ;
}

If I then import the resulting library in Python, and try to use it, I get the following:

>>> import test
>>> b = test.B()
>>> b.i()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Incompatible function arguments. The following argument types are supported:
    1. (A) -> None

The same goes for calling 'g' and accessing 'x'.

I tried the same with Boost.Python, and it does not seem to have this problem. (Which is a pitty, 'cause again, I really like this slim header-only library, instead of Boost ;-) )

EDIT: Oh, yes, almost forgot. If you remove the 2 comments, and expose the A class and the fact that is is B's base class, everything works as it should.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions