[issue8212] A tp_dealloc of a subclassed class cannot resurrect an object
Kristján Valur Jónsson
report at bugs.python.org
Tue Mar 23 14:28:32 CET 2010
New submission from Kristján Valur Jónsson <kristjan at ccpgames.com>:
The tp_dealloc of a type can chose to resurrect an object. the subtype_dealloc() in typeobject.c does this when it calls the tp_del() member and it has increased the refcount.
The problem is, that if you subclass a custom C object, and that C object's tp_dealloc() chooses to resurrect a dying object, that doesn't go well with subtype_dealloc().
After calling basedealloc() (line 1002 in typeobject.c), the object's type is decrefed. But if the object was resurrected by basedealloc() this shouldn't have been done. The object will be alive, but the type will be missing a reference. This will cause a crash later.
This could be fixable if we knew somehow after calling basedealloc() if the object were still alive. But we cannot check "self" because it may have died.
The only way out of this conundrum that I can see is to change the signature of tp_dealloc() to return a flag, whether it did actually delete the object or not.
Of course, I see no easy way around not clearing the slots, but the clearing of the dict could be postponed until after the call to basedealloc().
Since tp_dealloc _can_ resurrect objects (subtype_dealloc does it), subclassing such objects should work, and not crash the interpreter if the base class' dp_dealloc() decides to do so.
title: A tp_dealloc of a subclassed class cannot resurrect an object
versions: Python 2.7
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list