From eb09c6c1b978208ceee40f05bbe75491b6ff8ad6 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 27 Jul 2023 21:27:44 -0700 Subject: [PATCH] One way to deal with the order dependency issue. This is not the best way, more like a proof of concept. --- include/pybind11/detail/type_caster_base.h | 27 +++++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 16387506cf..a6a8e968ca 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -713,13 +713,28 @@ class type_caster_generic { // if we can find an exact match (or, for a simple C++ type, an inherited match); if // so, we can safely reinterpret_cast to the relevant pointer. if (bases.size() > 1) { - for (auto *base : bases) { - if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type) - : base->type == typeinfo->type) { - this_.load_value( - reinterpret_cast(src.ptr())->get_value_and_holder(base)); - return true; + type_info *best_base = nullptr; + if (no_cpp_mi) { + for (auto *base : bases) { + if (PyType_IsSubtype(base->type, typeinfo->type)) { + if (best_base == nullptr + || PyType_IsSubtype(base->type, best_base->type)) { + best_base = base; + } + } } + } else { + for (auto *base : bases) { + if (base->type == typeinfo->type) { + best_base = base; + break; + } + } + } + if (best_base != nullptr) { + this_.load_value( + reinterpret_cast(src.ptr())->get_value_and_holder(best_base)); + return true; } }