From 73825f351b8ae93a98232c07679be72e8fea9790 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Thu, 20 Feb 2025 17:53:56 +0000 Subject: [PATCH 1/3] Improved reference_internal documentation (#5528) * Improved reference_internal documentation Improved the wording to make it easier to understand and added a note about the policy falling back to move if the return value is not an lvalue reference or pointer. * Added bold and ticket number --- docs/advanced/functions.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/advanced/functions.rst b/docs/advanced/functions.rst index 372934b09..ff00c9c8a 100644 --- a/docs/advanced/functions.rst +++ b/docs/advanced/functions.rst @@ -81,9 +81,11 @@ The following table provides an overview of available policies: | | it is no longer used. Warning: undefined behavior will ensue when the C++ | | | side deletes an object that is still referenced and used by Python. | +--------------------------------------------------+----------------------------------------------------------------------------+ -| :enum:`return_value_policy::reference_internal` | Indicates that the lifetime of the return value is tied to the lifetime | -| | of a parent object, namely the implicit ``this``, or ``self`` argument of | -| | the called method or property. Internally, this policy works just like | +| :enum:`return_value_policy::reference_internal` | If the return value is an lvalue reference or a pointer, the parent object | +| | (the implicit ``this``, or ``self`` argument of the called method or | +| | property) is kept alive for at least the lifespan of the return value. | +| | **Otherwise this policy falls back to :enum:`return_value_policy::move` | +| | (see #5528).** Internally, this policy works just like | | | :enum:`return_value_policy::reference` but additionally applies a | | | ``keep_alive<0, 1>`` *call policy* (described in the next section) that | | | prevents the parent object from being garbage collected as long as the | From 09b9f44ab7a5e3cc6ae13bf2bdde25ee90eb092f Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Thu, 20 Feb 2025 20:04:16 +0200 Subject: [PATCH 2/3] add recently released pypy3.11 (#5534) --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e09540b3e..33e281db7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,6 +39,7 @@ jobs: - 'pypy-3.8' - 'pypy-3.9' - 'pypy-3.10' + - 'pypy-3.11' - 'graalpy-24.1' # Items in here will either be added to the build matrix (if not From d8565ac7317b4838a08308170f5b9ba82d091546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Thu, 20 Feb 2025 22:53:45 +0100 Subject: [PATCH 3/3] 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> --- include/pybind11/attr.h | 2 +- include/pybind11/detail/class.h | 6 +++--- include/pybind11/detail/common.h | 5 +++++ 3 files changed, 9 insertions(+), 4 deletions(-) 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)