[docs] [issue15088] PyGen_NeedsFinalizing is public, but undocumented

STINNER Victor report at bugs.python.org
Thu Sep 5 11:54:22 EDT 2019


STINNER Victor <vstinner at python.org> added the comment:

PyGen_NeedsFinalizing() was added by:

commit 49fd7fa4431da299196d74087df4a04f99f9c46f
Author: Thomas Wouters <thomas at python.org>
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):
    http://mail.python.org/pipermail/python-dev/2006-April/064051.html
    
    Hye-Shik Chang promised to look for a fix, so no need to fix it here. The
    tests that are expected to break are:
    
    test_codecencodings_cn
    test_codecencodings_hk
    test_codecencodings_jp
    test_codecencodings_kr
    test_codecencodings_tw
    test_codecs
    test_multibytecodec
    
    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);
        else
                return 0;
}


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

commit 796564c27b8f2e32b9fbc034bbdda75f9507ca43
Author: Antoine Pitrou <solipsis at pitrou.net>
Date:   Tue Jul 30 19:59:21 2013 +0200

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

Replaced:

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

with:

/* 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,
                               STACK_LEVEL());
            DISPATCH();
        }

----------
nosy: +vstinner

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue15088>
_______________________________________


More information about the docs mailing list