mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Issue #18112: PEP 442 implementation (safe object finalization).
This commit is contained in:
parent
c5d95b17ac
commit
796564c27b
25 changed files with 1254 additions and 321 deletions
|
@ -15,6 +15,31 @@ gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
gen_finalize(PyObject *self)
|
||||
{
|
||||
PyGenObject *gen = (PyGenObject *)self;
|
||||
PyObject *res;
|
||||
PyObject *error_type, *error_value, *error_traceback;
|
||||
|
||||
if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
|
||||
/* Generator isn't paused, so no need to close */
|
||||
return;
|
||||
|
||||
/* Save the current exception, if any. */
|
||||
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
||||
|
||||
res = gen_close(gen, NULL);
|
||||
|
||||
if (res == NULL)
|
||||
PyErr_WriteUnraisable(self);
|
||||
else
|
||||
Py_DECREF(res);
|
||||
|
||||
/* Restore the saved exception. */
|
||||
PyErr_Restore(error_type, error_value, error_traceback);
|
||||
}
|
||||
|
||||
static void
|
||||
gen_dealloc(PyGenObject *gen)
|
||||
{
|
||||
|
@ -27,12 +52,8 @@ gen_dealloc(PyGenObject *gen)
|
|||
|
||||
_PyObject_GC_TRACK(self);
|
||||
|
||||
if (gen->gi_frame != NULL && gen->gi_frame->f_stacktop != NULL) {
|
||||
/* Generator is paused, so we need to close */
|
||||
Py_TYPE(gen)->tp_del(self);
|
||||
if (self->ob_refcnt > 0)
|
||||
return; /* resurrected. :( */
|
||||
}
|
||||
if (PyObject_CallFinalizerFromDealloc(self))
|
||||
return; /* resurrected. :( */
|
||||
|
||||
_PyObject_GC_UNTRACK(self);
|
||||
Py_CLEAR(gen->gi_frame);
|
||||
|
@ -40,7 +61,6 @@ gen_dealloc(PyGenObject *gen)
|
|||
PyObject_GC_Del(gen);
|
||||
}
|
||||
|
||||
|
||||
static PyObject *
|
||||
gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
|
||||
{
|
||||
|
@ -222,68 +242,6 @@ gen_close(PyGenObject *gen, PyObject *args)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
gen_del(PyObject *self)
|
||||
{
|
||||
PyObject *res;
|
||||
PyObject *error_type, *error_value, *error_traceback;
|
||||
PyGenObject *gen = (PyGenObject *)self;
|
||||
|
||||
if (gen->gi_frame == NULL || gen->gi_frame->f_stacktop == NULL)
|
||||
/* Generator isn't paused, so no need to close */
|
||||
return;
|
||||
|
||||
/* Temporarily resurrect the object. */
|
||||
assert(self->ob_refcnt == 0);
|
||||
self->ob_refcnt = 1;
|
||||
|
||||
/* Save the current exception, if any. */
|
||||
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
||||
|
||||
res = gen_close(gen, NULL);
|
||||
|
||||
if (res == NULL)
|
||||
PyErr_WriteUnraisable(self);
|
||||
else
|
||||
Py_DECREF(res);
|
||||
|
||||
/* Restore the saved exception. */
|
||||
PyErr_Restore(error_type, error_value, error_traceback);
|
||||
|
||||
/* Undo the temporary resurrection; can't use DECREF here, it would
|
||||
* cause a recursive call.
|
||||
*/
|
||||
assert(self->ob_refcnt > 0);
|
||||
if (--self->ob_refcnt == 0)
|
||||
return; /* this is the normal path out */
|
||||
|
||||
/* close() resurrected it! Make it look like the original Py_DECREF
|
||||
* never happened.
|
||||
*/
|
||||
{
|
||||
Py_ssize_t refcnt = self->ob_refcnt;
|
||||
_Py_NewReference(self);
|
||||
self->ob_refcnt = refcnt;
|
||||
}
|
||||
assert(PyType_IS_GC(Py_TYPE(self)) &&
|
||||
_Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
|
||||
|
||||
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
|
||||
* we need to undo that. */
|
||||
_Py_DEC_REFTOTAL;
|
||||
/* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
|
||||
* chain, so no more to do there.
|
||||
* If COUNT_ALLOCS, the original decref bumped tp_frees, and
|
||||
* _Py_NewReference bumped tp_allocs: both of those need to be
|
||||
* undone.
|
||||
*/
|
||||
#ifdef COUNT_ALLOCS
|
||||
--(Py_TYPE(self)->tp_frees);
|
||||
--(Py_TYPE(self)->tp_allocs);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
PyDoc_STRVAR(throw_doc,
|
||||
"throw(typ[,val[,tb]]) -> raise exception in generator,\n\
|
||||
|
@ -517,7 +475,8 @@ PyTypeObject PyGen_Type = {
|
|||
PyObject_GenericGetAttr, /* tp_getattro */
|
||||
0, /* tp_setattro */
|
||||
0, /* tp_as_buffer */
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
||||
Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
|
||||
0, /* tp_doc */
|
||||
(traverseproc)gen_traverse, /* tp_traverse */
|
||||
0, /* tp_clear */
|
||||
|
@ -544,7 +503,9 @@ PyTypeObject PyGen_Type = {
|
|||
0, /* tp_cache */
|
||||
0, /* tp_subclasses */
|
||||
0, /* tp_weaklist */
|
||||
gen_del, /* tp_del */
|
||||
0, /* tp_del */
|
||||
0, /* tp_version_tag */
|
||||
gen_finalize, /* tp_finalize */
|
||||
};
|
||||
|
||||
PyObject *
|
||||
|
|
|
@ -255,6 +255,72 @@ _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
|
|||
return PyObject_INIT_VAR(op, tp, nitems);
|
||||
}
|
||||
|
||||
void
|
||||
PyObject_CallFinalizer(PyObject *self)
|
||||
{
|
||||
PyTypeObject *tp = Py_TYPE(self);
|
||||
|
||||
/* The former could happen on heaptypes created from the C API, e.g.
|
||||
PyType_FromSpec(). */
|
||||
if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_FINALIZE) ||
|
||||
tp->tp_finalize == NULL)
|
||||
return;
|
||||
/* tp_finalize should only be called once. */
|
||||
if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
|
||||
return;
|
||||
|
||||
tp->tp_finalize(self);
|
||||
if (PyType_IS_GC(tp))
|
||||
_PyGC_SET_FINALIZED(self, 1);
|
||||
}
|
||||
|
||||
int
|
||||
PyObject_CallFinalizerFromDealloc(PyObject *self)
|
||||
{
|
||||
Py_ssize_t refcnt;
|
||||
|
||||
/* Temporarily resurrect the object. */
|
||||
if (self->ob_refcnt != 0) {
|
||||
Py_FatalError("PyObject_CallFinalizerFromDealloc called on "
|
||||
"object with a non-zero refcount");
|
||||
}
|
||||
self->ob_refcnt = 1;
|
||||
|
||||
PyObject_CallFinalizer(self);
|
||||
|
||||
/* Undo the temporary resurrection; can't use DECREF here, it would
|
||||
* cause a recursive call.
|
||||
*/
|
||||
assert(self->ob_refcnt > 0);
|
||||
if (--self->ob_refcnt == 0)
|
||||
return 0; /* this is the normal path out */
|
||||
|
||||
/* tp_finalize resurrected it! Make it look like the original Py_DECREF
|
||||
* never happened.
|
||||
*/
|
||||
refcnt = self->ob_refcnt;
|
||||
_Py_NewReference(self);
|
||||
self->ob_refcnt = refcnt;
|
||||
|
||||
if (PyType_IS_GC(Py_TYPE(self))) {
|
||||
assert(_PyGC_REFS(self) != _PyGC_REFS_UNTRACKED);
|
||||
}
|
||||
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
|
||||
* we need to undo that. */
|
||||
_Py_DEC_REFTOTAL;
|
||||
/* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
|
||||
* chain, so no more to do there.
|
||||
* If COUNT_ALLOCS, the original decref bumped tp_frees, and
|
||||
* _Py_NewReference bumped tp_allocs: both of those need to be
|
||||
* undone.
|
||||
*/
|
||||
#ifdef COUNT_ALLOCS
|
||||
--Py_TYPE(self)->tp_frees;
|
||||
--Py_TYPE(self)->tp_allocs;
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
PyObject_Print(PyObject *op, FILE *fp, int flags)
|
||||
{
|
||||
|
@ -1981,7 +2047,7 @@ void
|
|||
_PyTrash_deposit_object(PyObject *op)
|
||||
{
|
||||
assert(PyObject_IS_GC(op));
|
||||
assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED);
|
||||
assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
|
||||
assert(op->ob_refcnt == 0);
|
||||
_Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
|
||||
_PyTrash_delete_later = op;
|
||||
|
@ -1993,7 +2059,7 @@ _PyTrash_thread_deposit_object(PyObject *op)
|
|||
{
|
||||
PyThreadState *tstate = PyThreadState_GET();
|
||||
assert(PyObject_IS_GC(op));
|
||||
assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED);
|
||||
assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
|
||||
assert(op->ob_refcnt == 0);
|
||||
_Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later;
|
||||
tstate->trash_delete_later = op;
|
||||
|
|
|
@ -921,6 +921,7 @@ subtype_dealloc(PyObject *self)
|
|||
PyTypeObject *type, *base;
|
||||
destructor basedealloc;
|
||||
PyThreadState *tstate = PyThreadState_GET();
|
||||
int has_finalizer;
|
||||
|
||||
/* Extract the type; we expect it to be a heap type */
|
||||
type = Py_TYPE(self);
|
||||
|
@ -936,6 +937,10 @@ subtype_dealloc(PyObject *self)
|
|||
clear_slots(), or DECREF the dict, or clear weakrefs. */
|
||||
|
||||
/* Maybe call finalizer; exit early if resurrected */
|
||||
if (type->tp_finalize) {
|
||||
if (PyObject_CallFinalizerFromDealloc(self) < 0)
|
||||
return;
|
||||
}
|
||||
if (type->tp_del) {
|
||||
type->tp_del(self);
|
||||
if (self->ob_refcnt > 0)
|
||||
|
@ -987,25 +992,36 @@ subtype_dealloc(PyObject *self)
|
|||
assert(base);
|
||||
}
|
||||
|
||||
/* If we added a weaklist, we clear it. Do this *before* calling
|
||||
the finalizer (__del__), clearing slots, or clearing the instance
|
||||
dict. */
|
||||
has_finalizer = type->tp_finalize || type->tp_del;
|
||||
|
||||
/* Maybe call finalizer; exit early if resurrected */
|
||||
if (has_finalizer)
|
||||
_PyObject_GC_TRACK(self);
|
||||
|
||||
if (type->tp_finalize) {
|
||||
if (PyObject_CallFinalizerFromDealloc(self) < 0) {
|
||||
/* Resurrected */
|
||||
goto endlabel;
|
||||
}
|
||||
}
|
||||
/* If we added a weaklist, we clear it. Do this *before* calling
|
||||
tp_del, clearing slots, or clearing the instance dict. */
|
||||
if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
|
||||
PyObject_ClearWeakRefs(self);
|
||||
|
||||
/* Maybe call finalizer; exit early if resurrected */
|
||||
if (type->tp_del) {
|
||||
_PyObject_GC_TRACK(self);
|
||||
type->tp_del(self);
|
||||
if (self->ob_refcnt > 0)
|
||||
goto endlabel; /* resurrected */
|
||||
else
|
||||
_PyObject_GC_UNTRACK(self);
|
||||
if (self->ob_refcnt > 0) {
|
||||
/* Resurrected */
|
||||
goto endlabel;
|
||||
}
|
||||
}
|
||||
if (has_finalizer) {
|
||||
_PyObject_GC_UNTRACK(self);
|
||||
/* New weakrefs could be created during the finalizer call.
|
||||
If this occurs, clear them out without calling their
|
||||
finalizers since they might rely on part of the object
|
||||
being finalized that has already been destroyed. */
|
||||
If this occurs, clear them out without calling their
|
||||
finalizers since they might rely on part of the object
|
||||
being finalized that has already been destroyed. */
|
||||
if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
|
||||
/* Modeled after GET_WEAKREFS_LISTPTR() */
|
||||
PyWeakReference **list = (PyWeakReference **) \
|
||||
|
@ -2231,7 +2247,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
|
|||
|
||||
/* Initialize tp_flags */
|
||||
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
|
||||
Py_TPFLAGS_BASETYPE;
|
||||
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE;
|
||||
if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
|
||||
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
|
||||
|
||||
|
@ -4111,6 +4127,10 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base)
|
|||
COPYSLOT(tp_init);
|
||||
COPYSLOT(tp_alloc);
|
||||
COPYSLOT(tp_is_gc);
|
||||
if ((type->tp_flags & Py_TPFLAGS_HAVE_FINALIZE) &&
|
||||
(base->tp_flags & Py_TPFLAGS_HAVE_FINALIZE)) {
|
||||
COPYSLOT(tp_finalize);
|
||||
}
|
||||
if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
|
||||
(base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
|
||||
/* They agree about gc. */
|
||||
|
@ -4736,6 +4756,18 @@ wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
|
|||
return (*func)(self, args, kwds);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
wrap_del(PyObject *self, PyObject *args, void *wrapped)
|
||||
{
|
||||
destructor func = (destructor)wrapped;
|
||||
|
||||
if (!check_num_args(args, 0))
|
||||
return NULL;
|
||||
|
||||
(*func)(self);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
|
||||
{
|
||||
|
@ -5617,16 +5649,12 @@ slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||
}
|
||||
|
||||
static void
|
||||
slot_tp_del(PyObject *self)
|
||||
slot_tp_finalize(PyObject *self)
|
||||
{
|
||||
_Py_IDENTIFIER(__del__);
|
||||
PyObject *del, *res;
|
||||
PyObject *error_type, *error_value, *error_traceback;
|
||||
|
||||
/* Temporarily resurrect the object. */
|
||||
assert(self->ob_refcnt == 0);
|
||||
self->ob_refcnt = 1;
|
||||
|
||||
/* Save the current exception, if any. */
|
||||
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
||||
|
||||
|
@ -5643,37 +5671,6 @@ slot_tp_del(PyObject *self)
|
|||
|
||||
/* Restore the saved exception. */
|
||||
PyErr_Restore(error_type, error_value, error_traceback);
|
||||
|
||||
/* Undo the temporary resurrection; can't use DECREF here, it would
|
||||
* cause a recursive call.
|
||||
*/
|
||||
assert(self->ob_refcnt > 0);
|
||||
if (--self->ob_refcnt == 0)
|
||||
return; /* this is the normal path out */
|
||||
|
||||
/* __del__ resurrected it! Make it look like the original Py_DECREF
|
||||
* never happened.
|
||||
*/
|
||||
{
|
||||
Py_ssize_t refcnt = self->ob_refcnt;
|
||||
_Py_NewReference(self);
|
||||
self->ob_refcnt = refcnt;
|
||||
}
|
||||
assert(!PyType_IS_GC(Py_TYPE(self)) ||
|
||||
_Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
|
||||
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
|
||||
* we need to undo that. */
|
||||
_Py_DEC_REFTOTAL;
|
||||
/* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
|
||||
* chain, so no more to do there.
|
||||
* If COUNT_ALLOCS, the original decref bumped tp_frees, and
|
||||
* _Py_NewReference bumped tp_allocs: both of those need to be
|
||||
* undone.
|
||||
*/
|
||||
#ifdef COUNT_ALLOCS
|
||||
--Py_TYPE(self)->tp_frees;
|
||||
--Py_TYPE(self)->tp_allocs;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
@ -5782,7 +5779,7 @@ static slotdef slotdefs[] = {
|
|||
"see help(type(x)) for signature",
|
||||
PyWrapperFlag_KEYWORDS),
|
||||
TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
|
||||
TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
|
||||
TPSLOT("__del__", tp_finalize, slot_tp_finalize, (wrapperfunc)wrap_del, ""),
|
||||
|
||||
BINSLOT("__add__", nb_add, slot_nb_add,
|
||||
"+"),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue