@@ -1017,11 +1017,14 @@ type_caster<T, SFINAE> &load_type(type_caster<T, SFINAE> &conv, const handle &ha
10171017 " Internal error: type_caster should only be used for C++ types" );
10181018 if (!conv.load (handle, true )) {
10191019#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1020- throw cast_error (" Unable to cast Python instance to C++ type (#define "
1021- " PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
1020+ throw cast_error (
1021+ " Unable to cast Python instance of type "
1022+ + str (type::handle_of (handle)).cast <std::string>()
1023+ + " to C++ type '?' (#define "
1024+ " PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
10221025#else
10231026 throw cast_error (" Unable to cast Python instance of type "
1024- + (std::string) str (type::handle_of (handle)) + " to C++ type '"
1027+ + str (type::handle_of (handle)). cast <std::string>( ) + " to C++ type '"
10251028 + type_id<T>() + " '" );
10261029#endif
10271030 }
@@ -1085,12 +1088,13 @@ detail::enable_if_t<!detail::move_never<T>::value, T> move(object &&obj) {
10851088 if (obj.ref_count () > 1 ) {
10861089#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
10871090 throw cast_error (
1088- " Unable to cast Python instance to C++ rvalue: instance has multiple references"
1089- " (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
1091+ " Unable to cast Python " + str (type::handle_of (obj)).cast <std::string>()
1092+ + " instance to C++ rvalue: instance has multiple references"
1093+ " (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
10901094#else
1091- throw cast_error (" Unable to move from Python " + (std::string) str ( type::handle_of (obj))
1092- + " instance to C++ " + type_id<T>()
1093- + " instance: instance has multiple references" );
1095+ throw cast_error (" Unable to move from Python "
1096+ + str ( type::handle_of (obj)). cast <std::string>() + " instance to C++ "
1097+ + type_id<T>() + " instance: instance has multiple references" );
10941098#endif
10951099 }
10961100
@@ -1195,9 +1199,10 @@ PYBIND11_NAMESPACE_END(detail)
11951199// The overloads could coexist, i.e. the #if is not strictly speaking needed,
11961200// but it is an easy minor optimization.
11971201#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1198- inline cast_error cast_error_unable_to_convert_call_arg () {
1199- return cast_error (" Unable to convert call argument to Python object (#define "
1200- " PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
1202+ inline cast_error cast_error_unable_to_convert_call_arg (const std::string &name) {
1203+ return cast_error (" Unable to convert call argument '" + name
1204+ + " ' to Python object (#define "
1205+ " PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
12011206}
12021207#else
12031208inline cast_error cast_error_unable_to_convert_call_arg (const std::string &name,
@@ -1220,7 +1225,7 @@ tuple make_tuple(Args &&...args_) {
12201225 for (size_t i = 0 ; i < args.size (); i++) {
12211226 if (!args[i]) {
12221227#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1223- throw cast_error_unable_to_convert_call_arg ();
1228+ throw cast_error_unable_to_convert_call_arg (std::to_string (i) );
12241229#else
12251230 std::array<std::string, size> argtypes{{type_id<Args>()...}};
12261231 throw cast_error_unable_to_convert_call_arg (std::to_string (i), argtypes[i]);
@@ -1510,7 +1515,7 @@ class unpacking_collector {
15101515 detail::make_caster<T>::cast (std::forward<T>(x), policy, {}));
15111516 if (!o) {
15121517#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1513- throw cast_error_unable_to_convert_call_arg ();
1518+ throw cast_error_unable_to_convert_call_arg (std::to_string (args_list. size ()) );
15141519#else
15151520 throw cast_error_unable_to_convert_call_arg (std::to_string (args_list.size ()),
15161521 type_id<T>());
@@ -1542,7 +1547,7 @@ class unpacking_collector {
15421547 }
15431548 if (!a.value ) {
15441549#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1545- throw cast_error_unable_to_convert_call_arg ();
1550+ throw cast_error_unable_to_convert_call_arg (a. name );
15461551#else
15471552 throw cast_error_unable_to_convert_call_arg (a.name , a.type );
15481553#endif
0 commit comments