From 2026df3a2cfcfc75bf1806357bc1c746da972e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Thu, 20 Feb 2025 16:23:07 +0100 Subject: [PATCH 1/3] 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. --- include/pybind11/attr.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index c863d8be5b..b389032e54 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -359,7 +359,8 @@ struct type_record { bases.append((PyObject *) base_info->type); -#if PY_VERSION_HEX < 0x030B0000 +// Keep in sync with enable_dynamic_attributes() in detail/class.h +#if PY_VERSION_HEX < 0x030B0000 || defined(PYPY_VERSION) dynamic_attr |= base_info->type->tp_dictoffset != 0; #else dynamic_attr |= (base_info->type->tp_flags & Py_TPFLAGS_MANAGED_DICT) != 0; From 459f24beb0839a19aa9491029eb3c3399c5e4a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Thu, 20 Feb 2025 20:34:43 +0100 Subject: [PATCH 2/3] Use a common `#define` for pre-`Py_TPFLAGS_MANAGED_DICT` Pythons --- include/pybind11/attr.h | 3 +-- include/pybind11/detail/class.h | 2 +- include/pybind11/detail/common.h | 5 +++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index b389032e54..eb15ffd073 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -359,8 +359,7 @@ struct type_record { bases.append((PyObject *) base_info->type); -// Keep in sync with enable_dynamic_attributes() in detail/class.h -#if PY_VERSION_HEX < 0x030B0000 || defined(PYPY_VERSION) +#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 1ed2e90c2c..9f59651074 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -574,7 +574,7 @@ 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 +#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 diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 8336f8bc13..372dd069a1 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) From 14ff0e3ee6cd91cab90405e01146f8eb960ab1a7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 20 Feb 2025 19:36:30 +0000 Subject: [PATCH 3/3] style: pre-commit fixes --- include/pybind11/detail/class.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index 9f59651074..aac6e847c6 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -575,8 +575,8 @@ inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) { auto *type = &heap_type->ht_type; type->tp_flags |= Py_TPFLAGS_HAVE_GC; #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 + 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