From 6772bdf358e67405a2736a7306622f8f6c43fa1a Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Mon, 9 Aug 2021 20:02:42 +0100 Subject: [PATCH 1/6] bpo-44874: deprecate Py_TRASHCAN_SAFE_BEGIN and Py_TRASHCAN_SAFE_END --- Include/cpython/object.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Include/cpython/object.h b/Include/cpython/object.h index 75cd0f9002215b..8aa81df52e89fb 100644 --- a/Include/cpython/object.h +++ b/Include/cpython/object.h @@ -534,7 +534,10 @@ PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc); Py_TRASHCAN_BEGIN_CONDITION(op, \ _PyTrash_cond(_PyObject_CAST(op), (destructor)dealloc)) -/* For backwards compatibility, these macros enable the trashcan - * unconditionally */ +/* The following two macros, Py_TRASHCAN_SAFE_BEGIN and + * Py_TRASHCAN_SAFE_END, are deprecated since version 3.11 and + * will be removed in the future. + * Use Py_TRASHCAN_BEGIN and Py_TRASHCAN_END instead. + */ #define Py_TRASHCAN_SAFE_BEGIN(op) Py_TRASHCAN_BEGIN_CONDITION(op, 1) #define Py_TRASHCAN_SAFE_END(op) Py_TRASHCAN_END From 62d64e71d0a7b9c420d50d1ab9ce9ac631b1edab Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 9 Aug 2021 19:05:21 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst b/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst new file mode 100644 index 00000000000000..3c4d4f45057665 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst @@ -0,0 +1 @@ +Deprecate the old trashcan macros (Py_TRASHCAN_SAFE_BEGIN/Py_TRASHCAN_SAFE_END). \ No newline at end of file From d4de5cd5be9da5025058c921f0d02d2895b4e8af Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Tue, 17 Aug 2021 15:43:12 +0100 Subject: [PATCH 3/6] add compiler warning --- Include/cpython/object.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Include/cpython/object.h b/Include/cpython/object.h index 8aa81df52e89fb..5ae6f367c6048a 100644 --- a/Include/cpython/object.h +++ b/Include/cpython/object.h @@ -539,5 +539,11 @@ PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc); * will be removed in the future. * Use Py_TRASHCAN_BEGIN and Py_TRASHCAN_END instead. */ -#define Py_TRASHCAN_SAFE_BEGIN(op) Py_TRASHCAN_BEGIN_CONDITION(op, 1) -#define Py_TRASHCAN_SAFE_END(op) Py_TRASHCAN_END +Py_DEPRECATED(3.11) typedef int UsingDeprecatedTrashcanMacro; +#define Py_TRASHCAN_SAFE_BEGIN(op) \ + do { \ + UsingDeprecatedTrashcanMacro cond=1; \ + Py_TRASHCAN_BEGIN_CONDITION(op, cond); +#define Py_TRASHCAN_SAFE_END(op) \ + Py_TRASHCAN_END; \ + } while(0); From f58b6276bce98b062df7e37188995e5270e981db Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Wed, 18 Aug 2021 10:31:02 +0100 Subject: [PATCH 4/6] add migration instructions to news file --- .../2021-08-09-19-05-20.bpo-44874.oOcfU4.rst | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst b/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst index 3c4d4f45057665..23d2e31c4cc323 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst @@ -1 +1,38 @@ -Deprecate the old trashcan macros (Py_TRASHCAN_SAFE_BEGIN/Py_TRASHCAN_SAFE_END). \ No newline at end of file +Deprecate the old trashcan macros (``Py_TRASHCAN_SAFE_BEGIN``/``Py_TRASHCAN_SAFE_END``). They should be replaced by the new macros ``Py_TRASHCAN_BEGIN`` and ``Py_TRASHCAN_END``. + +A tp_dealloc function that has the old macros, such as:: + + static void + mytype_dealloc(mytype *p) + { + PyObject_GC_UnTrack(p); + Py_TRASHCAN_SAFE_BEGIN(p); + ... + Py_TRASHCAN_SAFE_END + } + +should migrate to the new macros as follows:: + + static void + mytype_dealloc(mytype *p) + { + PyObject_GC_UnTrack(p); + Py_TRASHCAN_BEGIN(p, mytype_dealloc) + ... + Py_TRASHCAN_END + } + +Note that ``Py_TRASHCAN_BEGIN`` has a second argument which + should be the deallocation function it is in. + +To support older python versions in the same codebase, you +can define the following macros and use them throughout +the code (credit: these were copied from the ``mypy`` codebase):: + + #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 8 + #define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_BEGIN(op, dealloc) + #define CPy_TRASHCAN_END(op) Py_TRASHCAN_END + #else + #define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_SAFE_BEGIN(op) + #define CPy_TRASHCAN_END(op) Py_TRASHCAN_SAFE_END(op) + #endif From dc5cd3422ff94ae36ccc8f7e3177a7af578fe81c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Wed, 18 Aug 2021 21:10:26 +0200 Subject: [PATCH 5/6] Increase example readability Co-authored-by: Victor Stinner --- .../2021-08-09-19-05-20.bpo-44874.oOcfU4.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst b/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst index 23d2e31c4cc323..896c6f20c6e0e5 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst @@ -30,9 +30,9 @@ can define the following macros and use them throughout the code (credit: these were copied from the ``mypy`` codebase):: #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 8 - #define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_BEGIN(op, dealloc) - #define CPy_TRASHCAN_END(op) Py_TRASHCAN_END + # define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_BEGIN(op, dealloc) + # define CPy_TRASHCAN_END(op) Py_TRASHCAN_END #else - #define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_SAFE_BEGIN(op) - #define CPy_TRASHCAN_END(op) Py_TRASHCAN_SAFE_END(op) + # define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_SAFE_BEGIN(op) + # define CPy_TRASHCAN_END(op) Py_TRASHCAN_SAFE_END(op) #endif From 9d2d76d895ebbfaf41e3c933498539293b8c2c92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Wed, 18 Aug 2021 21:26:31 +0200 Subject: [PATCH 6/6] Move porting instructions to What's New in Python 3.11 --- Doc/whatsnew/3.11.rst | 41 +++++++++++++++++++ .../2021-08-09-19-05-20.bpo-44874.oOcfU4.rst | 37 ----------------- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 88b6f8fa7314e0..e67dea262447e4 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -304,6 +304,47 @@ New Features Porting to Python 3.11 ---------------------- +* The old trashcan macros (``Py_TRASHCAN_SAFE_BEGIN``/``Py_TRASHCAN_SAFE_END``) + are now deprecated. They should be replaced by the new macros + ``Py_TRASHCAN_BEGIN`` and ``Py_TRASHCAN_END``. + + A tp_dealloc function that has the old macros, such as:: + + static void + mytype_dealloc(mytype *p) + { + PyObject_GC_UnTrack(p); + Py_TRASHCAN_SAFE_BEGIN(p); + ... + Py_TRASHCAN_SAFE_END + } + + should migrate to the new macros as follows:: + + static void + mytype_dealloc(mytype *p) + { + PyObject_GC_UnTrack(p); + Py_TRASHCAN_BEGIN(p, mytype_dealloc) + ... + Py_TRASHCAN_END + } + + Note that ``Py_TRASHCAN_BEGIN`` has a second argument which + should be the deallocation function it is in. + + To support older Python versions in the same codebase, you + can define the following macros and use them throughout + the code (credit: these were copied from the ``mypy`` codebase):: + + #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 8 + # define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_BEGIN(op, dealloc) + # define CPy_TRASHCAN_END(op) Py_TRASHCAN_END + #else + # define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_SAFE_BEGIN(op) + # define CPy_TRASHCAN_END(op) Py_TRASHCAN_SAFE_END(op) + #endif + * The :c:func:`PyType_Ready` function now raises an error if a type is defined with the :const:`Py_TPFLAGS_HAVE_GC` flag set but has no traverse function (:c:member:`PyTypeObject.tp_traverse`). diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst b/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst index 896c6f20c6e0e5..1aed5351d74173 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-08-09-19-05-20.bpo-44874.oOcfU4.rst @@ -1,38 +1 @@ Deprecate the old trashcan macros (``Py_TRASHCAN_SAFE_BEGIN``/``Py_TRASHCAN_SAFE_END``). They should be replaced by the new macros ``Py_TRASHCAN_BEGIN`` and ``Py_TRASHCAN_END``. - -A tp_dealloc function that has the old macros, such as:: - - static void - mytype_dealloc(mytype *p) - { - PyObject_GC_UnTrack(p); - Py_TRASHCAN_SAFE_BEGIN(p); - ... - Py_TRASHCAN_SAFE_END - } - -should migrate to the new macros as follows:: - - static void - mytype_dealloc(mytype *p) - { - PyObject_GC_UnTrack(p); - Py_TRASHCAN_BEGIN(p, mytype_dealloc) - ... - Py_TRASHCAN_END - } - -Note that ``Py_TRASHCAN_BEGIN`` has a second argument which - should be the deallocation function it is in. - -To support older python versions in the same codebase, you -can define the following macros and use them throughout -the code (credit: these were copied from the ``mypy`` codebase):: - - #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 8 - # define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_BEGIN(op, dealloc) - # define CPy_TRASHCAN_END(op) Py_TRASHCAN_END - #else - # define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_SAFE_BEGIN(op) - # define CPy_TRASHCAN_END(op) Py_TRASHCAN_SAFE_END(op) - #endif