Skip to content

Commit 59ca2cb

Browse files
committed
test_with_boost_python: test_python_multiple_inheritance
pybind/pybind11#4762
1 parent f688c13 commit 59ca2cb

File tree

6 files changed

+202
-0
lines changed

6 files changed

+202
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
find_package(PythonLibs REQUIRED)
4+
find_package(Boost COMPONENTS python REQUIRED)
5+
6+
# Without this, any build libraries automatically have names "lib{x}.so"
7+
set(CMAKE_SHARED_MODULE_PREFIX "")
8+
9+
add_library(test_python_multiple_inheritance MODULE test_python_multiple_inheritance.cpp)
10+
target_link_libraries(test_python_multiple_inheritance ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
11+
target_include_directories(test_python_multiple_inheritance PRIVATE ${PYTHON_INCLUDE_DIRS})

test_with_boost_python/README.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cmake -S . -B build -DCMAKE_VERBOSE_MAKEFILE=ON
2+
(cd build; make)
3+
pytest -vv
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <boost/python.hpp>
2+
3+
namespace test_python_multiple_inheritance {
4+
5+
// Copied from:
6+
// https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python_multiple_inheritance.h
7+
8+
struct CppBase {
9+
explicit CppBase(int value) : base_value(value) {}
10+
int get_base_value() const { return base_value; }
11+
void reset_base_value(int new_value) { base_value = new_value; }
12+
13+
private:
14+
int base_value;
15+
};
16+
17+
struct CppDrvd : CppBase {
18+
explicit CppDrvd(int value) : CppBase(value), drvd_value(value * 3) {}
19+
int get_drvd_value() const { return drvd_value; }
20+
void reset_drvd_value(int new_value) { drvd_value = new_value; }
21+
22+
int get_base_value_from_drvd() const { return get_base_value(); }
23+
void reset_base_value_from_drvd(int new_value) { reset_base_value(new_value); }
24+
25+
private:
26+
int drvd_value;
27+
};
28+
29+
} // namespace test_python_multiple_inheritance
30+
31+
BOOST_PYTHON_MODULE(test_python_multiple_inheritance) {
32+
using namespace test_python_multiple_inheritance;
33+
namespace py = boost::python;
34+
35+
py::class_<CppBase>("CppBase", py::no_init)
36+
.def(py::init<int>())
37+
.def("get_base_value", &CppBase::get_base_value)
38+
.def("reset_base_value", &CppBase::reset_base_value);
39+
40+
py::class_<CppDrvd, py::bases<CppBase>>("CppDrvd", py::no_init)
41+
.def(py::init<int>())
42+
.def("get_drvd_value", &CppDrvd::get_drvd_value)
43+
.def("reset_drvd_value", &CppDrvd::reset_drvd_value)
44+
.def("get_base_value_from_drvd", &CppDrvd::get_base_value_from_drvd)
45+
.def("reset_base_value_from_drvd", &CppDrvd::reset_base_value_from_drvd);
46+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Adapted from:
2+
# https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python/python_multiple_inheritance_test.py
3+
4+
from build import test_python_multiple_inheritance as m
5+
6+
7+
class PC(m.CppBase):
8+
pass
9+
10+
11+
class PPCCInit(PC, m.CppDrvd):
12+
def __init__(self, value):
13+
PC.__init__(self, value)
14+
m.CppDrvd.__init__(self, value + 1)
15+
16+
17+
def test_PC():
18+
d = PC(11)
19+
assert d.get_base_value() == 11
20+
d.reset_base_value(13)
21+
assert d.get_base_value() == 13
22+
23+
24+
def test_PPCCInit():
25+
d = PPCCInit(11)
26+
assert d.get_drvd_value() == 36
27+
d.reset_drvd_value(55)
28+
assert d.get_drvd_value() == 55
29+
30+
assert d.get_base_value() == 12
31+
assert d.get_base_value_from_drvd() == 12
32+
d.reset_base_value(20)
33+
assert d.get_base_value() == 20
34+
assert d.get_base_value_from_drvd() == 20
35+
d.reset_base_value_from_drvd(30)
36+
assert d.get_base_value() == 30
37+
assert d.get_base_value_from_drvd() == 30
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- The C compiler identification is GNU 12.2.0
2+
-- The CXX compiler identification is GNU 12.2.0
3+
-- Detecting C compiler ABI info
4+
-- Detecting C compiler ABI info - done
5+
-- Check for working C compiler: /usr/bin/cc - skipped
6+
-- Detecting C compile features
7+
-- Detecting C compile features - done
8+
-- Detecting CXX compiler ABI info
9+
-- Detecting CXX compiler ABI info - done
10+
-- Check for working CXX compiler: /usr/bin/c++ - skipped
11+
-- Detecting CXX compile features
12+
-- Detecting CXX compile features - done
13+
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.11.so (found version "3.11.4")
14+
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.74.0/BoostConfig.cmake (found version "1.74.0") found components: python
15+
-- Configuring done
16+
-- Generating done
17+
-- Build files have been written to: /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build
18+
/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
19+
/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
20+
make -f CMakeFiles/Makefile2 all
21+
make[1]: Entering directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
22+
make -f CMakeFiles/test_python_multiple_inheritance.dir/build.make CMakeFiles/test_python_multiple_inheritance.dir/depend
23+
make[2]: Entering directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
24+
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=
25+
make[2]: Leaving directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
26+
make -f CMakeFiles/test_python_multiple_inheritance.dir/build.make CMakeFiles/test_python_multiple_inheritance.dir/build
27+
make[2]: Entering directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
28+
[ 50%] Building CXX object CMakeFiles/test_python_multiple_inheritance.dir/test_python_multiple_inheritance.cpp.o
29+
/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
30+
[100%] Linking CXX shared module test_python_multiple_inheritance.so
31+
/usr/bin/cmake -E cmake_link_script CMakeFiles/test_python_multiple_inheritance.dir/link.txt --verbose=1
32+
/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
33+
make[2]: Leaving directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
34+
[100%] Built target test_python_multiple_inheritance
35+
make[1]: Leaving directory '/usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build'
36+
/usr/bin/cmake -E cmake_progress_start /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python/build/CMakeFiles 0
37+
============================= test session starts ==============================
38+
platform linux -- Python 3.11.4, pytest-7.2.1, pluggy-1.0.0+repack -- /usr/bin/python3
39+
cachedir: .pytest_cache
40+
rootdir: /usr/local/google/home/rwgk/clone/stuff/test_with_boost_python
41+
plugins: xdist-3.3.1
42+
collecting ... collected 2 items
43+
44+
test_python_multiple_inheritance.py::test_PC PASSED [ 50%]
45+
test_python_multiple_inheritance.py::test_PPCCInit PASSED [100%]
46+
47+
============================== 2 passed in 0.02s ===============================
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
https://github.com/pybind/pybind11/pull/4762/commits/bdd938ad7e0e52eb749e3e58555328f6f595d815
2+
3+
--- /usr/local/google/home/rwgk/forked/pybind11/tests/test_python_multiple_inheritance.cpp 2023-07-25 20:59:56.075087212 -0700
4+
+++ ./test_python_multiple_inheritance.cpp 2023-07-25 22:03:08.582798952 -0700
5+
@@ -1,4 +1,4 @@
6+
-#include "pybind11_tests.h"
7+
+#include <boost/python.hpp>
8+
9+
namespace test_python_multiple_inheritance {
10+
11+
@@ -28,15 +28,16 @@
12+
13+
} // namespace test_python_multiple_inheritance
14+
15+
-TEST_SUBMODULE(python_multiple_inheritance, m) {
16+
+BOOST_PYTHON_MODULE(test_python_multiple_inheritance) {
17+
using namespace test_python_multiple_inheritance;
18+
+ namespace py = boost::python;
19+
20+
- py::class_<CppBase>(m, "CppBase")
21+
+ py::class_<CppBase>("CppBase", py::no_init)
22+
.def(py::init<int>())
23+
.def("get_base_value", &CppBase::get_base_value)
24+
.def("reset_base_value", &CppBase::reset_base_value);
25+
26+
- py::class_<CppDrvd, CppBase>(m, "CppDrvd")
27+
+ py::class_<CppDrvd, py::bases<CppBase>>("CppDrvd", py::no_init)
28+
.def(py::init<int>())
29+
.def("get_drvd_value", &CppDrvd::get_drvd_value)
30+
.def("reset_drvd_value", &CppDrvd::reset_drvd_value)
31+
--- /usr/local/google/home/rwgk/forked/pybind11/tests/test_python_multiple_inheritance.py 2023-07-25 16:09:12.790920850 -0700
32+
+++ ./test_python_multiple_inheritance.py 2023-07-25 22:33:08.596640849 -0700
33+
@@ -1,7 +1,7 @@
34+
# Adapted from:
35+
# https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python/python_multiple_inheritance_test.py
36+
37+
-from pybind11_tests import python_multiple_inheritance as m
38+
+from build import test_python_multiple_inheritance as m
39+
40+
41+
class PC(m.CppBase):
42+
@@ -27,13 +27,11 @@
43+
d.reset_drvd_value(55)
44+
assert d.get_drvd_value() == 55
45+
46+
- # CppBase is initialized and used when CppBase methods are called, but
47+
- # CppDrvd is used when CppDrvd methods are called.
48+
- assert d.get_base_value() == 11
49+
+ assert d.get_base_value() == 12
50+
assert d.get_base_value_from_drvd() == 12
51+
d.reset_base_value(20)
52+
assert d.get_base_value() == 20
53+
- assert d.get_base_value_from_drvd() == 12
54+
+ assert d.get_base_value_from_drvd() == 20
55+
d.reset_base_value_from_drvd(30)
56+
- assert d.get_base_value() == 20
57+
+ assert d.get_base_value() == 30
58+
assert d.get_base_value_from_drvd() == 30

0 commit comments

Comments
 (0)