@@ -902,8 +902,10 @@ template <typename type> class type_caster_base : public type_caster_generic {
902
902
make_copy_constructor (src), make_move_constructor (src));
903
903
}
904
904
905
- static handle cast_holder (const itype *src, const void *holder) {
905
+ static handle cast_holder (const itype *src, const void *holder, const std::type_info &holder_type ) {
906
906
auto st = src_and_type (src);
907
+ if (!same_type (*st.second ->holder_type , holder_type))
908
+ throw cast_error (std::string (" Unexpected holder type: " ) + holder_type.name () + " , expected: " + st.second ->holder_type ->name ());
907
909
return type_caster_generic::cast (
908
910
st.first , return_value_policy::take_ownership, {}, st.second ,
909
911
nullptr , nullptr , holder);
@@ -1505,7 +1507,7 @@ struct copyable_holder_caster : public type_caster_base<type> {
1505
1507
1506
1508
static handle cast (const holder_type &src, return_value_policy, handle) {
1507
1509
const auto *ptr = holder_helper<holder_type>::get (src);
1508
- return type_caster_base<type>::cast_holder (ptr, & src);
1510
+ return type_caster_base<type>::cast_holder (ptr, std::addressof ( src), typeid (holder_type) );
1509
1511
}
1510
1512
1511
1513
protected:
@@ -1517,6 +1519,8 @@ struct copyable_holder_caster : public type_caster_base<type> {
1517
1519
1518
1520
bool load_value (value_and_holder &&v_h) {
1519
1521
if (v_h.holder_constructed ()) {
1522
+ if (!same_type (*typeinfo->holder_type , typeid (holder_type)))
1523
+ throw cast_error (std::string (" Unexpected holder type: " ) + typeid (holder_type).name () + " , expected: " + typeinfo->holder_type ->name ());
1520
1524
value = v_h.value_ptr ();
1521
1525
holder = v_h.template holder <holder_type>();
1522
1526
return true ;
@@ -1563,7 +1567,7 @@ struct move_only_holder_caster {
1563
1567
1564
1568
static handle cast (holder_type &&src, return_value_policy, handle) {
1565
1569
auto *ptr = holder_helper<holder_type>::get (src);
1566
- return type_caster_base<type>::cast_holder (ptr, std::addressof (src));
1570
+ return type_caster_base<type>::cast_holder (ptr, std::addressof (src), typeid (holder_type) );
1567
1571
}
1568
1572
static constexpr auto name = type_caster_base<type>::name;
1569
1573
};
0 commit comments