From 3fa714834f94e81a422d7566836efd21d9340f3b Mon Sep 17 00:00:00 2001 From: Sergei Izmailov Date: Wed, 31 Aug 2022 11:29:26 +0900 Subject: [PATCH 1/2] fix: Python-3.12 compatibility Enable dynamic attributes for `pybind11_static_property` --- include/pybind11/detail/class.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index a98e5e5414..d73814671b 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -55,6 +55,9 @@ extern "C" inline int pybind11_static_set(PyObject *self, PyObject *obj, PyObjec return PyProperty_Type.tp_descr_set(self, cls, value); } +// Forward declaration to use in `make_static_property_type()` +inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type); + /** A `static_property` is the same as a `property` but the `__get__()` and `__set__()` methods are modified to always use the object type instead of a concrete instance. Return value: New reference. */ @@ -86,7 +89,11 @@ inline PyTypeObject *make_static_property_type() { if (PyType_Ready(type) < 0) { pybind11_fail("make_static_property_type(): failure in PyType_Ready()!"); } - +# if PY_VERSION_HEX >= 0x030C0000 + // Since Python-3.12 property-derived types are required to + // have dynamic attributes (to set `__doc__`) + enable_dynamic_attributes(heap_type); +# endif setattr((PyObject *) type, "__module__", str("pybind11_builtins")); PYBIND11_SET_OLDPY_QUALNAME(type, name_obj); From 019bc66da0def79cabe7c79b0f268d880ff5044c Mon Sep 17 00:00:00 2001 From: Sergei Izmailov Date: Mon, 5 Sep 2022 12:44:48 +0900 Subject: [PATCH 2/2] Add future-notice comment --- include/pybind11/detail/class.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index d73814671b..528e716f78 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -89,11 +89,14 @@ inline PyTypeObject *make_static_property_type() { if (PyType_Ready(type) < 0) { pybind11_fail("make_static_property_type(): failure in PyType_Ready()!"); } + # if PY_VERSION_HEX >= 0x030C0000 + // PRE 3.12 FEATURE FREEZE. PLEASE REVIEW AFTER FREEZE. // Since Python-3.12 property-derived types are required to // have dynamic attributes (to set `__doc__`) enable_dynamic_attributes(heap_type); # endif + setattr((PyObject *) type, "__module__", str("pybind11_builtins")); PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);