Index: Lib/test/test_descr.py =================================================================== --- Lib/test/test_descr.py (revision 69209) +++ Lib/test/test_descr.py (working copy) @@ -2958,6 +2958,16 @@ continue cant(cls(), cls2) + # Issue5283: when __class__ changes in __del__, the wrong + # type gets DECREF'd. + class O(object): + pass + class A(object): + def __del__(self): + self.__class__ = O + l = [A() for x in range(100)] + del l + def test_set_dict(self): # Testing __dict__ assignment... class C(object): pass Index: Objects/typeobject.c =================================================================== --- Objects/typeobject.c (revision 69209) +++ Objects/typeobject.c (working copy) @@ -928,6 +928,9 @@ assert(base); } + /* Extract the type again; tp_del may have changed it */ + type = Py_TYPE(self); + /* Call the base tp_dealloc() */ assert(basedealloc); basedealloc(self); @@ -1009,6 +1012,9 @@ } } + /* Extract the type again; tp_del may have changed it */ + type = Py_TYPE(self); + /* Call the base tp_dealloc(); first retrack self if * basedealloc knows about gc. */