@@ -91,7 +91,8 @@ public:
9191 template <typename T_, \
9292 ::pybind11::detail::enable_if_t < \
9393 std::is_same<type, ::pybind11::detail::remove_cv_t <T_>>::value, \
94- int > = 0 > \
94+ int > \
95+ = 0 > \
9596 static ::pybind11::handle cast ( \
9697 T_ *src, ::pybind11::return_value_policy policy, ::pybind11::handle parent) { \
9798 if (!src) \
@@ -897,6 +898,10 @@ struct handle_type_name<float_> {
897898 static constexpr auto name = const_name(" float" );
898899};
899900template <>
901+ struct handle_type_name <function> {
902+ static constexpr auto name = const_name(" Callable" );
903+ };
904+ template <>
900905struct handle_type_name <none> {
901906 static constexpr auto name = const_name(" None" );
902907};
@@ -963,7 +968,7 @@ struct move_always<
963968 enable_if_t <
964969 all_of<move_is_plain_type<T>,
965970 negation<is_copy_constructible<T>>,
966- std:: is_move_constructible<T>,
971+ is_move_constructible<T>,
967972 std::is_same<decltype (std::declval<make_caster<T>>().operator T &()), T &>>::value>>
968973 : std::true_type {};
969974template <typename T, typename SFINAE = void >
@@ -974,7 +979,7 @@ struct move_if_unreferenced<
974979 enable_if_t <
975980 all_of<move_is_plain_type<T>,
976981 negation<move_always<T>>,
977- std:: is_move_constructible<T>,
982+ is_move_constructible<T>,
978983 std::is_same<decltype (std::declval<make_caster<T>>().operator T &()), T &>>::value>>
979984 : std::true_type {};
980985template <typename T>
@@ -1016,11 +1021,14 @@ type_caster<T, SFINAE> &load_type(type_caster<T, SFINAE> &conv, const handle &ha
10161021 " Internal error: type_caster should only be used for C++ types" );
10171022 if (!conv.load (handle, true )) {
10181023#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1019- throw cast_error (" Unable to cast Python instance to C++ type (#define "
1020- " PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
1024+ throw cast_error (
1025+ " Unable to cast Python instance of type "
1026+ + str (type::handle_of (handle)).cast <std::string>()
1027+ + " to C++ type '?' (#define "
1028+ " PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
10211029#else
10221030 throw cast_error (" Unable to cast Python instance of type "
1023- + (std::string) str (type::handle_of (handle)) + " to C++ type '"
1031+ + str (type::handle_of (handle)). cast <std::string>( ) + " to C++ type '"
10241032 + type_id<T>() + " '" );
10251033#endif
10261034 }
@@ -1037,7 +1045,11 @@ make_caster<T> load_type(const handle &handle) {
10371045PYBIND11_NAMESPACE_END (detail)
10381046
10391047// pytype -> C++ type
1040- template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value, int> = 0>
1048+ template <typename T,
1049+ detail::enable_if_t<!detail::is_pyobject<T>::value
1050+ && !detail::is_same_ignoring_cvref<T, PyObject *>::value,
1051+ int>
1052+ = 0>
10411053T cast(const handle &handle) {
10421054 using namespace detail ;
10431055 static_assert (!cast_is_temporary_value_reference<T>::value,
@@ -1051,6 +1063,34 @@ T cast(const handle &handle) {
10511063 return T (reinterpret_borrow<object>(handle));
10521064}
10531065
1066+ // Note that `cast<PyObject *>(obj)` increments the reference count of `obj`.
1067+ // This is necessary for the case that `obj` is a temporary, and could
1068+ // not possibly be different, given
1069+ // 1. the established convention that the passed `handle` is borrowed, and
1070+ // 2. we don't want to force all generic code using `cast<T>()` to special-case
1071+ // handling of `T` = `PyObject *` (to increment the reference count there).
1072+ // It is the responsibility of the caller to ensure that the reference count
1073+ // is decremented.
1074+ template <typename T,
1075+ typename Handle,
1076+ detail::enable_if_t <detail::is_same_ignoring_cvref<T, PyObject *>::value
1077+ && detail::is_same_ignoring_cvref<Handle, handle>::value,
1078+ int >
1079+ = 0 >
1080+ T cast (Handle &&handle) {
1081+ return handle.inc_ref ().ptr ();
1082+ }
1083+ // To optimize way an inc_ref/dec_ref cycle:
1084+ template <typename T,
1085+ typename Object,
1086+ detail::enable_if_t <detail::is_same_ignoring_cvref<T, PyObject *>::value
1087+ && detail::is_same_ignoring_cvref<Object, object>::value,
1088+ int >
1089+ = 0 >
1090+ T cast (Object &&obj) {
1091+ return obj.release ().ptr ();
1092+ }
1093+
10541094// C++ type -> py::object
10551095template <typename T, detail::enable_if_t <!detail::is_pyobject<T>::value, int > = 0 >
10561096object cast (T &&value,
@@ -1084,12 +1124,13 @@ detail::enable_if_t<!detail::move_never<T>::value, T> move(object &&obj) {
10841124 if (obj.ref_count () > 1 ) {
10851125#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
10861126 throw cast_error (
1087- " Unable to cast Python instance to C++ rvalue: instance has multiple references"
1088- " (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
1127+ " Unable to cast Python " + str (type::handle_of (obj)).cast <std::string>()
1128+ + " instance to C++ rvalue: instance has multiple references"
1129+ " (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
10891130#else
1090- throw cast_error (" Unable to move from Python " + (std::string) str ( type::handle_of (obj))
1091- + " instance to C++ " + type_id<T>()
1092- + " instance: instance has multiple references" );
1131+ throw cast_error (" Unable to move from Python "
1132+ + str ( type::handle_of (obj)). cast <std::string>() + " instance to C++ "
1133+ + type_id<T>() + " instance: instance has multiple references" );
10931134#endif
10941135 }
10951136
@@ -1194,9 +1235,10 @@ PYBIND11_NAMESPACE_END(detail)
11941235// The overloads could coexist, i.e. the #if is not strictly speaking needed,
11951236// but it is an easy minor optimization.
11961237#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1197- inline cast_error cast_error_unable_to_convert_call_arg () {
1198- return cast_error (" Unable to convert call argument to Python object (#define "
1199- " PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
1238+ inline cast_error cast_error_unable_to_convert_call_arg (const std::string &name) {
1239+ return cast_error (" Unable to convert call argument '" + name
1240+ + " ' to Python object (#define "
1241+ " PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
12001242}
12011243#else
12021244inline cast_error cast_error_unable_to_convert_call_arg (const std::string &name,
@@ -1219,7 +1261,7 @@ tuple make_tuple(Args &&...args_) {
12191261 for (size_t i = 0 ; i < args.size (); i++) {
12201262 if (!args[i]) {
12211263#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1222- throw cast_error_unable_to_convert_call_arg ();
1264+ throw cast_error_unable_to_convert_call_arg (std::to_string (i) );
12231265#else
12241266 std::array<std::string, size> argtypes{{type_id<Args>()...}};
12251267 throw cast_error_unable_to_convert_call_arg (std::to_string (i), argtypes[i]);
@@ -1339,7 +1381,15 @@ inline namespace literals {
13391381/* * \rst
13401382 String literal version of `arg`
13411383 \endrst */
1342- constexpr arg operator " " _a(const char *name, size_t ) { return arg (name); }
1384+ constexpr arg
1385+ #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5
1386+ operator " " _a // gcc 4.8.5 insists on having a space (hard error).
1387+ #else
1388+ operator " " _a // clang 17 generates a deprecation warning if there is a space.
1389+ #endif
1390+ (const char *name, size_t ) {
1391+ return arg (name);
1392+ }
13431393} // namespace literals
13441394
13451395PYBIND11_NAMESPACE_BEGIN (detail)
@@ -1509,7 +1559,7 @@ class unpacking_collector {
15091559 detail::make_caster<T>::cast (std::forward<T>(x), policy, {}));
15101560 if (!o) {
15111561#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1512- throw cast_error_unable_to_convert_call_arg ();
1562+ throw cast_error_unable_to_convert_call_arg (std::to_string (args_list. size ()) );
15131563#else
15141564 throw cast_error_unable_to_convert_call_arg (std::to_string (args_list.size ()),
15151565 type_id<T>());
@@ -1541,7 +1591,7 @@ class unpacking_collector {
15411591 }
15421592 if (!a.value ) {
15431593#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1544- throw cast_error_unable_to_convert_call_arg ();
1594+ throw cast_error_unable_to_convert_call_arg (a. name );
15451595#else
15461596 throw cast_error_unable_to_convert_call_arg (a.name , a.type );
15471597#endif
0 commit comments