2929#include < vector>
3030
3131PYBIND11_NAMESPACE_BEGIN (PYBIND11_NAMESPACE)
32+
33+ PYBIND11_WARNING_DISABLE_MSVC(4127 )
34+
3235PYBIND11_NAMESPACE_BEGIN(detail)
3336
3437template <typename type, typename SFINAE = void>
@@ -88,7 +91,8 @@ public:
8891 template <typename T_, \
8992 ::pybind11::detail::enable_if_t < \
9093 std::is_same<type, ::pybind11::detail::remove_cv_t <T_>>::value, \
91- int > = 0 > \
94+ int > \
95+ = 0 > \
9296 static ::pybind11::handle cast ( \
9397 T_ *src, ::pybind11::return_value_policy policy, ::pybind11::handle parent) { \
9498 if (!src) \
@@ -389,7 +393,7 @@ struct string_caster {
389393
390394 // For UTF-8 we avoid the need for a temporary `bytes` object by using
391395 // `PyUnicode_AsUTF8AndSize`.
392- if (PYBIND11_SILENCE_MSVC_C4127 ( UTF_N == 8 ) ) {
396+ if (UTF_N == 8 ) {
393397 Py_ssize_t size = -1 ;
394398 const auto *buffer
395399 = reinterpret_cast <const CharT *>(PyUnicode_AsUTF8AndSize (load_src.ptr (), &size));
@@ -416,7 +420,7 @@ struct string_caster {
416420 = reinterpret_cast <const CharT *>(PYBIND11_BYTES_AS_STRING (utfNbytes.ptr ()));
417421 size_t length = (size_t ) PYBIND11_BYTES_SIZE (utfNbytes.ptr ()) / sizeof (CharT);
418422 // Skip BOM for UTF-16/32
419- if (PYBIND11_SILENCE_MSVC_C4127 ( UTF_N > 8 ) ) {
423+ if (UTF_N > 8 ) {
420424 buffer++;
421425 length--;
422426 }
@@ -572,7 +576,7 @@ struct type_caster<CharT, enable_if_t<is_std_char_type<CharT>::value>> {
572576 // figure out how long the first encoded character is in bytes to distinguish between these
573577 // two errors. We also allow want to allow unicode characters U+0080 through U+00FF, as
574578 // those can fit into a single char value.
575- if (PYBIND11_SILENCE_MSVC_C4127 ( StringCaster::UTF_N == 8 ) && str_len > 1 && str_len <= 4 ) {
579+ if (StringCaster::UTF_N == 8 && str_len > 1 && str_len <= 4 ) {
576580 auto v0 = static_cast <unsigned char >(value[0 ]);
577581 // low bits only: 0-127
578582 // 0b110xxxxx - start of 2-byte sequence
@@ -598,7 +602,7 @@ struct type_caster<CharT, enable_if_t<is_std_char_type<CharT>::value>> {
598602 // UTF-16 is much easier: we can only have a surrogate pair for values above U+FFFF, thus a
599603 // surrogate pair with total length 2 instantly indicates a range error (but not a "your
600604 // string was too long" error).
601- else if (PYBIND11_SILENCE_MSVC_C4127 ( StringCaster::UTF_N == 16 ) && str_len == 2 ) {
605+ else if (StringCaster::UTF_N == 16 && str_len == 2 ) {
602606 one_char = static_cast <CharT>(value[0 ]);
603607 if (one_char >= 0xD800 && one_char < 0xE000 ) {
604608 throw value_error (" Character code point not in range(0x10000)" );
@@ -960,7 +964,7 @@ struct move_always<
960964 enable_if_t <
961965 all_of<move_is_plain_type<T>,
962966 negation<is_copy_constructible<T>>,
963- std:: is_move_constructible<T>,
967+ is_move_constructible<T>,
964968 std::is_same<decltype (std::declval<make_caster<T>>().operator T &()), T &>>::value>>
965969 : std::true_type {};
966970template <typename T, typename SFINAE = void >
@@ -971,7 +975,7 @@ struct move_if_unreferenced<
971975 enable_if_t <
972976 all_of<move_is_plain_type<T>,
973977 negation<move_always<T>>,
974- std:: is_move_constructible<T>,
978+ is_move_constructible<T>,
975979 std::is_same<decltype (std::declval<make_caster<T>>().operator T &()), T &>>::value>>
976980 : std::true_type {};
977981template <typename T>
@@ -1013,11 +1017,14 @@ type_caster<T, SFINAE> &load_type(type_caster<T, SFINAE> &conv, const handle &ha
10131017 " Internal error: type_caster should only be used for C++ types" );
10141018 if (!conv.load (handle, true )) {
10151019#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1016- throw cast_error (" Unable to cast Python instance to C++ type (#define "
1017- " 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)" );
10181025#else
10191026 throw cast_error (" Unable to cast Python instance of type "
1020- + (std::string) str (type::handle_of (handle)) + " to C++ type '"
1027+ + str (type::handle_of (handle)). cast <std::string>( ) + " to C++ type '"
10211028 + type_id<T>() + " '" );
10221029#endif
10231030 }
@@ -1034,7 +1041,11 @@ make_caster<T> load_type(const handle &handle) {
10341041PYBIND11_NAMESPACE_END (detail)
10351042
10361043// pytype -> C++ type
1037- template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value, int> = 0>
1044+ template <typename T,
1045+ detail::enable_if_t<!detail::is_pyobject<T>::value
1046+ && !detail::is_same_ignoring_cvref<T, PyObject *>::value,
1047+ int>
1048+ = 0>
10381049T cast(const handle &handle) {
10391050 using namespace detail ;
10401051 static_assert (!cast_is_temporary_value_reference<T>::value,
@@ -1048,6 +1059,34 @@ T cast(const handle &handle) {
10481059 return T (reinterpret_borrow<object>(handle));
10491060}
10501061
1062+ // Note that `cast<PyObject *>(obj)` increments the reference count of `obj`.
1063+ // This is necessary for the case that `obj` is a temporary, and could
1064+ // not possibly be different, given
1065+ // 1. the established convention that the passed `handle` is borrowed, and
1066+ // 2. we don't want to force all generic code using `cast<T>()` to special-case
1067+ // handling of `T` = `PyObject *` (to increment the reference count there).
1068+ // It is the responsibility of the caller to ensure that the reference count
1069+ // is decremented.
1070+ template <typename T,
1071+ typename Handle,
1072+ detail::enable_if_t <detail::is_same_ignoring_cvref<T, PyObject *>::value
1073+ && detail::is_same_ignoring_cvref<Handle, handle>::value,
1074+ int >
1075+ = 0 >
1076+ T cast (Handle &&handle) {
1077+ return handle.inc_ref ().ptr ();
1078+ }
1079+ // To optimize way an inc_ref/dec_ref cycle:
1080+ template <typename T,
1081+ typename Object,
1082+ detail::enable_if_t <detail::is_same_ignoring_cvref<T, PyObject *>::value
1083+ && detail::is_same_ignoring_cvref<Object, object>::value,
1084+ int >
1085+ = 0 >
1086+ T cast (Object &&obj) {
1087+ return obj.release ().ptr ();
1088+ }
1089+
10511090// C++ type -> py::object
10521091template <typename T, detail::enable_if_t <!detail::is_pyobject<T>::value, int > = 0 >
10531092object cast (T &&value,
@@ -1081,12 +1120,13 @@ detail::enable_if_t<!detail::move_never<T>::value, T> move(object &&obj) {
10811120 if (obj.ref_count () > 1 ) {
10821121#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
10831122 throw cast_error (
1084- " Unable to cast Python instance to C++ rvalue: instance has multiple references"
1085- " (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
1123+ " Unable to cast Python " + str (type::handle_of (obj)).cast <std::string>()
1124+ + " instance to C++ rvalue: instance has multiple references"
1125+ " (#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
10861126#else
1087- throw cast_error (" Unable to move from Python " + (std::string) str ( type::handle_of (obj))
1088- + " instance to C++ " + type_id<T>()
1089- + " instance: instance has multiple references" );
1127+ throw cast_error (" Unable to move from Python "
1128+ + str ( type::handle_of (obj)). cast <std::string>() + " instance to C++ "
1129+ + type_id<T>() + " instance: instance has multiple references" );
10901130#endif
10911131 }
10921132
@@ -1191,9 +1231,10 @@ PYBIND11_NAMESPACE_END(detail)
11911231// The overloads could coexist, i.e. the #if is not strictly speaking needed,
11921232// but it is an easy minor optimization.
11931233#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1194- inline cast_error cast_error_unable_to_convert_call_arg () {
1195- return cast_error (" Unable to convert call argument to Python object (#define "
1196- " PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
1234+ inline cast_error cast_error_unable_to_convert_call_arg (const std::string &name) {
1235+ return cast_error (" Unable to convert call argument '" + name
1236+ + " ' to Python object (#define "
1237+ " PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" );
11971238}
11981239#else
11991240inline cast_error cast_error_unable_to_convert_call_arg (const std::string &name,
@@ -1216,7 +1257,7 @@ tuple make_tuple(Args &&...args_) {
12161257 for (size_t i = 0 ; i < args.size (); i++) {
12171258 if (!args[i]) {
12181259#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1219- throw cast_error_unable_to_convert_call_arg ();
1260+ throw cast_error_unable_to_convert_call_arg (std::to_string (i) );
12201261#else
12211262 std::array<std::string, size> argtypes{{type_id<Args>()...}};
12221263 throw cast_error_unable_to_convert_call_arg (std::to_string (i), argtypes[i]);
@@ -1506,7 +1547,7 @@ class unpacking_collector {
15061547 detail::make_caster<T>::cast (std::forward<T>(x), policy, {}));
15071548 if (!o) {
15081549#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1509- throw cast_error_unable_to_convert_call_arg ();
1550+ throw cast_error_unable_to_convert_call_arg (std::to_string (args_list. size ()) );
15101551#else
15111552 throw cast_error_unable_to_convert_call_arg (std::to_string (args_list.size ()),
15121553 type_id<T>());
@@ -1538,7 +1579,7 @@ class unpacking_collector {
15381579 }
15391580 if (!a.value ) {
15401581#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
1541- throw cast_error_unable_to_convert_call_arg ();
1582+ throw cast_error_unable_to_convert_call_arg (a. name );
15421583#else
15431584 throw cast_error_unable_to_convert_call_arg (a.name , a.type );
15441585#endif
0 commit comments