[Python-checkins] bpo-40170: Hide impl detail of Py_TRASHCAN_BEGIN macro (GH-23235)

vstinner webhook-mailer at python.org
Tue Nov 24 17:03:37 EST 2020


https://github.com/python/cpython/commit/ed1a5a5baca8f61e9a99c5be3adc16b1801514fe
commit: ed1a5a5baca8f61e9a99c5be3adc16b1801514fe
branch: master
author: Hai Shi <shihai1992 at gmail.com>
committer: vstinner <vstinner at python.org>
date: 2020-11-24T23:03:31+01:00
summary:

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.

files:
A Misc/NEWS.d/next/C API/2020-11-13-01-40-28.bpo-40170.uh8lEf.rst
M Include/cpython/object.h
M Objects/object.c

diff --git a/Include/cpython/object.h b/Include/cpython/object.h
index 43b0be37557a3..19c066b0ab78c 100644
--- a/Include/cpython/object.h
+++ b/Include/cpython/object.h
@@ -516,6 +516,8 @@ struct _ts;
 /* Python 3.9 private API, invoked by the macros below. */
 PyAPI_FUNC(int) _PyTrash_begin(struct _ts *tstate, PyObject *op);
 PyAPI_FUNC(void) _PyTrash_end(struct _ts *tstate);
+/* Python 3.10 private API, invoked by the Py_TRASHCAN_BEGIN(). */
+PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc);
 
 #define PyTrash_UNWIND_LEVEL 50
 
@@ -539,7 +541,7 @@ PyAPI_FUNC(void) _PyTrash_end(struct _ts *tstate);
 
 #define Py_TRASHCAN_BEGIN(op, dealloc) \
     Py_TRASHCAN_BEGIN_CONDITION(op, \
-        Py_TYPE(op)->tp_dealloc == (destructor)(dealloc))
+        _PyTrash_cond(_PyObject_CAST(op), (destructor)dealloc))
 
 /* For backwards compatibility, these macros enable the trashcan
  * unconditionally */
diff --git a/Misc/NEWS.d/next/C API/2020-11-13-01-40-28.bpo-40170.uh8lEf.rst b/Misc/NEWS.d/next/C API/2020-11-13-01-40-28.bpo-40170.uh8lEf.rst
new file mode 100644
index 0000000000000..741f9520686f3
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2020-11-13-01-40-28.bpo-40170.uh8lEf.rst	
@@ -0,0 +1,3 @@
+The ``Py_TRASHCAN_BEGIN`` macro no longer accesses PyTypeObject attributes,
+but now can get the condition by calling the new private
+:c:func:`_PyTrash_cond()` function which hides implementation details.
diff --git a/Objects/object.c b/Objects/object.c
index be7790eefd118..2e8717f506ca0 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -2134,6 +2134,15 @@ _PyTrash_end(PyThreadState *tstate)
 }
 
 
+/* bpo-40170: It's only be used in Py_TRASHCAN_BEGIN macro to hide
+   implementation details. */
+int
+_PyTrash_cond(PyObject *op, destructor dealloc)
+{
+    return Py_TYPE(op)->tp_dealloc == dealloc;
+}
+
+
 void _Py_NO_RETURN
 _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
                        const char *file, int line, const char *function)



More information about the Python-checkins mailing list