[Python-Dev] PEP 442 clarification for type hierarchies

Antoine Pitrou solipsis at pitrou.net
Tue Aug 6 17:49:08 CEST 2013


Le Tue, 06 Aug 2013 17:18:59 +0200,
Stefan Behnel <stefan_ml at behnel.de> a écrit :

> Antoine Pitrou, 06.08.2013 14:12:
> > Le Mon, 05 Aug 2013 22:30:29 +0200,
> > Stefan Behnel a écrit :
> >>
> >> Hmm, it's a bit unfortunate that tp_finalize() maps so directly to
> >> __del__(), but I think this can be fixed. In any case, each
> >> tp_finalize() function must only ever be called once, so if a
> >> subtype inherited the tp_finalize() slot from its parent, it
> >> mustn't be called again.
> > 
> > This is already dealt with by a custom bit in the GC header (cf.
> > _PyGC_IS_FINALIZED, IIRC).
> 
> But that's only at an instance level. If a type in the hierarchy
> inherited the slot function for tp_finalize() from its parent, then
> the child must skip its parent in the call chain to prevent calling
> the same slot function twice. No instance flag can help you here.

Ah, sorry. I had misunderstood what you were talking about.
Yes, you're right, a tp_finalize implementation should avoid calling
itself recursively.
If there's some C API that can be added to ease it, I'm ok for adding
it.

> Maybe I'm wrong here. It's the default implementation of
> tp_finalize() that calls __del__, right?

Yes.

> If a Python class with a
> __del__ inherits from an extension type that implements
> tp_finalize(), then whose tp_finalize() will be executed first?

Then only the Python __del__ gets called. It should call
super().__del__() manually, to ensure the extension type's
tp_finalize gets called.

Regards

Antoine.




More information about the Python-Dev mailing list