This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients cheryl.sabella, docs@python, nanjekyejoannah, ncoghlan, pitrou, plasticgap, vstinner
Date 2019-09-05.15:54:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
PyGen_NeedsFinalizing() was added by:

commit 49fd7fa4431da299196d74087df4a04f99f9c46f
Author: Thomas Wouters <>
Date:   Fri Apr 21 10:40:58 2006 +0000

    Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
    number of tests, all because of the codecs/_multibytecodecs issue described
    here (it's not a Py3K issue, just something Py3K discovers):
    Hye-Shik Chang promised to look for a fix, so no need to fix it here. The
    tests that are expected to break are:
    This merge fixes an actual test failure (test_weakref) in this branch,
    though, so I believe merging is the right thing to do anyway.

It was used in this gcmodule.c function:

/* Return true if object has a finalization method.
 * CAUTION:  An instance of an old-style class has to be checked for a
 *__del__ method, and earlier versions of this used to call PyObject_HasAttr,
 * which in turn could call the class's __getattr__ hook (if any).  That
 * could invoke arbitrary Python code, mutating the object graph in arbitrary
 * ways, and that was the source of some excruciatingly subtle bugs.
static int
has_finalizer(PyObject *op)
        if (PyInstance_Check(op)) {
                assert(delstr != NULL);
                return _PyInstance_Lookup(op, delstr) != NULL;
        else if (PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
                return op->ob_type->tp_del != NULL;
        else if (PyGen_CheckExact(op))
                return PyGen_NeedsFinalizing((PyGenObject *)op);
                return 0;

(2) The PEP 442 implementation made PyGen_NeedsFinalizing() useless:

commit 796564c27b8f2e32b9fbc034bbdda75f9507ca43
Author: Antoine Pitrou <>
Date:   Tue Jul 30 19:59:21 2013 +0200

    Issue #18112: PEP 442 implementation (safe object finalization).


/* Return true if object has a finalization method. */
static int
has_finalizer(PyObject *op)
    if (PyGen_CheckExact(op))
        return PyGen_NeedsFinalizing((PyGenObject *)op);
        return op->ob_type->tp_del != NULL;


/* Return true if object has a pre-PEP 442 finalization method. */
static int
has_legacy_finalizer(PyObject *op)
    return op->ob_type->tp_del != NULL;


The last reference to PyGen_NeedsFinalizing() can be found in ceval.c:

        case TARGET(SETUP_FINALLY): {
            /* NOTE: If you add any new block-setup opcodes that
               are not try/except/finally handlers, you may need
               to update the PyGen_NeedsFinalizing() function.

            PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Date User Action Args
2019-09-05 15:54:22vstinnersetrecipients: + vstinner, ncoghlan, pitrou, docs@python, plasticgap, cheryl.sabella, nanjekyejoannah
2019-09-05 15:54:22vstinnersetmessageid: <>
2019-09-05 15:54:22vstinnerlinkissue15088 messages
2019-09-05 15:54:22vstinnercreate