GH-124715: Move trashcan mechanism into Py_Dealloc (GH-132280)

This commit is contained in:
Mark Shannon 2025-04-30 11:37:53 +01:00 committed by GitHub
parent 0f23e84cda
commit 44e4c479fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 88 additions and 196 deletions

View file

@ -566,11 +566,9 @@ filter_dealloc(PyObject *self)
{
filterobject *lz = _filterobject_CAST(self);
PyObject_GC_UnTrack(lz);
Py_TRASHCAN_BEGIN(lz, filter_dealloc)
Py_XDECREF(lz->func);
Py_XDECREF(lz->it);
Py_TYPE(lz)->tp_free(lz);
Py_TRASHCAN_END
}
static int

View file

@ -482,12 +482,6 @@ _Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
uintptr_t here_addr = _Py_get_machine_stack_pointer();
assert(_tstate->c_stack_soft_limit != 0);
if (_tstate->c_stack_hard_limit == 0) {
_Py_InitializeRecursionLimits(tstate);
}
if (here_addr >= _tstate->c_stack_soft_limit) {
return 0;
}
assert(_tstate->c_stack_hard_limit != 0);
if (here_addr < _tstate->c_stack_hard_limit) {
/* Overflowing while handling an overflow. Give up. */

View file

@ -2207,9 +2207,8 @@ void
PyObject_GC_UnTrack(void *op_raw)
{
PyObject *op = _PyObject_CAST(op_raw);
/* Obscure: the Py_TRASHCAN mechanism requires that we be able to
* call PyObject_GC_UnTrack twice on an object.
*/
/* The code for some objects, such as tuples, is a bit
* sloppy about when the object is tracked and untracked. */
if (_PyObject_GC_IS_TRACKED(op)) {
_PyObject_GC_UNTRACK(op);
}

View file

@ -2511,9 +2511,8 @@ void
PyObject_GC_UnTrack(void *op_raw)
{
PyObject *op = _PyObject_CAST(op_raw);
/* Obscure: the Py_TRASHCAN mechanism requires that we be able to
* call PyObject_GC_UnTrack twice on an object.
*/
/* The code for some objects, such as tuples, is a bit
* sloppy about when the object is tracked and untracked. */
if (_PyObject_GC_IS_TRACKED(op)) {
_PyObject_GC_UNTRACK(op);
}

View file

@ -1118,7 +1118,6 @@ hamt_node_bitmap_dealloc(PyObject *self)
}
PyObject_GC_UnTrack(self);
Py_TRASHCAN_BEGIN(self, hamt_node_bitmap_dealloc)
if (len > 0) {
i = len;
@ -1128,7 +1127,6 @@ hamt_node_bitmap_dealloc(PyObject *self)
}
Py_TYPE(self)->tp_free(self);
Py_TRASHCAN_END
}
#ifdef Py_DEBUG
@ -1508,7 +1506,6 @@ hamt_node_collision_dealloc(PyObject *self)
/* Collision's tp_dealloc */
Py_ssize_t len = Py_SIZE(self);
PyObject_GC_UnTrack(self);
Py_TRASHCAN_BEGIN(self, hamt_node_collision_dealloc)
if (len > 0) {
PyHamtNode_Collision *node = _PyHamtNode_Collision_CAST(self);
while (--len >= 0) {
@ -1516,7 +1513,6 @@ hamt_node_collision_dealloc(PyObject *self)
}
}
Py_TYPE(self)->tp_free(self);
Py_TRASHCAN_END
}
#ifdef Py_DEBUG
@ -1878,13 +1874,11 @@ hamt_node_array_dealloc(PyObject *self)
{
/* Array's tp_dealloc */
PyObject_GC_UnTrack(self);
Py_TRASHCAN_BEGIN(self, hamt_node_array_dealloc)
PyHamtNode_Array *obj = _PyHamtNode_Array_CAST(self);
for (Py_ssize_t i = 0; i < HAMT_ARRAY_NODE_SIZE; i++) {
Py_XDECREF(obj->a_array[i]);
}
Py_TYPE(self)->tp_free(self);
Py_TRASHCAN_END
}
#ifdef Py_DEBUG

View file

@ -419,10 +419,8 @@ inst_seq_dealloc(PyObject *op)
{
_PyInstructionSequence *seq = (_PyInstructionSequence *)op;
PyObject_GC_UnTrack(seq);
Py_TRASHCAN_BEGIN(seq, inst_seq_dealloc)
PyInstructionSequence_Fini(seq);
PyObject_GC_Del(seq);
Py_TRASHCAN_END
}
static int

View file

@ -854,6 +854,10 @@ error:
static PyStatus
pycore_interp_init(PyThreadState *tstate)
{
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
if (_tstate->c_stack_hard_limit == 0) {
_Py_InitializeRecursionLimits(tstate);
}
PyInterpreterState *interp = tstate->interp;
PyStatus status;
PyObject *sysmod = NULL;

View file

@ -2168,7 +2168,10 @@ _PyThreadState_Attach(PyThreadState *tstate)
if (current_fast_get() != NULL) {
Py_FatalError("non-NULL old thread state");
}
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
if (_tstate->c_stack_hard_limit == 0) {
_Py_InitializeRecursionLimits(tstate);
}
while (1) {
_PyEval_AcquireLock(tstate);

View file

@ -236,11 +236,9 @@ tb_dealloc(PyObject *op)
{
PyTracebackObject *tb = _PyTracebackObject_CAST(op);
PyObject_GC_UnTrack(tb);
Py_TRASHCAN_BEGIN(tb, tb_dealloc)
Py_XDECREF(tb->tb_next);
Py_XDECREF(tb->tb_frame);
PyObject_GC_Del(tb);
Py_TRASHCAN_END
}
static int