Sync Py_TPFLAGS_MANAGED_DICT for PyPy3.11 across the codebase (#5537)

* Sync `Py_TPFLAGS_MANAGED_DICT` for PyPy3.11 across the codebase

Adjust the `Py_TPFLAGS_MANAGED_DICT` logic in `include/pybind11/attr.h`
to match the one used in `include/pybind11/detail/class.h`.

This is a followup to #5508.

* Use a common `#define` for pre-`Py_TPFLAGS_MANAGED_DICT` Pythons

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Michał Górny 2025-02-20 22:53:45 +01:00 committed by GitHub
parent 09b9f44ab7
commit d8565ac731
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 4 deletions

View File

@ -359,7 +359,7 @@ struct type_record {
bases.append((PyObject *) base_info->type); 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; dynamic_attr |= base_info->type->tp_dictoffset != 0;
#else #else
dynamic_attr |= (base_info->type->tp_flags & Py_TPFLAGS_MANAGED_DICT) != 0; dynamic_attr |= (base_info->type->tp_flags & Py_TPFLAGS_MANAGED_DICT) != 0;

View File

@ -574,9 +574,9 @@ extern "C" inline int pybind11_clear(PyObject *self) {
inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) { inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
auto *type = &heap_type->ht_type; auto *type = &heap_type->ht_type;
type->tp_flags |= Py_TPFLAGS_HAVE_GC; type->tp_flags |= Py_TPFLAGS_HAVE_GC;
#if PY_VERSION_HEX < 0x030B0000 || defined(PYPY_VERSION) // For PyPy see PR #5508 #ifdef PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET
type->tp_dictoffset = type->tp_basicsize; // place dict at the end type->tp_dictoffset = type->tp_basicsize; // place dict at the end
type->tp_basicsize += (ssize_t) sizeof(PyObject *); // and allocate enough space for it type->tp_basicsize += (ssize_t) sizeof(PyObject *); // and allocate enough space for it
#else #else
type->tp_flags |= Py_TPFLAGS_MANAGED_DICT; type->tp_flags |= Py_TPFLAGS_MANAGED_DICT;
#endif #endif

View File

@ -1256,5 +1256,10 @@ constexpr
# define PYBIND11_DETAILED_ERROR_MESSAGES # define PYBIND11_DETAILED_ERROR_MESSAGES
#endif #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(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)