8000 bpo-44874: deprecate Py_TRASHCAN_SAFE_BEGIN and Py_TRASHCAN_SAFE_END … · python/cpython@31ee985 · GitHub
[go: up one dir, main page]

Skip to content

Commit 31ee985

Browse files
iritkatrielvstinnerambv
authored
bpo-44874: deprecate Py_TRASHCAN_SAFE_BEGIN and Py_TRASHCAN_SAFE_END (GH-27693)
Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
1 parent a3a4d20 commit 31ee985

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

Doc/whatsnew/3.11.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,47 @@ New Features
307307
Porting to Python 3.11
308308
----------------------
309309

310+
* The old trashcan macros (``Py_TRASHCAN_SAFE_BEGIN``/``Py_TRASHCAN_SAFE_END``)
311+
are now deprecated. They should be replaced by the new macros
312+
``Py_TRASHCAN_BEGIN`` and ``Py_TRASHCAN_END``.
313+
314+
A tp_dealloc function that has the old macros, such as::
315+
316+
static void
317+
mytype_dealloc(mytype *p)
318+
{
319+
PyObject_GC_UnTrack(p);
320+
Py_TRASHCAN_SAFE_BEGIN(p);
321+
...
322+
Py_TRASHCAN_SAFE_END
323+
}
324+
325+
should migrate to the new macros as follows::
326+
327+
static void
328+
mytype_dealloc(mytype *p)
329+
{
330+
PyObject_GC_UnTrack(p);
331+
Py_TRASHCAN_BEGIN(p, mytype_dealloc)
332+
...
333+
Py_TRASHCAN_END
334+
}
335+
336+
Note that ``Py_TRASHCAN_BEGIN`` has a second argument which
337+
should be the deallocation function it is in.
338+
339+
To support older Python versions in the same codebase, you
340+
can define the following macros and use them throughout
341+
the code (credit: these were copied from the ``mypy`` codebase)::
342+
343+
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 8
344+
# define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_BEGIN(op, dealloc)
345+
# define CPy_TRASHCAN_END(op) Py_TRASHCAN_END
346+
#else
347+
# define CPy_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_SAFE_BEGIN(op)
348+
# define CPy_TRASHCAN_END(op) Py_TRASHCAN_SAFE_END(op)
349+
#endif
350+
310351
* The :c:func:`PyType_Ready` function now raises an error if a type is defined
311352
with the :const:`Py_TPFLAGS_HAVE_GC` flag set but has no traverse function
312353
(:c:member:`PyTypeObject.tp_traverse`).

Include/cpython/object.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,16 @@ PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc);
534534
Py_TRASHCAN_BEGIN_CONDITION(op, \
535535
_PyTrash_cond(_PyObject_CAST(op), (destructor)dealloc))
536536

537-
/* For backwards compatibility, these macros enable the trashcan
538-
* unconditionally */
539-
#define Py_TRASHCAN_SAFE_BEGIN(op) Py_TRASHCAN_BEGIN_CONDITION(op, 1)
540-
#define Py_TRASHCAN_SAFE_END(op) Py_TRASHCAN_END
537+
/* The following two macros, Py_TRASHCAN_SAFE_BEGIN and
538+
* Py_TRASHCAN_SAFE_END, are deprecated since version 3.11 and
539+
* will be removed in the future.
540+
* Use Py_TRASHCAN_BEGIN and Py_TRASHCAN_END instead.
541+
*/
542+
Py_DEPRECATED(3.11) typedef int UsingDeprecatedTrashcanMacro;
543+
#define Py_TRASHCAN_SAFE_BEGIN(op) \
544+
do { \
545+
UsingDeprecatedTrashcanMacro cond=1; \
546+
Py_TRASHCAN_BEGIN_CONDITION(op, cond);
547+
#define Py_TRASHCAN_SAFE_END(op) \
548+
Py_TRASHCAN_END; \
549+
} while(0);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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``.

0 commit comments

Comments
 (0)
0