pybind / pybind/pybind11

def_property and virtual inheritance

Open
#1,545 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

The below code is an adaption of #959. The base virtual base class has a member variable and the usual c++ accessor, and for now only the base A and the two classes B0_ and B1 which derive immediately from it are wrapped.

struct A {
    std::vector<int> a_;
    std::vector<int>& a(void) { return a_; }
};
struct B0_ : public virtual A {};
struct B1 : public virtual A {};
struct C : public B0_, public B1
{ C() { std::cout << "in constructor" << std::endl; } };

PYBIND11_MAKE_OPAQUE(std::vector<int>);

PYBIND11_MODULE(cpp, m)
{
  py::bind_vector<std::vector<int>>(m, "IntVector");

  py::class_<A>(m, "A")
    .def_property("a", &A::a, &A::a);
  py::class_<B0_, A>(m, "B0")
    .def(py::init<>());
  py::class_<B1, A>(m, "B1");
  // py::class_<C, B0_, B1>(m, "C");
}

Running the following in python

import cpp
G = cpp.B0()
n = len(G.a)

raises the following exception

Traceback (most recent call last):
  File "./test.py", line 6, in <module>
    print(len(G.a))
OverflowError: cannot fit 'int' into an index-sized integer

However, after uncommenting the line py::class_<C, B0_, B1>(m, "C"); to expose the final class in the diamond pattern the overflow exception is no longer raised and everything behaves as expected.

I'm using pybind11 master e7761e3 and have tested this on OSX with LLVM version 9.0.0 (clang-900.0.39.2), and also on linux with gcc version 7.3.1 20180303.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the shown C++ bindings and Python calls, focusing on py::class_, def_property, bind_vector, and the virtual-inheritance hierarchy. Trace the binding behavior for B0 when C is not exposed, then verify that len(G.a) works without requiring the C binding.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.