Skip to content

Commit 9eea907

Browse files
committed
pybind11 equivalent of Boost.Python test similar to reproducer under #1333
1 parent 952086d commit 9eea907

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

tests/test_cpp_base_py_derived.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// pybind11 equivalent of Boost.Python test:
2+
// https://github.com/rwgk/rwgk_tbx/blob/6c9a6d6bc72d5c1b8609724433259c5b47178680/cpp_base_py_derived_ext.cpp
3+
// See also: https://github.com/pybind/pybind11/issues/1333 (this was the starting point)
4+
5+
#include "pybind11_tests.h"
6+
7+
namespace pybind11_tests {
8+
namespace cpp_base_py_derived {
9+
10+
struct base {
11+
base() : base_num(100) {}
12+
13+
virtual int get_num() const { return base_num; }
14+
15+
virtual std::shared_ptr<base> clone() const {
16+
return std::shared_ptr<base>(new base(150));
17+
}
18+
19+
virtual ~base() = default;
20+
21+
private:
22+
explicit base(int num) : base_num(num) {}
23+
int base_num;
24+
};
25+
26+
inline int get_num(std::shared_ptr<base> b) { return b->get_num(); }
27+
28+
inline int clone_get_num(std::shared_ptr<base> b) {
29+
std::shared_ptr<base> c = b->clone();
30+
return (b->get_num() + 3) * 1000 + (c->get_num() + 7);
31+
}
32+
33+
struct base_trampoline : public base {
34+
using base::base;
35+
36+
int get_num() const override {
37+
PYBIND11_OVERRIDE(int, base, get_num);
38+
}
39+
40+
std::shared_ptr<base> clone() const override {
41+
PYBIND11_OVERRIDE(std::shared_ptr<base>, base, clone);
42+
}
43+
};
44+
45+
TEST_SUBMODULE(cpp_base_py_derived, m) {
46+
py::class_<base, base_trampoline, std::shared_ptr<base>>(m, "base")
47+
.def(py::init<>())
48+
.def("get_num", &base::get_num)
49+
.def("clone", &base::clone)
50+
;
51+
52+
m.def("get_num", get_num);
53+
m.def("clone_get_num", clone_get_num);
54+
}
55+
56+
} // namespace cpp_base_py_derived
57+
} // namespace pybind11_tests

tests/test_cpp_base_py_derived.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
# pybind11 equivalent of Boost.Python test:
3+
# https://github.com/rwgk/rwgk_tbx/blob/6c9a6d6bc72d5c1b8609724433259c5b47178680/tst_cpp_base_py_derived.py
4+
# See also: https://github.com/pybind/pybind11/issues/1333 (this was the starting point)
5+
import pytest
6+
7+
from pybind11_tests import cpp_base_py_derived as m
8+
9+
10+
class drvd(m.base):
11+
12+
def __init__(self, _num = 200):
13+
super().__init__()
14+
self._drvd_num = _num
15+
16+
def get_num(self):
17+
return self._drvd_num
18+
19+
def clone(self):
20+
return drvd(250)
21+
22+
23+
def test_base():
24+
b = m.base()
25+
assert b.get_num() == 100
26+
m.get_num(b) == 100
27+
bc = b.clone()
28+
assert bc.get_num() == 150
29+
assert m.clone_get_num(b) == 103157
30+
31+
32+
def test_drvd():
33+
d = drvd()
34+
assert d.get_num() == 200
35+
assert m.get_num(d) == 200
36+
dc = d.clone()
37+
assert dc.get_num() == 250
38+
assert m.clone_get_num(d) == 203257

0 commit comments

Comments
 (0)