Skip to content

Commit d8565ac

Browse files
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>
1 parent 09b9f44 commit d8565ac

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

include/pybind11/attr.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ struct type_record {
359359

360360
bases.append((PyObject *) base_info->type);
361361

362-
#if PY_VERSION_HEX < 0x030B0000
362+
#ifdef PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET
363363
dynamic_attr |= base_info->type->tp_dictoffset != 0;
364364
#else
365365
dynamic_attr |= (base_info->type->tp_flags & Py_TPFLAGS_MANAGED_DICT) != 0;

include/pybind11/detail/class.h

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

include/pybind11/detail/common.h

+5
Original file line numberDiff line numberDiff line change
@@ -1256,5 +1256,10 @@ constexpr
12561256
# define PYBIND11_DETAILED_ERROR_MESSAGES
12571257
#endif
12581258

1259+
// CPython 3.11+ provides Py_TPFLAGS_MANAGED_DICT, but PyPy3.11 does not, see PR #5508.
1260+
#if PY_VERSION_HEX < 0x030B0000 || defined(PYPY_VERSION)
1261+
# define PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET
1262+
#endif
1263+
12591264
PYBIND11_NAMESPACE_END(detail)
12601265
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

0 commit comments

Comments
 (0)