diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index c863d8be5..eb15ffd07 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -359,7 +359,7 @@ struct type_record { bases.append((PyObject *) base_info->type); -#if PY_VERSION_HEX < 0x030B0000 +#ifdef PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET dynamic_attr |= base_info->type->tp_dictoffset != 0; #else dynamic_attr |= (base_info->type->tp_flags & Py_TPFLAGS_MANAGED_DICT) != 0; diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 1ed2e90c2..aac6e847c 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -574,9 +574,9 @@ extern "C" inline int pybind11_clear(PyObject *self) { inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) { auto *type = &heap_type->ht_type; type->tp_flags |= Py_TPFLAGS_HAVE_GC; -#if PY_VERSION_HEX < 0x030B0000 || defined(PYPY_VERSION) // For PyPy see PR #5508 - type->tp_dictoffset = type->tp_basicsize; // place dict at the end - type->tp_basicsize += (ssize_t) sizeof(PyObject *); // and allocate enough space for it +#ifdef PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET + type->tp_dictoffset = type->tp_basicsize; // place dict at the end + type->tp_basicsize += (ssize_t) sizeof(PyObject *); // and allocate enough space for it #else type->tp_flags |= Py_TPFLAGS_MANAGED_DICT; #endif diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 8336f8bc1..372dd069a 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -1256,5 +1256,10 @@ constexpr # define PYBIND11_DETAILED_ERROR_MESSAGES #endif +// CPython 3.11+ provides Py_TPFLAGS_MANAGED_DICT, but PyPy3.11 does not, see PR #5508. +#if PY_VERSION_HEX < 0x030B0000 || defined(PYPY_VERSION) +# define PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET +#endif + PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)