Skip to content

Commit ef17743

Browse files
committed
Split out format_message_key_error_key_object() to reduce amount of templated code.
1 parent bd60a6a commit ef17743

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

include/pybind11/stl_bind.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -694,13 +694,9 @@ struct ItemsViewImpl : public detail::items_view {
694694
Map ↦
695695
};
696696

697-
template <typename KeyType>
698-
str format_message_key_error(const KeyType &key) {
697+
inline str format_message_key_error_key_object(handle py_key) {
699698
str message = "pybind11::bind_map key";
700-
object py_key;
701-
try {
702-
py_key = cast(key);
703-
} catch (const std::exception &) {
699+
if (!py_key) {
704700
return message;
705701
}
706702
try {
@@ -720,6 +716,18 @@ str format_message_key_error(const KeyType &key) {
720716
return message;
721717
}
722718

719+
template <typename KeyType>
720+
str format_message_key_error(const KeyType &key) {
721+
object py_key;
722+
try {
723+
py_key = cast(key);
724+
} catch (const std::exception &) {
725+
do { // Trick to avoid "empty catch" warning/error.
726+
} while (false);
727+
}
728+
return format_message_key_error_key_object(py_key);
729+
}
730+
723731
PYBIND11_NAMESPACE_END(detail)
724732

725733
template <typename Map, typename holder_type = std::unique_ptr<Map>, typename... Args>

0 commit comments

Comments
 (0)