Skip to content

Commit

Permalink
test_with_boost_python: test_python_multiple_inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jul 26, 2023
1 parent f688c13 commit 59ca2cb
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 0 deletions.
11 changes: 11 additions & 0 deletions test_with_boost_python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.5)

find_package(PythonLibs REQUIRED)
find_package(Boost COMPONENTS python REQUIRED)

# Without this, any build libraries automatically have names "lib{x}.so"
set(CMAKE_SHARED_MODULE_PREFIX "")

add_library(test_python_multiple_inheritance MODULE test_python_multiple_inheritance.cpp)
target_link_libraries(test_python_multiple_inheritance ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
target_include_directories(test_python_multiple_inheritance PRIVATE ${PYTHON_INCLUDE_DIRS})
3 changes: 3 additions & 0 deletions test_with_boost_python/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake -S . -B build -DCMAKE_VERBOSE_MAKEFILE=ON
(cd build; make)
pytest -vv
46 changes: 46 additions & 0 deletions test_with_boost_python/test_python_multiple_inheritance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <boost/python.hpp>

namespace test_python_multiple_inheritance {

// Copied from:
// https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python_multiple_inheritance.h

struct CppBase {
explicit CppBase(int value) : base_value(value) {}
int get_base_value() const { return base_value; }
void reset_base_value(int new_value) { base_value = new_value; }

private:
int base_value;
};

struct CppDrvd : CppBase {
explicit CppDrvd(int value) : CppBase(value), drvd_value(value * 3) {}
int get_drvd_value() const { return drvd_value; }
void reset_drvd_value(int new_value) { drvd_value = new_value; }

int get_base_value_from_drvd() const { return get_base_value(); }
void reset_base_value_from_drvd(int new_value) { reset_base_value(new_value); }

private:
int drvd_value;
};

} // namespace test_python_multiple_inheritance

BOOST_PYTHON_MODULE(test_python_multiple_inheritance) {
using namespace test_python_multiple_inheritance;
namespace py = boost::python;

py::class_<CppBase>("CppBase", py::no_init)
.def(py::init<int>())
.def("get_base_value", &CppBase::get_base_value)
.def("reset_base_value", &CppBase::reset_base_value);

py::class_<CppDrvd, py::bases<CppBase>>("CppDrvd", py::no_init)
.def(py::init<int>())
.def("get_drvd_value", &CppDrvd::get_drvd_value)
.def("reset_drvd_value", &CppDrvd::reset_drvd_value)
.def("get_base_value_from_drvd", &CppDrvd::get_base_value_from_drvd)
.def("reset_base_value_from_drvd", &CppDrvd::reset_base_value_from_drvd);
}
37 changes: 37 additions & 0 deletions test_with_boost_python/test_python_multiple_inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Adapted from:
# https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python/python_multiple_inheritance_test.py

from build import test_python_multiple_inheritance as m


class PC(m.CppBase):
pass


class PPCCInit(PC, m.CppDrvd):
def __init__(self, value):
PC.__init__(self, value)
m.CppDrvd.__init__(self, value + 1)


def test_PC():
d = PC(11)
assert d.get_base_value() == 11
d.reset_base_value(13)
assert d.get_base_value() == 13


def test_PPCCInit():
d = PPCCInit(11)
assert d.get_drvd_value() == 36
d.reset_drvd_value(55)
assert d.get_drvd_value() == 55

assert d.get_base_value() == 12
assert d.get_base_value_from_drvd() == 12
d.reset_base_value(20)
assert d.get_base_value() == 20
assert d.get_base_value_from_drvd() == 20
d.reset_base_value_from_drvd(30)
assert d.get_base_value() == 30
assert d.get_base_value_from_drvd() == 30
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.11.so (found version "3.11.4")
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.74.0/BoostConfig.cmake (found version "1.74.0") found components: python
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build
/usr/bin/cmake -S/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python -B/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build/CMakeFiles /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build//CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
make -f CMakeFiles/test_python_multiple_inheritance.dir/build.make CMakeFiles/test_python_multiple_inheritance.dir/depend
make[2]: Entering directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
cd /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build/CMakeFiles/test_python_multiple_inheritance.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
make -f CMakeFiles/test_python_multiple_inheritance.dir/build.make CMakeFiles/test_python_multiple_inheritance.dir/build
make[2]: Entering directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
[ 50%] Building CXX object CMakeFiles/test_python_multiple_inheritance.dir/test_python_multiple_inheritance.cpp.o
/usr/bin/c++ -DBOOST_ALL_NO_LIB -DBOOST_PYTHON_DYN_LINK -Dtest_python_multiple_inheritance_EXPORTS -I/usr/include/python3.11 -fPIC -MD -MT CMakeFiles/test_python_multiple_inheritance.dir/test_python_multiple_inheritance.cpp.o -MF CMakeFiles/test_python_multiple_inheritance.dir/test_python_multiple_inheritance.cpp.o.d -o CMakeFiles/test_python_multiple_inheritance.dir/test_python_multiple_inheritance.cpp.o -c /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/test_python_multiple_inheritance.cpp
[100%] Linking CXX shared module test_python_multiple_inheritance.so
/usr/bin/cmake -E cmake_link_script CMakeFiles/test_python_multiple_inheritance.dir/link.txt --verbose=1
/usr/bin/c++ -fPIC -shared -o test_python_multiple_inheritance.so CMakeFiles/test_python_multiple_inheritance.dir/test_python_multiple_inheritance.cpp.o /usr/lib/x86_64-linux-gnu/libboost_python311.so.1.74.0 /usr/lib/x86_64-linux-gnu/libpython3.11.so
make[2]: Leaving directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
[100%] Built target test_python_multiple_inheritance
make[1]: Leaving directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
/usr/bin/cmake -E cmake_progress_start /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build/CMakeFiles 0
============================= test session starts ==============================
platform linux -- Python 3.11.4, pytest-7.2.1, pluggy-1.0.0+repack -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python
plugins: xdist-3.3.1
collecting ... collected 2 items

test_python_multiple_inheritance.py::test_PC PASSED [ 50%]
test_python_multiple_inheritance.py::test_PPCCInit PASSED [100%]

============================== 2 passed in 0.02s ===============================
58 changes: 58 additions & 0 deletions test_with_boost_python/test_python_multiple_inheritance_diffs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
https://github.com/pybind/pybind11/pull/4762/commits/bdd938ad7e0e52eb749e3e58555328f6f595d815

--- /usr/local/google/home/rwgk/forked/pybind11/tests/test_python_multiple_inheritance.cpp 2023-07-25 20:59:56.075087212 -0700
+++ ./test_python_multiple_inheritance.cpp 2023-07-25 22:03:08.582798952 -0700
@@ -1,4 +1,4 @@
-#include "pybind11_tests.h"
+#include <boost/python.hpp>

namespace test_python_multiple_inheritance {

@@ -28,15 +28,16 @@

} // namespace test_python_multiple_inheritance

-TEST_SUBMODULE(python_multiple_inheritance, m) {
+BOOST_PYTHON_MODULE(test_python_multiple_inheritance) {
using namespace test_python_multiple_inheritance;
+ namespace py = boost::python;

- py::class_<CppBase>(m, "CppBase")
+ py::class_<CppBase>("CppBase", py::no_init)
.def(py::init<int>())
.def("get_base_value", &CppBase::get_base_value)
.def("reset_base_value", &CppBase::reset_base_value);

- py::class_<CppDrvd, CppBase>(m, "CppDrvd")
+ py::class_<CppDrvd, py::bases<CppBase>>("CppDrvd", py::no_init)
.def(py::init<int>())
.def("get_drvd_value", &CppDrvd::get_drvd_value)
.def("reset_drvd_value", &CppDrvd::reset_drvd_value)
--- /usr/local/google/home/rwgk/forked/pybind11/tests/test_python_multiple_inheritance.py 2023-07-25 16:09:12.790920850 -0700
+++ ./test_python_multiple_inheritance.py 2023-07-25 22:33:08.596640849 -0700
@@ -1,7 +1,7 @@
# Adapted from:
# https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python/python_multiple_inheritance_test.py

-from pybind11_tests import python_multiple_inheritance as m
+from build import test_python_multiple_inheritance as m


class PC(m.CppBase):
@@ -27,13 +27,11 @@
d.reset_drvd_value(55)
assert d.get_drvd_value() == 55

- # CppBase is initialized and used when CppBase methods are called, but
- # CppDrvd is used when CppDrvd methods are called.
- assert d.get_base_value() == 11
+ assert d.get_base_value() == 12
assert d.get_base_value_from_drvd() == 12
d.reset_base_value(20)
assert d.get_base_value() == 20
- assert d.get_base_value_from_drvd() == 12
+ assert d.get_base_value_from_drvd() == 20
d.reset_base_value_from_drvd(30)
- assert d.get_base_value() == 20
+ assert d.get_base_value() == 30
assert d.get_base_value_from_drvd() == 30

0 comments on commit 59ca2cb

Please sign in to comment.