Pass thunk-corrected pointer to loaded_as_unique_ptr(). With this all test_class_sh_mi_thunks tests pass.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2024-07-07 07:34:00 -07:00
parent 103666ad40
commit 042ea81fbe
2 changed files with 25 additions and 13 deletions

View File

@ -1027,9 +1027,10 @@ public:
if (typeinfo->default_holder) { if (typeinfo->default_holder) {
sh_load_helper.loaded_v_h = v_h; sh_load_helper.loaded_v_h = v_h;
sh_load_helper.loaded_v_h.type = get_type_info(typeid(type)); sh_load_helper.loaded_v_h.type = get_type_info(typeid(type));
type_caster_generic::load_value(std::move(v_h));
return; return;
} }
throw std::runtime_error("BAKEIN_WIP: What is the best behavior here?"); throw std::runtime_error("BAKEIN_WIP: What is the best behavior here (load_value)?");
} }
template <typename> template <typename>
@ -1037,11 +1038,29 @@ public:
explicit operator std::unique_ptr<type, deleter>() { explicit operator std::unique_ptr<type, deleter>() {
if (typeinfo->default_holder) { if (typeinfo->default_holder) {
return sh_load_helper.template loaded_as_unique_ptr<deleter>(); return sh_load_helper.template loaded_as_unique_ptr<deleter>(value);
} }
pybind11_fail("Passing std::unique_ptr from Python to C++ requires smart_holder."); pybind11_fail("Passing std::unique_ptr from Python to C++ requires smart_holder.");
} }
bool try_implicit_casts(handle src, bool convert) {
for (auto &cast : typeinfo->implicit_casts) {
move_only_holder_caster sub_caster(*cast.first);
if (sub_caster.load(src, convert)) {
value = cast.second(sub_caster.value);
if (typeinfo->default_holder) {
// BAKEIN_WIP: Copy pointer only?
sh_load_helper.loaded_v_h = sub_caster.sh_load_helper.loaded_v_h;
} else {
throw std::runtime_error(
"BAKEIN_WIP: What is the best behavior here (try_implicit_casts)?");
}
return true;
}
}
return false;
}
static bool try_direct_conversions(handle) { return false; } static bool try_direct_conversions(handle) { return false; }
smart_holder_type_caster_support::load_helper<remove_cv_t<type>> sh_load_helper; // Const2Mutbl smart_holder_type_caster_support::load_helper<remove_cv_t<type>> sh_load_helper; // Const2Mutbl

View File

@ -887,7 +887,8 @@ struct load_helper : value_and_holder_helper {
} }
template <typename D> template <typename D>
std::unique_ptr<T, D> loaded_as_unique_ptr(const char *context = "loaded_as_unique_ptr") { std::unique_ptr<T, D> loaded_as_unique_ptr(void *raw_void_ptr,
const char *context = "loaded_as_unique_ptr") {
if (!have_holder()) { if (!have_holder()) {
return unique_with_deleter<T, D>(nullptr, std::unique_ptr<D>()); return unique_with_deleter<T, D>(nullptr, std::unique_ptr<D>());
} }
@ -896,17 +897,8 @@ struct load_helper : value_and_holder_helper {
holder().ensure_is_not_disowned(context); holder().ensure_is_not_disowned(context);
holder().template ensure_compatible_rtti_uqp_del<T, D>(context); holder().template ensure_compatible_rtti_uqp_del<T, D>(context);
holder().ensure_use_count_1(context); holder().ensure_use_count_1(context);
auto raw_void_ptr = holder().template as_raw_ptr_unowned<void>();
void *value_void_ptr = loaded_v_h.value_ptr(); T *raw_type_ptr = static_cast<T *>(raw_void_ptr);
if (value_void_ptr != raw_void_ptr) {
pybind11_fail("smart_holder_type_casters: loaded_as_unique_ptr failure:"
" value_void_ptr != raw_void_ptr");
}
// SMART_HOLDER_WIP: MISSING: Safety checks for type conversions
// (T must be polymorphic or meet certain other conditions).
T *raw_type_ptr = convert_type(raw_void_ptr);
auto *self_life_support auto *self_life_support
= dynamic_raw_ptr_cast_if_possible<trampoline_self_life_support>(raw_type_ptr); = dynamic_raw_ptr_cast_if_possible<trampoline_self_life_support>(raw_type_ptr);
@ -943,6 +935,7 @@ struct load_helper : value_and_holder_helper {
if (self_life_support != nullptr) { if (self_life_support != nullptr) {
self_life_support->activate_life_support(loaded_v_h); self_life_support->activate_life_support(loaded_v_h);
} else { } else {
void *value_void_ptr = loaded_v_h.value_ptr();
loaded_v_h.value_ptr() = nullptr; loaded_v_h.value_ptr() = nullptr;
deregister_instance(loaded_v_h.inst, value_void_ptr, loaded_v_h.type); deregister_instance(loaded_v_h.inst, value_void_ptr, loaded_v_h.type);
} }