File tree Expand file tree Collapse file tree 2 files changed +9
-2
lines changed Expand file tree Collapse file tree 2 files changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -37,13 +37,13 @@ void wrap(py::module m) {
37
37
.def (py::init<>())
38
38
.def_readwrite (" num" , &SimpleBase::num)
39
39
.def (py::pickle (
40
- [](const py::object& self) {
40
+ [](const py::object & self) {
41
41
py::dict d;
42
42
if (py::hasattr (self, " __dict__" ))
43
43
d = self.attr (" __dict__" );
44
44
return py::make_tuple (self.attr (" num" ), d);
45
45
},
46
- [](const py::tuple& t) {
46
+ [](const py::tuple & t) {
47
47
if (t.size () != 2 )
48
48
throw std::runtime_error (" Invalid state!" );
49
49
auto cpp_state = std::unique_ptr<SimpleBase>(new SimpleBaseTrampoline);
@@ -54,6 +54,9 @@ void wrap(py::module m) {
54
54
55
55
m.def (" make_SimpleCppDerivedAsBase" ,
56
56
[]() { return std::unique_ptr<SimpleBase>(new SimpleCppDerived); });
57
+ m.def (" check_dynamic_cast_SimpleCppDerived" , [](const SimpleBase *base_ptr) {
58
+ return dynamic_cast <const SimpleCppDerived *>(base_ptr) != nullptr ;
59
+ });
57
60
}
58
61
59
62
} // namespace exercise_trampoline
Original file line number Diff line number Diff line change @@ -67,6 +67,7 @@ def test_roundtrip_simple_py_derived():
67
67
68
68
def test_roundtrip_simple_cpp_derived ():
69
69
p = m .make_SimpleCppDerivedAsBase ()
70
+ assert m .check_dynamic_cast_SimpleCppDerived (p )
70
71
p .num = 404
71
72
if not env .PYPY :
72
73
# To ensure that this unit test is not accidentally invalidated.
@@ -77,3 +78,6 @@ def test_roundtrip_simple_cpp_derived():
77
78
p2 = pickle .loads (data )
78
79
assert isinstance (p2 , m .SimpleBase )
79
80
assert p2 .num == 404
81
+ # Issue #3062: pickleable base C++ classes can incur object slicing
82
+ # if derived typeid is not registered with pybind11
83
+ assert not m .check_dynamic_cast_SimpleCppDerived (p2 )
You can’t perform that action at this time.
0 commit comments