Skip to content

Commit ed1a5a5

Browse files
authored
bpo-40170: Hide impl detail of Py_TRASHCAN_BEGIN macro (GH-23235)
The Py_TRASHCAN_BEGIN macro no longer accesses PyTypeObject attributes, but now can get the condition by calling the new private _PyTrash_cond() function which hides implementation details.
1 parent 0ec34ca commit ed1a5a5

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Include/cpython/object.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ struct _ts;
516516
/* Python 3.9 private API, invoked by the macros below. */
517517
PyAPI_FUNC(int) _PyTrash_begin(struct _ts *tstate, PyObject *op);
518518
PyAPI_FUNC(void) _PyTrash_end(struct _ts *tstate);
519+
/* Python 3.10 private API, invoked by the Py_TRASHCAN_BEGIN(). */
520+
PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc);
519521

520522
#define PyTrash_UNWIND_LEVEL 50
521523

@@ -539,7 +541,7 @@ PyAPI_FUNC(void) _PyTrash_end(struct _ts *tstate);
539541

540542
#define Py_TRASHCAN_BEGIN(op, dealloc) \
541543
Py_TRASHCAN_BEGIN_CONDITION(op, \
542-
Py_TYPE(op)->tp_dealloc == (destructor)(dealloc))
544+
_PyTrash_cond(_PyObject_CAST(op), (destructor)dealloc))
543545

544546
/* For backwards compatibility, these macros enable the trashcan
545547
* unconditionally */
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The ``Py_TRASHCAN_BEGIN`` macro no longer accesses PyTypeObject attributes,
2+
but now can get the condition by calling the new private
3+
:c:func:`_PyTrash_cond()` function which hides implementation details.

Objects/object.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,6 +2134,15 @@ _PyTrash_end(PyThreadState *tstate)
21342134
}
21352135

21362136

2137+
/* bpo-40170: It's only be used in Py_TRASHCAN_BEGIN macro to hide
2138+
implementation details. */
2139+
int
2140+
_PyTrash_cond(PyObject *op, destructor dealloc)
2141+
{
2142+
return Py_TYPE(op)->tp_dealloc == dealloc;
2143+
}
2144+
2145+
21372146
void _Py_NO_RETURN
21382147
_PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
21392148
const char *file, int line, const char *function)

0 commit comments

Comments
 (0)