You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ran into an issue the other day and py::multiple_inheritance solved it. However, based on the documentation I could find, it doesn't look like it should be needed in this scenario (since there is only a single chain of inheritance). However, adding it definitely solves the problem.
I think one of three things is going on:
I have misread or misunderstood the documentation.
This is case where it is needed, and the documentation should be updated.
This shouldn't be needed, but there is a bug somewhere else, and this is fixing it.
If it's 1, please let me know and I can just close this. If it's 2 or 3, I'm happy to help dig in and figure out what is going on and/or submit PRs to help update documentation. I just want to make sure it's not 1 before I spend any time on 2 or 3.
The only documentation I could find on this is here in the changelog, which states it should only be needed "when C++ bases are hidden from pybind11." Maybe virtual inheritance counts as being hidden from pybind11, but at least to me, that's not obvious, as the virtual base class is included when creating the derived class in pybind11.
I've whittled this down to a simplest working case. Here are the relevant files:
fromtest_PyimportVirtualBasevb=VirtualBase(3)
# From member accessprint("From member access:")
print(f"x: {vb.x}")
print(f"y: {vb.y}")
print("From methods:")
print("x: ",end="", flush=True)
vb.printX()
print("y: ",end="", flush=True)
vb.printY()
CMakeLists.txt
cmake_minimum_required(VERSION3.25)
project(example)
# TODO: Change for releaseadd_compile_options(-Wall-Werror)
# Contorl whether we build with shared libraries or static librariesoption(BUILD_SHARED_LIBS"Build using shared libraries"OFF)
# If not building with shared libs, set POSITION_INDEPENDENT_CODE # so that -fPIC gets used. This will# allow linking the static libraries into the shared pybind11 libraries.if(NOTBUILD_SHARED_LIBS)
set(CMAKE_POSITION_INDEPENDENT_CODEON)
endif()
# Use C++ 20set(CMAKE_CXX_STANDARD20)
# Turn on generation of compile_commands.json for LSPset(CMAKE_EXPORT_COMPILE_COMMANDSON)
# Turn on colored diagnosticsset(CMAKE_COLOR_DIAGNOSTICSON)
# Find Python in the systemfind_package(Python3REQUIREDCOMPONENTSInterpreterDevelopment)
# Find pybind11 in the system# This is needed for CMake < 3.27. # After Cmake 3.27+, can remove setting PYBIND11_FINDPYTHON.set(PYBIND11_FINDPYTHONON)
find_package(pybind113.0REQUIREDCONFIG)
# Create the bf libraryadd_library(testtest.cc)
# Create the BF.py Python filepybind11_add_module(test_Pytest_Py.cc)
target_link_libraries(test_PyPRIVATEtest)
install(TARGETStesttest_PyDESTINATION.)
After building and installing, if I run python test.py I see the following output:
From member access:
x: 3
y: 3
From methods:
x: 6
y: 3
which is incorrect. I should see x=6 and y=6 in both cases. However, if I add py::multiple_inheritance() to the VirtualBase in test_Py.cc so the file now reads
From member access:
x: 6
y: 3
From methods:
x: 6
y: 3
as expected. Similarly, if I make it just normal inheritance rather than virtual inheritance, I can remove the py::multiple_inheritance() and everything works as expected.
I realize the example at a glance looks strange--why would anyone ever do virtual inheritance with only a single base class and no derived classes--but that is only because I've boiled it down to this simplest working case from a much more complex example.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I ran into an issue the other day and
py::multiple_inheritance
solved it. However, based on the documentation I could find, it doesn't look like it should be needed in this scenario (since there is only a single chain of inheritance). However, adding it definitely solves the problem.I think one of three things is going on:
If it's 1, please let me know and I can just close this. If it's 2 or 3, I'm happy to help dig in and figure out what is going on and/or submit PRs to help update documentation. I just want to make sure it's not 1 before I spend any time on 2 or 3.
The only documentation I could find on this is here in the changelog, which states it should only be needed "when C++ bases are hidden from pybind11." Maybe virtual inheritance counts as being hidden from
pybind11
, but at least to me, that's not obvious, as the virtual base class is included when creating the derived class inpybind11
.I've whittled this down to a simplest working case. Here are the relevant files:
test.h
test.cc
test_Py.cc
test.py
CMakeLists.txt
I'm building and installing like this:
After building and installing, if I run
python test.py
I see the following output:which is incorrect. I should see x=6 and y=6 in both cases. However, if I add
py::multiple_inheritance()
to theVirtualBase
intest_Py.cc
so the file now readstest_Py.cc
The output of
test.py
becomesas expected. Similarly, if I make it just normal inheritance rather than virtual inheritance, I can remove the
py::multiple_inheritance()
and everything works as expected.I realize the example at a glance looks strange--why would anyone ever do virtual inheritance with only a single base class and no derived classes--but that is only because I've boiled it down to this simplest working case from a much more complex example.
Beta Was this translation helpful? Give feedback.
All reactions