[Python-Dev] PEP 442 accepted

Nick Coghlan ncoghlan at gmail.com
Sat Jun 15 06:57:49 CEST 2013


On 15 June 2013 03:34, Antoine Pitrou <solipsis at pitrou.net> wrote:
> On Wed, 5 Jun 2013 09:10:54 -0700
> Benjamin Peterson <benjamin at python.org> wrote:
>> I (and Guido) are accepting PEP 442 (Safe object finalization) on the
>> condition that finalizers are only ever called once globally.
>
> Ok, so there's an issue with that condition: it can't be upholded on
> non-GC objects. Creating a non-GC object is quite obscure and rare,
> though, since it requires basically a class with no __dict__ and an
> empty __slots__:
>
> class C:
>     __slots__ = ()
>     survivors = []
>
>     def __del__(self):
>         self.survivors.append(self)
>
>
> In this case, a C instance's __del__ will be called every time
> destruction is attempted, not only once. Is that a realistic problem?

So, to trigger that __del__() method a second time, such an object
would have to be:

1. Defined in the first place (the use cases for stateless objects
with destructors seem rare...)
2. Hanging off a reference cycle
3. Which then gets resurrected

If it isn't easy to fix by changing the way such classes are
constructed by typeobject.c (i.e. force them to be GC-aware when they
define a __del__ method, since they may still be hanging off the edge
of a reference cycle, even if they can't form one themselves), I'd say
chalk it up as a CPython wart and not worry about it for now.

As far as the test_subtype_resurrection failure goes, I think the
associated comment says it all: "(On non-CPython implementations,
however, __del__ is typically not called again.)"

The test should just be rewritten to check that __del__ is only called
once in the face of resurrection.

Cheers,
Nick.

--
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list