From d14d91e02afba4046c9a899ad47a20220ac1f1a5 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 16 Dec 2023 19:06:16 -0800 Subject: [PATCH] Remove `handle_type_name` default implementation, add explicit specializations, adjust tests. The primary change is: ```diff template -struct handle_type_name { - static constexpr auto name = const_name(); -}; +struct handle_type_name; + ``` All other changes are adjustments to restore successful build & test. --- include/pybind11/cast.h | 41 ++++++++++++++++++++++++++++++++++--- include/pybind11/pybind11.h | 14 +++++++++++++ include/pybind11/pytypes.h | 1 + tests/test_exceptions.py | 2 +- tests/test_pytypes.cpp | 9 ++++++++ 5 files changed, 63 insertions(+), 4 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index cdeb4aa8b..fbccf8bf5 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -872,9 +872,8 @@ template struct is_holder_type> : std::true_type {}; template -struct handle_type_name { - static constexpr auto name = const_name(); -}; +struct handle_type_name; + template <> struct handle_type_name { static constexpr auto name = const_name("object"); @@ -968,6 +967,18 @@ struct handle_type_name { static constexpr auto name = const_name("type"); }; template <> +struct handle_type_name { + static constexpr auto name = const_name("capsule"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("ellipsis"); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name("weakref"); +}; +template <> struct handle_type_name { static constexpr auto name = const_name("*args"); }; @@ -975,6 +986,30 @@ template <> struct handle_type_name { static constexpr auto name = const_name("**kwargs"); }; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); +}; +template <> +struct handle_type_name { + static constexpr auto name = const_name(); +}; template struct pyobject_caster { diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index bc2f5924a..429d2138d 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1190,6 +1190,15 @@ protected: } }; +PYBIND11_NAMESPACE_BEGIN(detail) + +template <> +struct handle_type_name { + static constexpr auto name = const_name("Callable"); +}; + +PYBIND11_NAMESPACE_END(detail) + /// Wrapper for Python extension modules class module_ : public object { public: @@ -2618,6 +2627,11 @@ public: PYBIND11_NAMESPACE_BEGIN(detail) +template <> +struct handle_type_name> { + static constexpr auto name = const_name("Exception"); +}; + // Helper function for register_exception and register_local_exception template exception & diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index e7c7c8124..d5f6af8e0 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -59,6 +59,7 @@ struct sequence_item; struct list_item; struct tuple_item; } // namespace accessor_policies +// PLEASE KEEP handle_type_name SPECIALIZATIONS IN SYNC. using obj_attr_accessor = accessor; using str_attr_accessor = accessor; using item_accessor = accessor; diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index a0c51c564..378ec6348 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -424,4 +424,4 @@ def test_fn_cast_int_exception(): def test_return_exception_void(): with pytest.raises(TypeError) as excinfo: m.return_exception_void() - assert "`exception`" in str(excinfo.value) + assert "Exception" in str(excinfo.value) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 0cd480ebb..03d46ae98 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -41,6 +41,15 @@ class float_ : public py::object { }; } // namespace external +namespace pybind11 { +namespace detail { +template <> +struct handle_type_name { + static constexpr auto name = const_name("float"); +}; +} // namespace detail +} // namespace pybind11 + namespace implicit_conversion_from_0_to_handle { // Uncomment to trigger compiler error. Note: Before PR #4008 this used to compile successfully. // void expected_to_trigger_compiler_error() { py::handle(0); }