Skip to content

Commit d14d91e

Browse files
committed
Remove handle_type_name default implementation, add explicit specializations, adjust tests.
The primary change is: ```diff template <typename T> -struct handle_type_name { - static constexpr auto name = const_name<T>(); -}; +struct handle_type_name; + ``` All other changes are adjustments to restore successful build & test.
1 parent 66ee131 commit d14d91e

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

include/pybind11/cast.h

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,9 +872,8 @@ template <typename base, typename deleter>
872872
struct is_holder_type<base, std::unique_ptr<base, deleter>> : std::true_type {};
873873

874874
template <typename T>
875-
struct handle_type_name {
876-
static constexpr auto name = const_name<T>();
877-
};
875+
struct handle_type_name;
876+
878877
template <>
879878
struct handle_type_name<object> {
880879
static constexpr auto name = const_name("object");
@@ -968,13 +967,49 @@ struct handle_type_name<type> {
968967
static constexpr auto name = const_name("type");
969968
};
970969
template <>
970+
struct handle_type_name<capsule> {
971+
static constexpr auto name = const_name("capsule");
972+
};
973+
template <>
974+
struct handle_type_name<ellipsis> {
975+
static constexpr auto name = const_name("ellipsis");
976+
};
977+
template <>
978+
struct handle_type_name<weakref> {
979+
static constexpr auto name = const_name("weakref");
980+
};
981+
template <>
971982
struct handle_type_name<args> {
972983
static constexpr auto name = const_name("*args");
973984
};
974985
template <>
975986
struct handle_type_name<kwargs> {
976987
static constexpr auto name = const_name("**kwargs");
977988
};
989+
template <>
990+
struct handle_type_name<obj_attr_accessor> {
991+
static constexpr auto name = const_name<obj_attr_accessor>();
992+
};
993+
template <>
994+
struct handle_type_name<str_attr_accessor> {
995+
static constexpr auto name = const_name<str_attr_accessor>();
996+
};
997+
template <>
998+
struct handle_type_name<item_accessor> {
999+
static constexpr auto name = const_name<item_accessor>();
1000+
};
1001+
template <>
1002+
struct handle_type_name<sequence_accessor> {
1003+
static constexpr auto name = const_name<sequence_accessor>();
1004+
};
1005+
template <>
1006+
struct handle_type_name<list_accessor> {
1007+
static constexpr auto name = const_name<list_accessor>();
1008+
};
1009+
template <>
1010+
struct handle_type_name<tuple_accessor> {
1011+
static constexpr auto name = const_name<tuple_accessor>();
1012+
};
9781013

9791014
template <typename type>
9801015
struct pyobject_caster {

include/pybind11/pybind11.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,15 @@ class cpp_function : public function {
11901190
}
11911191
};
11921192

1193+
PYBIND11_NAMESPACE_BEGIN(detail)
1194+
1195+
template <>
1196+
struct handle_type_name<cpp_function> {
1197+
static constexpr auto name = const_name("Callable");
1198+
};
1199+
1200+
PYBIND11_NAMESPACE_END(detail)
1201+
11931202
/// Wrapper for Python extension modules
11941203
class module_ : public object {
11951204
public:
@@ -2618,6 +2627,11 @@ class exception : public object {
26182627

26192628
PYBIND11_NAMESPACE_BEGIN(detail)
26202629

2630+
template <>
2631+
struct handle_type_name<exception<void>> {
2632+
static constexpr auto name = const_name("Exception");
2633+
};
2634+
26212635
// Helper function for register_exception and register_local_exception
26222636
template <typename CppException>
26232637
exception<CppException> &

include/pybind11/pytypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct sequence_item;
5959
struct list_item;
6060
struct tuple_item;
6161
} // namespace accessor_policies
62+
// PLEASE KEEP handle_type_name SPECIALIZATIONS IN SYNC.
6263
using obj_attr_accessor = accessor<accessor_policies::obj_attr>;
6364
using str_attr_accessor = accessor<accessor_policies::str_attr>;
6465
using item_accessor = accessor<accessor_policies::generic_item>;

tests/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,4 @@ def test_fn_cast_int_exception():
424424
def test_return_exception_void():
425425
with pytest.raises(TypeError) as excinfo:
426426
m.return_exception_void()
427-
assert "`exception<void>`" in str(excinfo.value)
427+
assert "Exception" in str(excinfo.value)

tests/test_pytypes.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ class float_ : public py::object {
4141
};
4242
} // namespace external
4343

44+
namespace pybind11 {
45+
namespace detail {
46+
template <>
47+
struct handle_type_name<external::float_> {
48+
static constexpr auto name = const_name("float");
49+
};
50+
} // namespace detail
51+
} // namespace pybind11
52+
4453
namespace implicit_conversion_from_0_to_handle {
4554
// Uncomment to trigger compiler error. Note: Before PR #4008 this used to compile successfully.
4655
// void expected_to_trigger_compiler_error() { py::handle(0); }

0 commit comments

Comments
 (0)