diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 621557627..be9266684 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -149,39 +149,39 @@ public: } } else { #if !defined(PYPY_VERSION) - auto index_check = [](PyObject *o) { return PyIndex_Check(o); }; + auto index_check = [](PyObject *o) { return PyIndex_Check(o); }; #else - // In PyPy 7.3.3, `PyIndex_Check` is implemented by calling `__index__`, - // while CPython only considers the existence of `nb_index`/`__index__`. - auto index_check = [](PyObject *o) { return hasattr(o, "__index__"); }; + // In PyPy 7.3.3, `PyIndex_Check` is implemented by calling `__index__`, + // while CPython only considers the existence of `nb_index`/`__index__`. + auto index_check = [](PyObject *o) { return hasattr(o, "__index__"); }; #endif - if (PyFloat_Check(src.ptr()) - || (!convert && !PYBIND11_LONG_CHECK(src.ptr()) && !index_check(src.ptr()))) { - return false; - } - - handle src_or_index = src; - // PyPy: 7.3.7's 3.8 does not implement PyLong_*'s __index__ calls. -#if defined(PYPY_VERSION) - object index; - if (!PYBIND11_LONG_CHECK(src.ptr())) { // So: index_check(src.ptr()) - index = reinterpret_steal(PyNumber_Index(src.ptr())); - if (!index) { - PyErr_Clear(); - if (!convert) + if (PyFloat_Check(src.ptr()) + || (!convert && !PYBIND11_LONG_CHECK(src.ptr()) && !index_check(src.ptr()))) { return false; - } else { - src_or_index = index; } - } + + handle src_or_index = src; + // PyPy: 7.3.7's 3.8 does not implement PyLong_*'s __index__ calls. +#if defined(PYPY_VERSION) + object index; + if (!PYBIND11_LONG_CHECK(src.ptr())) { // So: index_check(src.ptr()) + index = reinterpret_steal(PyNumber_Index(src.ptr())); + if (!index) { + PyErr_Clear(); + if (!convert) + return false; + } else { + src_or_index = index; + } + } #endif - if PYBIND11_IF_CONSTEXPR (std::is_unsigned::value) { - py_value = as_unsigned(src_or_index.ptr()); - } else { // signed integer: - py_value = sizeof(T) <= sizeof(long) - ? (py_type) PyLong_AsLong(src_or_index.ptr()) - : (py_type) PYBIND11_LONG_AS_LONGLONG(src_or_index.ptr()); - } + if PYBIND11_IF_CONSTEXPR (std::is_unsigned::value) { + py_value = as_unsigned(src_or_index.ptr()); + } else { // signed integer: + py_value = sizeof(T) <= sizeof(long) + ? (py_type) PyLong_AsLong(src_or_index.ptr()) + : (py_type) PYBIND11_LONG_AS_LONGLONG(src_or_index.ptr()); + } } // Python API reported an error diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index f927ac9b1..c33120931 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -2011,7 +2011,7 @@ inline str enum_name(handle arg) { struct enum_base { enum_base(const handle &base, const handle &parent) : m_base(base), m_parent(parent) {} - template + template PYBIND11_NOINLINE void init() { m_base.attr("__entries") = dict(); auto property = handle((PyObject *) &PyProperty_Type);