[Python-Dev] Counting references to Py_None

Tim Peters tim.peters at gmail.com
Mon Mar 21 17:56:06 EDT 2016

Brett Cannon <brett at python.org>]
>> And if we didn't keep its count accurately it would eventually hit
>> zero and constantly have its dealloc function checked for.

[Armin Rigo]
[> I think the idea is really consistency.  If we wanted to avoid all
> "Py_INCREF(Py_None);", it would be possible: we could let the refcount
> of None decrement to zero, at which point its deallocator is called;
> but this deallocator can simply bumps the refcount to a large value
> again.  The deallocator would end up being called very rarely.

It could, but it does something else now ;-)  I've seen this trigger,
from C code that had no idea it was playing with None, but just had
general refcounting errors.  So this does serve a debugging purpose,
although rarely:

static void
none_dealloc(PyObject* ignore)
    /* This should never get called, but we also don't want to SEGV if
     * we accidentally decref None out of existence.
    Py_FatalError("deallocating None");

(that's in object.c)

More information about the Python-Dev mailing list