File tree 1 file changed +12
-3
lines changed
1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -1020,23 +1020,32 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t
1020
1020
if (!src)
1021
1021
return false ;
1022
1022
1023
+ #if !defined(PYPY_VERSION)
1024
+ auto index_check = [](PyObject *o) { return PyIndex_Check (o); };
1025
+ #else
1026
+ // In PyPy 7.3.3, `PyIndex_Check` is implemented by calling `__index__`,
1027
+ // while CPython only considers the existence of `nb_index`/`__index__`.
1028
+ auto index_check = [](PyObject *o) { return hasattr (o, " __index__" ); };
1029
+ #endif
1030
+
1023
1031
if (std::is_floating_point<T>::value) {
1024
1032
if (convert || PyFloat_Check (src.ptr ()))
1025
1033
py_value = (py_type) PyFloat_AsDouble (src.ptr ());
1026
1034
else
1027
1035
return false ;
1028
1036
} else if (PyFloat_Check (src.ptr ())) {
1029
1037
return false ;
1030
- } else if (!convert && !PyIndex_Check (src.ptr ()) && !PYBIND11_LONG_CHECK (src.ptr ())) {
1038
+ } else if (!convert && !index_check (src.ptr ()) && !PYBIND11_LONG_CHECK (src.ptr ())) {
1031
1039
return false ;
1032
1040
} else {
1033
1041
handle obj = src;
1034
1042
#if PY_VERSION_HEX < 0x03080000
1035
1043
bool do_decref = false ;
1036
- if (PyIndex_Check (src.ptr ())) {
1044
+ if (index_check (src.ptr ())) {
1037
1045
PyObject *tmp = PyNumber_Index (src.ptr ());
1038
1046
if (!tmp) {
1039
- py_value = (py_type) -1 ;
1047
+ PyErr_Clear ();
1048
+ return false ;
1040
1049
}
1041
1050
do_decref = true ;
1042
1051
obj = tmp;
You can’t perform that action at this time.
0 commit comments