File tree Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -26,11 +26,19 @@ struct SimpleBase {
26
26
// For compatibility with old clang versions:
27
27
SimpleBase () = default ;
28
28
SimpleBase (const SimpleBase &) = default ;
29
+
30
+ virtual int get_value_via_vtable () const { return 1 ; }
29
31
};
30
32
31
- struct SimpleBaseTrampoline : SimpleBase {};
33
+ struct SimpleBaseTrampoline : SimpleBase {
34
+ int get_value_via_vtable () const override {
35
+ PYBIND11_OVERRIDE (int , SimpleBase, get_value_via_vtable);
36
+ }
37
+ };
32
38
33
- struct SimpleCppDerived : SimpleBase {};
39
+ struct SimpleCppDerived : SimpleBase {
40
+ int get_value_via_vtable () const override { return 10 ; }
41
+ };
34
42
35
43
void wrap (py::module m) {
36
44
py::class_<SimpleBase, SimpleBaseTrampoline>(m, " SimpleBase" )
@@ -54,6 +62,9 @@ void wrap(py::module m) {
54
62
55
63
m.def (" make_SimpleCppDerivedAsBase" ,
56
64
[]() { return std::unique_ptr<SimpleBase>(new SimpleCppDerived); });
65
+
66
+ m.def (" get_value_via_vtable_cpp" ,
67
+ [](const SimpleBase& obj) { return obj.get_value_via_vtable (); });
57
68
}
58
69
59
70
} // namespace exercise_trampoline
Original file line number Diff line number Diff line change @@ -51,23 +51,28 @@ def test_enum_pickle():
51
51
# exercise_trampoline
52
52
#
53
53
class SimplePyDerived (m .SimpleBase ):
54
- pass
54
+ def get_value_via_vtable (self ):
55
+ return 100
56
+
55
57
56
58
57
59
def test_roundtrip_simple_py_derived ():
58
60
p = SimplePyDerived ()
59
61
p .num = 202
60
62
p .stored_in_dict = 303
63
+ assert m .get_value_via_vtable_cpp (p ) == 100
61
64
data = pickle .dumps (p , pickle .HIGHEST_PROTOCOL )
62
65
p2 = pickle .loads (data )
63
66
assert isinstance (p2 , SimplePyDerived )
64
67
assert p2 .num == 202
65
68
assert p2 .stored_in_dict == 303
69
+ assert m .get_value_via_vtable_cpp (p2 ) == 100
66
70
67
71
68
72
def test_roundtrip_simple_cpp_derived ():
69
73
p = m .make_SimpleCppDerivedAsBase ()
70
74
p .num = 404
75
+ assert m .get_value_via_vtable_cpp (p ) == 10
71
76
if not env .PYPY :
72
77
# To ensure that this unit test is not accidentally invalidated.
73
78
with pytest .raises (AttributeError ):
@@ -77,3 +82,5 @@ def test_roundtrip_simple_cpp_derived():
77
82
p2 = pickle .loads (data )
78
83
assert isinstance (p2 , m .SimpleBase )
79
84
assert p2 .num == 404
85
+ # ERROR: Fails due to object slicing.
86
+ assert m .get_value_via_vtable_cpp (p2 ) == 10
You can’t perform that action at this time.
0 commit comments