8000 GH-124715: trashcan protection for all GC objects by nascheme · Pull Request #124716 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-124715: trashcan protection for all GC objects #124716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip: fix _PyObject_GC_UNTRACK calls
These calls in tp_dealloc methods now need to be removed since
_Py_Dealloc always does the untrack for GC objects
  • Loading branch information
nascheme committed Sep 27, 2024
commit 73644a76b8313756d78eca64afc09df3d5c5267e
6 changes: 3 additions & 3 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,10 @@ buffered_dealloc(buffered *self)
{
PyTypeObject *tp = Py_TYPE(self);
self->finalizing = 1;
if (_PyIOBase_finalize((PyObject *) self) < 0)
if (_PyIOBase_finalize((PyObject *) self) < 0) {
PyObject_GC_Track(self); // untracked by _Py_Dealloc
return;
_PyObject_GC_UNTRACK(self);
}
self->ok = 0;
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *)self);
Expand Down Expand Up @@ -2298,7 +2299,6 @@ static void
bufferedrwpair_dealloc(rwpair *self)
{
PyTypeObject *tp = Py_TYPE(self);
_PyObject_GC_UNTRACK(self);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *)self);
(void)bufferedrwpair_clear(self);
Expand Down
1 change: 0 additions & 1 deletion Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,6 @@ static void
bytesio_dealloc(bytesio *self)
{
PyTypeObject *tp = Py_TYPE(self);
_PyObject_GC_UNTRACK(self);
if (self->exports > 0) {
PyErr_SetString(PyExc_SystemError,
"deallocated BytesIO object has exported buffers");
Expand Down
5 changes: 3 additions & 2 deletions Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,10 @@ fileio_dealloc(fileio *self)
{
PyTypeObject *tp = Py_TYPE(self);
self->finalizing = 1;
if (_PyIOBase_finalize((PyObject *) self) < 0)
if (_PyIOBase_finalize((PyObject *) self) < 0) {
PyObject_GC_Track(self); // untracked by _Py_Dealloc
return;
_PyObject_GC_UNTRACK(self);
}
if (self->stat_atopen != NULL) {
PyMem_Free(self->stat_atopen);
self->stat_atopen = NULL;
Expand Down
2 changes: 1 addition & 1 deletion Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,10 @@ iobase_dealloc(iobase *self)
if (_PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE)) {
Py_INCREF(Py_TYPE(self));
}
PyObject_GC_Track(self); // untracked by _Py_Dealloc
return;
}
PyTypeObject *tp = Py_TYPE(self);
_PyObject_GC_UNTRACK(self);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
Py_CLEAR(self->dict);
Expand Down
1 change: 0 additions & 1 deletion Modules/_io/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,6 @@ static void
stringio_dealloc(stringio *self)
{
PyTypeObject *tp = Py_TYPE(self);
_PyObject_GC_UNTRACK(self);
self->ok = 0;
if (self->buf) {
PyMem_Free(self->buf);
Expand Down
6 changes: 3 additions & 3 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ static void
incrementalnewlinedecoder_dealloc(nldecoder_object *self)
{
PyTypeObject *tp = Py_TYPE(self);
_PyObject_GC_UNTRACK(self);
(void)incrementalnewlinedecoder_clear(self);
tp->tp_free((PyObject *)self);
Py_DECREF(tp);
Expand Down Expand Up @@ -1456,10 +1455,11 @@ textiowrapper_dealloc(textio *self)
{
PyTypeObject *tp = Py_TYPE(self);
self->finalizing = 1;
if (_PyIOBase_finalize((PyObject *) self) < 0)
if (_PyIOBase_finalize((PyObject *) self) < 0) {
PyObject_GC_Track(self); // untracked by _Py_Dealloc
return;
}
self->ok = 0;
_PyObject_GC_UNTRACK(self);
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *)self);
(void)textiowrapper_clear(self);
Expand Down
5 changes: 3 additions & 2 deletions Modules/_io/winconsoleio.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,10 @@ winconsoleio_dealloc(winconsoleio *self)
{
PyTypeObject *tp = Py_TYPE(self);
self->finalizing = 1;
if (_PyIOBase_finalize((PyObject *) self) < 0)
if (_PyIOBase_finalize((PyObject *) self) < 0) {
PyObject_GC_Track(self); // untracked by _Py_Dealloc
return;
_PyObject_GC_UNTRACK(self);
}
if (self->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) self);
Py_CLEAR(self->dict);
Expand Down
1 change: 0 additions & 1 deletion Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,6 @@ typedef struct {
static void
bytearrayiter_dealloc(bytesiterobject *it)
{
_PyObject_GC_UNTRACK(it);
Py_XDECREF(it->it_seq);
PyObject_GC_Del(it);
}
Expand Down
1 change: 0 additions & 1 deletion Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3223,7 +3223,6 @@ typedef struct {
static void
striter_dealloc(striterobject *it)
{
_PyObject_GC_UNTRACK(it);
Py_XDECREF(it->it_seq);
PyObject_GC_Del(it);
}
Expand Down
1 change: 0 additions & 1 deletion Objects/cellobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ PyCell_Set(PyObject *op, PyObject *value)
static void
cell_dealloc(PyCellObject *op)
{
_PyObject_GC_UNTRACK(op);
Py_XDECREF(op->ob_ref);
PyObject_GC_Del(op);
}
Expand Down
2 changes: 0 additions & 2 deletions Objects/classobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ method_new_impl(PyTypeObject *type, PyObject *function, PyObject *instance)
static void
method_dealloc(PyMethodObject *im)
{
_PyObject_GC_UNTRACK(im);
if (im->im_weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *)im);
Py_DECREF(im->im_func);
Expand Down Expand Up @@ -432,7 +431,6 @@ instancemethod_getattro(PyObject *self, PyObject *name)

static void
instancemethod_dealloc(PyObject *self) {
_PyObject_GC_UNTRACK(self);
Py_DECREF(PyInstanceMethod_GET_FUNCTION(self));
PyObject_GC_Del(self);
}
Expand Down
2 changes: 0 additions & 2 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,6 @@ static void
mappingproxy_dealloc(PyObject *self)
{
mappingproxyobject *pp = (mappingproxyobject *)self;
_PyObject_GC_UNTRACK(pp);
Py_DECREF(pp->mapping);
PyObject_GC_Del(pp);
}
Expand Down Expand Up @@ -1632,7 +1631,6 @@ property_dealloc(PyObject *self)
{
propertyobject *gs = (propertyobject *)self;

_PyObject_GC_UNTRACK(self);
Py_XDECREF(gs->prop_get);
Py_XDECREF(gs->prop_set);
Py_XDECREF(gs->prop_del);
Expand Down
4 changes: 0 additions & 4 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5015,8 +5015,6 @@ static void
dictiter_dealloc(PyObject *self)
{
dictiterobject *di = (dictiterobject *)self;
/* bpo-31095: UnTrack is needed before calling any callbacks */
_PyObject_GC_UNTRACK(di);
Py_XDECREF(di->di_dict);
Py_XDECREF(di->di_result);
PyObject_GC_Del(di);
Expand Down Expand Up @@ -5815,8 +5813,6 @@ static void
dictview_dealloc(PyObject *self)
{
_PyDictViewObject *dv = (_PyDictViewObject *)self;
/* bpo-31095: UnTrack is needed before calling any callbacks */
_PyObject_GC_UNTRACK(dv);
Py_XDECREF(dv->dv_dict);
PyObject_GC_Del(dv);
}
Expand Down
10000 10 changes: 0 additions & 10 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,6 @@ SystemExit_clear(PySystemExitObject *self)
static void
SystemExit_dealloc(PySystemExitObject *self)
{
_PyObject_GC_UNTRACK(self);
SystemExit_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
Expand Down Expand Up @@ -887,7 +886,6 @@ BaseExceptionGroup_clear(PyBaseExceptionGroupObject *self)
static void
BaseExceptionGroup_dealloc(PyBaseExceptionGroupObject *self)
{
_PyObject_GC_UNTRACK(self);
BaseExceptionGroup_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
Expand Down Expand Up @@ -1605,7 +1603,6 @@ ImportError_clear(PyImportErrorObject *self)
static void
ImportError_dealloc(PyImportErrorObject *self)
{
_PyObject_GC_UNTRACK(self);
ImportError_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
Expand Down Expand Up @@ -1995,7 +1992,6 @@ OSError_clear(PyOSErrorObject *self)
static void
OSError_dealloc(PyOSErrorObject *self)
{
_PyObject_GC_UNTRACK(self);
OSError_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
Expand Down Expand Up @@ -2266,7 +2262,6 @@ NameError_clear(PyNameErrorObject *self)
static void
NameError_dealloc(PyNameErrorObject *self)
{
_PyObject_GC_UNTRACK(self);
NameError_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
Expand Down Expand Up @@ -2342,7 +2337,6 @@ AttributeError_clear(PyAttributeErrorObject *self)
static void
AttributeError_dealloc(PyAttributeErrorObject *self)
{
_PyObject_GC_UNTRACK(self);
AttributeError_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
Expand Down Expand Up @@ -2480,7 +2474,6 @@ SyntaxError_clear(PySyntaxErrorObject *self)
static void
SyntaxError_dealloc(PySyntaxErrorObject *self)
{
_PyObject_GC_UNTRACK(self);
SyntaxError_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
Expand Down Expand Up @@ -2930,7 +2923,6 @@ UnicodeError_clear(PyUnicodeErrorObject *self)
static void
UnicodeError_dealloc(PyUnicodeErrorObject *self)
{
_PyObject_GC_UNTRACK(self);
UnicodeError_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
Expand Down Expand Up @@ -3389,8 +3381,6 @@ _PyErr_NoMemory(PyThreadState *tstate)
static void
MemoryError_dealloc(PyBaseExceptionObject *self)
{
_PyObject_GC_UNTRACK(self);

BaseException_clear(self);

/* If this is a subclass of MemoryError, we don't need to
Expand Down
4 changes: 0 additions & 4 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1637,10 +1637,6 @@ frame_dealloc(PyFrameObject *f)
/* It is the responsibility of the owning generator/coroutine
* to have cleared the generator pointer */

if (_PyObject_GC_IS_TRACKED(f)) {
_PyObject_GC_UNTRACK(f);
}

Py_TRASHCAN_BEGIN(f, frame_dealloc);
/* GH-106092: If f->f_frame was on the stack and we reached the maximum
* nesting depth for deallocations, the trashcan may have delayed this
Expand Down
4 changes: 1 addition & 3 deletions Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1049,10 +1049,10 @@ func_dealloc(PyFunctionObject *op)
handle_func_event(PyFunction_EVENT_DESTROY, op, NULL);
if (Py_REFCNT(op) > 1) {
Py_SET_REFCNT(op, Py_REFCNT(op) - 1);
_PyObject_GC_TRACK(op); // untracked by _Py_Dealloc
return;
}
Py_SET_REFCNT(op, 0);
_PyObject_GC_UNTRACK(op);
if (op->func_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *) op);
}
Expand Down Expand Up @@ -1254,7 +1254,6 @@ typedef struct {
static void
cm_dealloc(classmethod *cm)
{
_PyObject_GC_UNTRACK((PyObject *)cm);
Py_XDECREF(cm->cm_callable);
Py_XDECREF(cm->cm_dict);
Py_TYPE(cm)->tp_free((PyObject *)cm);
Expand Down Expand Up @@ -1473,7 +1472,6 @@ typedef struct {
static void
sm_dealloc(staticmethod *sm)
{
_PyObject_GC_UNTRACK((PyObject *)sm);
Py_XDECREF(sm->sm_callable);
Py_XDECREF(sm->sm_dict);
Py_TYPE(sm)->tp_free((PyObject *)sm);
Expand Down
1 change: 0 additions & 1 deletion Objects/genericaliasobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ ga_dealloc(PyObject *self)
{
gaobject *alias = (gaobject *)self;

_PyObject_GC_UNTRACK(self);
if (alias->weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *)alias);
}
Expand Down
11 changes: 3 additions & 8 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,15 @@ gen_dealloc(PyGenObject *gen)
{
PyObject *self = (PyObject *) gen;

_PyObject_GC_UNTRACK(gen);

if (gen->gi_weakreflist != NULL)
PyObject_ClearWeakRefs(self);

_PyObject_GC_TRACK(self);
_PyObject_GC_TRACK(self); // _Py_Dealloc calls untrack

if (PyObject_CallFinalizerFromDealloc(self))
return; /* resurrected. :( */

_PyObject_GC_UNTRACK(self);
_PyObject_GC_UNTRACK(self); // tp_del requires this
if (PyAsyncGen_CheckExact(gen)) {
/* We have to handle this case for asynchronous generators
right here, because this code has to be between UNTRACK
Expand Down Expand Up @@ -1222,7 +1220,6 @@ PyTypeObject PyCoro_Type = {
static void
coro_wrapper_dealloc(PyCoroWrapper *cw)
{
_PyObject_GC_UNTRACK((PyObject *)cw);
Py_CLEAR(cw->cw_coroutine);
PyObject_GC_Del(cw);
}
Expand Down Expand Up @@ -1691,7 +1688,6 @@ async_gen_asend_dealloc(PyAsyncGenASend *o)
return;
}

_PyObject_GC_UNTRACK((PyObject *)o);
Py_CLEAR(o->ags_gen);
Py_CLEAR(o->ags_sendval);

Expand Down Expand Up @@ -1913,7 +1909,6 @@ async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
static void
async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o)
{
_PyObject_GC_UNTRACK((PyObject *)o);
Py_CLEAR(o->agw_val);
_Py_FREELIST_FREE(async_gens, o, PyObject_GC_Del);
}
Expand Down Expand Up @@ -1998,10 +1993,10 @@ static void
async_gen_athrow_dealloc(PyAsyncGenAThrow *o)
{
if (PyObject_CallFinalizerFromDealloc((PyObject *)o)) {
PyObject_GC_Track(o); // untracked by _Py_Dealloc
return;
}

_PyObject_GC_UNTRACK((PyObject *)o);
Py_CLEAR(o->agt_gen);
Py_CLEAR(o->agt_args);
PyObject_GC_Del(o);
Expand Down
3 changes: 0 additions & 3 deletions Objects/iterobject.c
C28E
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ PySeqIter_New(PyObject *seq)
static void
iter_dealloc(seqiterobject *it)
{
_PyObject_GC_UNTRACK(it);
Py_XDECREF(it->it_seq);
PyObject_GC_Del(it);
}
Expand Down Expand Up @@ -197,7 +196,6 @@ PyCallIter_New(PyObject *callable, PyObject *sentinel)
static void
calliter_dealloc(calliterobject *it)
{
_PyObject_GC_UNTRACK(it);
Py_XDECREF(it->it_callable);
Py_XDECREF(it->it_sentinel);
PyObject_GC_Del(it);
Expand Down Expand Up @@ -307,7 +305,6 @@ typedef struct {
static void
anextawaitable_dealloc(anextawaitableobject *obj)
{
_PyObject_GC_UNTRACK(obj);
Py_XDECREF(obj->wrapped);
Py_XDECREF(obj->default_value);
PyObject_GC_Del(obj);
Expand Down
1 change: 0 additions & 1 deletion Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3854,7 +3854,6 @@ static void
listiter_dealloc(PyObject *self)
{
_PyListIterObject *it = (_PyListIterObject *)self;
_PyObject_GC_UNTRACK(it);
Py_XDECREF(it->it_seq);
PyObject_GC_Del(it);
}
Expand Down
Loading
0