This change fixes the 6 unit tests in

bbfb0259b5/open_spiel/python/mfg/algorithms
    best_response_value_test
    distribution_test
    fictitious_play_test
    greedy_policy_test
    nash_conv_test
    policy_value_test

CAVEAT: The fix is NOT covered by pybind11 unit tests.
This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-06-25 13:25:19 -07:00 committed by Ralf W. Grosse-Kunstleve
parent 9fce2956bc
commit cb548bb3cc
2 changed files with 27 additions and 3 deletions

View File

@ -60,6 +60,13 @@ Details:
namespace pybindit { namespace pybindit {
namespace memory { namespace memory {
static constexpr bool type_has_shared_from_this(...) { return false; }
template <typename T>
static constexpr bool type_has_shared_from_this(const std::enable_shared_from_this<T> *) {
return true;
}
struct guarded_delete { struct guarded_delete {
std::weak_ptr<void> released_ptr; // Trick to keep the smart_holder memory footprint small. std::weak_ptr<void> released_ptr; // Trick to keep the smart_holder memory footprint small.
void (*del_ptr)(void *); void (*del_ptr)(void *);

View File

@ -393,6 +393,9 @@ struct smart_holder_type_caster_load {
throw_if_uninitialized_or_disowned_holder(); throw_if_uninitialized_or_disowned_holder();
holder_type &hld = holder(); holder_type &hld = holder();
hld.ensure_is_not_disowned("loaded_as_shared_ptr"); hld.ensure_is_not_disowned("loaded_as_shared_ptr");
if (hld.vptr_is_using_noop_deleter) {
throw std::runtime_error("Non-owning holder (loaded_as_shared_ptr).");
}
auto void_raw_ptr = hld.template as_raw_ptr_unowned<void>(); auto void_raw_ptr = hld.template as_raw_ptr_unowned<void>();
auto type_raw_ptr = convert_type(void_raw_ptr); auto type_raw_ptr = convert_type(void_raw_ptr);
if (hld.pointee_depends_on_holder_owner) { if (hld.pointee_depends_on_holder_owner) {
@ -407,12 +410,26 @@ struct smart_holder_type_caster_load {
vptr_gd_ptr->released_ptr = to_be_released; vptr_gd_ptr->released_ptr = to_be_released;
return to_be_released; return to_be_released;
} }
if (std::get_deleter<shared_ptr_dec_ref_deleter>(hld.vptr) != nullptr) {
// SMART_HOLDER_WIP: unit test coverage.
std::shared_ptr<void> void_shd_ptr = hld.template as_shared_ptr<void>();
return std::shared_ptr<T>(void_shd_ptr, type_raw_ptr);
}
if (!pybindit::memory::type_has_shared_from_this(type_raw_ptr)) {
// SMART_HOLDER_WIP: unit test coverage.
// SMART_HOLDER_WIP: keep weak_ref?
auto self = reinterpret_cast<PyObject *>(load_impl.loaded_v_h.inst);
Py_INCREF(self);
return std::shared_ptr<T>(type_raw_ptr, shared_ptr_dec_ref_deleter{self});
}
if (hld.vptr_is_external_shared_ptr) {
// SMART_HOLDER_WIP: unit test coverage.
pybind11_fail("smart_holder_type_casters loaded_as_shared_ptr failure: external "
"shared_ptr for type with shared_from_this.");
}
pybind11_fail("smart_holder_type_casters: loaded_as_shared_ptr failure: internal " pybind11_fail("smart_holder_type_casters: loaded_as_shared_ptr failure: internal "
"inconsistency."); "inconsistency.");
} }
if (hld.vptr_is_using_noop_deleter) {
throw std::runtime_error("Non-owning holder (loaded_as_shared_ptr).");
}
std::shared_ptr<void> void_shd_ptr = hld.template as_shared_ptr<void>(); std::shared_ptr<void> void_shd_ptr = hld.template as_shared_ptr<void>();
return std::shared_ptr<T>(void_shd_ptr, type_raw_ptr); return std::shared_ptr<T>(void_shd_ptr, type_raw_ptr);
} }