[Python-Dev] PEP 442 accepted

Antoine Pitrou solipsis at pitrou.net
Sat Jun 15 12:55:57 CEST 2013


On Sat, 15 Jun 2013 14:57:49 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:

> 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

They don't need to hang off a reference cycle. You can resurrect a
non-cyclic object from __del__ too.

But, yeah, stateless objects must be pretty rare, since by definition
they cannot represent anything (except perhaps "nothing").

> 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

Objects which hang off the edge of a reference cycle don't need to be
GC-aware, since traversing them isn't needed to be break the cycle.
I think "chalking it up as a CPython wart" is a reasonable solution, I
just wanted to ask.

Regards

Antoine.


More information about the Python-Dev mailing list