From bb6b429a2ffd55f10323b05b398f1228f1690aec Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 6 Jul 2024 15:27:04 -0700 Subject: [PATCH] MESSY but success for: test_get_vec_size_raw_shared[get_drvd_as_base0_raw_ptr-vec_size_base0_shared_ptr] scons -j 48 selected_test_cpp=test_class_sh_mi_thunks.cpp && "$(cat PYROOT)"/bin/python3 $HOME/clone/pybind11_scons/run_tests.py ../pybind11 test_class_sh_mi_thunks.py -s -vv -k 'test_get_vec_size_raw_shared[get_drvd_as_base0_raw_ptr-vec_size_base0_shared_ptr]' ``` =========================================================== test session starts ============================================================ platform linux -- Python 3.11.8, pytest-7.4.4, pluggy-1.5.0 -- /usr/bin/python3 cachedir: .pytest_cache C++ Info: 13.2.0 C++20 __pybind11_internals_v10000000_gcc_libstdcpp_cxxabi1018__ PYBIND11_SIMPLE_GIL_MANAGEMENT=False PYBIND11_NUMPY_1_ONLY=False rootdir: /usr/local/google/home/rwgk/forked/pybind11/tests configfile: pytest.ini plugins: xdist-3.5.0 collected 10 items / 9 deselected / 1 selected test_class_sh_mi_thunks.py::test_get_vec_size_raw_shared[get_drvd_as_base0_raw_ptr-vec_size_base0_shared_ptr] LOOOK A LOOOK get raw drvd 47927280 LOOOK get raw base 47927312 LOOOK B LOOOK shd load(src, convert) X LOOOK shd try_implicit_casts(src, convert) Q LOOOK shd try_implicit_casts(src, convert) R LOOOK shd load(src, convert) X LOOOK shd load_value(v_h) ENTRY LOOOK shd load_value(v_h) A LOOOK shd load_value(v_h) B LOOOK shd load(src, convert) Y 47927280 LOOOK shd try_implicit_casts(src, convert) S LOOOK shd load(src, convert) Y 47927312 LOOOK operator std::shared_ptr & A 47927312 /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/detail/../cast.h:891 LOOOK /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/detail/../detail/type_caster_base.h:838 LOOOK /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/detail/../detail/type_caster_base.h:842 LOOOK operator std::shared_ptr & B 47927312 /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/detail/../cast.h:893 LOOOK shd const Base0 *obj 47927312 LOOOK shd const auto *obj_der 47927280 LOOOK C PASSED ===================================================== 1 passed, 9 deselected in 0.01s ====================================================== ``` --- include/pybind11/cast.h | 36 ++++++++++++++++++++-- include/pybind11/detail/type_caster_base.h | 3 ++ tests/test_class_sh_mi_thunks.cpp | 8 +++++ tests/test_class_sh_mi_thunks.py | 3 ++ 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 18c96d293..ab9447d2d 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -848,11 +848,19 @@ public: using base::value; bool load(handle src, bool convert) { +#ifdef JUNK if (typeinfo->default_holder) { - return type_caster_generic::load(src, convert); + printf("\nLOOOK shd load(src, convert) A\n"); fflush(stdout); // NOLINT + bool retval = type_caster_generic::load(src, convert); + printf("\nLOOOK shd load(src, convert) B %ld\n", (long) value); fflush(stdout); // NOLINT + return retval; } - return base::template load_impl>>( +#endif + printf("\nLOOOK shd load(src, convert) X\n"); fflush(stdout); // NOLINT + bool retval = base::template load_impl>>( src, convert); + printf("\nLOOOK shd load(src, convert) Y %ld\n", (long) value); fflush(stdout); // NOLINT + return retval; } explicit operator type *() { @@ -880,7 +888,9 @@ public: explicit operator std::shared_ptr &() { if (typeinfo->default_holder) { +printf("\nLOOOK operator std::shared_ptr & A %ld %s:%d\n", (long) value, __FILE__, __LINE__); fflush(stdout); // NOLINT shared_ptr_holder = sh_load_helper.loaded_as_shared_ptr(value); +printf("\nLOOOK operator std::shared_ptr & B %ld %s:%d\n", (long) shared_ptr_holder.get(), __FILE__, __LINE__); fflush(stdout); // NOLINT } return shared_ptr_holder; } @@ -920,9 +930,12 @@ protected: } void load_value(value_and_holder &&v_h) { +printf("\nLOOOK shd load_value(v_h) ENTRY\n"); fflush(stdout); // NOLINT if (typeinfo->default_holder) { +printf("\nLOOOK shd load_value(v_h) A\n"); fflush(stdout); // NOLINT sh_load_helper.loaded_v_h = v_h; type_caster_generic::load_value(std::move(v_h)); +printf("\nLOOOK shd load_value(v_h) B\n"); fflush(stdout); // NOLINT return; } load_value_shared_ptr(v_h); @@ -938,7 +951,24 @@ protected: detail::enable_if_t::value, int> = 0> bool try_implicit_casts(handle src, bool convert) { if (typeinfo->default_holder) { - return type_caster_generic::try_implicit_casts(src, convert); +#ifdef JUNK + printf("\nLOOOK shd try_implicit_casts(src, convert) E\n"); fflush(stdout); // NOLINT + bool retval = type_caster_generic::try_implicit_casts(src, convert); + printf("\nLOOOK shd try_implicit_casts(src, convert) F\n"); fflush(stdout); // NOLINT + return retval; +#endif + printf("\nLOOOK shd try_implicit_casts(src, convert) Q\n"); fflush(stdout); // NOLINT + for (auto &cast : typeinfo->implicit_casts) { + printf("\nLOOOK shd try_implicit_casts(src, convert) R\n"); fflush(stdout); // NOLINT + copyable_holder_caster sub_caster(*cast.first); + if (sub_caster.load(src, convert)) { + printf("\nLOOOK shd try_implicit_casts(src, convert) S\n"); fflush(stdout); // NOLINT + value = cast.second(sub_caster.value); + sh_load_helper.loaded_v_h = sub_caster.sh_load_helper.loaded_v_h; + return true; + } + } + return false; } for (auto &cast : typeinfo->implicit_casts) { copyable_holder_caster sub_caster(*cast.first); diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 779bb15a4..f6cbbbce2 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -835,9 +835,11 @@ struct load_helper : value_and_holder_helper { std::shared_ptr loaded_as_shared_ptr(void *void_raw_ptr, handle responsible_parent = nullptr) const { +printf("\nLOOOK %s:%d\n", __FILE__, __LINE__); fflush(stdout); // NOLINT if (!have_holder()) { return nullptr; } +printf("\nLOOOK %s:%d\n", __FILE__, __LINE__); fflush(stdout); // NOLINT throw_if_uninitialized_or_disowned_holder(typeid(T)); holder_type &hld = holder(); hld.ensure_is_not_disowned("loaded_as_shared_ptr"); @@ -1110,6 +1112,7 @@ public: type_caster_generic sub_caster(*cast.first); if (sub_caster.load(src, convert)) { value = cast.second(sub_caster.value); + printf("\nLOOOK implicit from %ld to %ld\n", (long) sub_caster.value, (long) value); fflush(stdout); // NOLINT return true; } } diff --git a/tests/test_class_sh_mi_thunks.cpp b/tests/test_class_sh_mi_thunks.cpp index c4f430e86..a34ef308a 100644 --- a/tests/test_class_sh_mi_thunks.cpp +++ b/tests/test_class_sh_mi_thunks.cpp @@ -58,6 +58,8 @@ TEST_SUBMODULE(class_sh_mi_thunks, m) { []() { auto *drvd = new Derived; auto *base0 = dynamic_cast(drvd); +printf("\nLOOOK get raw drvd %ld\n", (long) drvd); fflush(stdout); // NOLINT +printf("\nLOOOK get raw base %ld\n", (long) base0); fflush(stdout); // NOLINT return base0; }, py::return_value_policy::take_ownership); @@ -65,6 +67,8 @@ TEST_SUBMODULE(class_sh_mi_thunks, m) { m.def("get_drvd_as_base0_shared_ptr", []() { auto drvd = std::make_shared(); auto base0 = std::dynamic_pointer_cast(drvd); +printf("\nLOOOK get shd drvd %ld\n", (long) drvd.get()); fflush(stdout); // NOLINT +printf("\nLOOOK get shd base %ld\n", (long) base0.get()); fflush(stdout); // NOLINT return base0; }); @@ -75,7 +79,9 @@ TEST_SUBMODULE(class_sh_mi_thunks, m) { }); m.def("vec_size_base0_raw_ptr", [](const Base0 *obj) { +printf("\nLOOOK raw const Base0 *obj %ld\n", (long) obj); fflush(stdout); // NOLINT const auto *obj_der = dynamic_cast(obj); +printf("\nLOOOK raw const auto *obj_der %ld\n", (long) obj_der); fflush(stdout); // NOLINT if (obj_der == nullptr) { return std::size_t(0); } @@ -83,7 +89,9 @@ TEST_SUBMODULE(class_sh_mi_thunks, m) { }); m.def("vec_size_base0_shared_ptr", [](const std::shared_ptr &obj) -> std::size_t { +printf("\nLOOOK shd const Base0 *obj %ld\n", (long) obj.get()); fflush(stdout); // NOLINT const auto obj_der = std::dynamic_pointer_cast(obj); +printf("\nLOOOK shd const auto *obj_der %ld\n", (long) obj_der.get()); fflush(stdout); // NOLINT if (!obj_der) { return std::size_t(0); } diff --git a/tests/test_class_sh_mi_thunks.py b/tests/test_class_sh_mi_thunks.py index c1c8a3043..9f1957700 100644 --- a/tests/test_class_sh_mi_thunks.py +++ b/tests/test_class_sh_mi_thunks.py @@ -30,8 +30,11 @@ def test_ptrdiff_drvd_base0(): ], ) def test_get_vec_size_raw_shared(get_fn, vec_size_fn): + print(f"\nLOOOK A", flush=True) obj = get_fn() + print(f"\nLOOOK B", flush=True) assert vec_size_fn(obj) == 5 + print(f"\nLOOOK C", flush=True) @pytest.mark.parametrize(