On 04/02, Armin Rigo wrote:
Hi all,
On 20 March 2017 at 22:28, Nathaniel Smith <njs@pobox.com> wrote:
Modern CPython, and all extant versions of PyPy and Jython, guarantee that __del__ is called at most once.
Just a note, if someone actually depends on this: it is not true in all cases. For example, in CPython 3.5.3:
class X: ... __slots__=() # <= note this! ... def __del__(self): ... print("DEL") ... global resurrect ... resurrect = self ... print(X()) <__main__.X object at 0x7f5d1ad600d0> DEL resurrect=None DEL
Objects/typeobject.c:type_new() /* Enable GC unless this class is not adding new instance variables and the base class did not use GC. */ if ((base->tp_flags & Py_TPFLAGS_HAVE_GC) || type->tp_basicsize > base->tp_basicsize) type->tp_flags |= Py_TPFLAGS_HAVE_GC; That is. "type->tp_basicsize > base->tp_basicsize" is false if empty __slots__, so we do not set HAVE_GC. And PyObject_CallFinalizer() doesn't set FINALIZED if PyType_IS_GC(tp) == F. Oleg.