From 89b8e840726799007fd62ab4f7233ddd6662f102 Mon Sep 17 00:00:00 2001 From: Neal Norwitz Date: Tue, 24 Dec 2002 14:48:41 +0000 Subject: [PATCH] Fix SF #658106, Setting __class__ to NoneType Backport Guido's checkin 2.171: Disallow class assignment completely unless both old and new are heap types. This prevents nonsense like 2.__class__ = bool or True.__class__ = int. --- Objects/typeobject.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index e8ba871872ff..8b08bc0749c9 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1702,6 +1702,13 @@ object_set_class(PyObject *self, PyObject *value, void *closure) return -1; } new = (PyTypeObject *)value; + if (!(new->tp_flags & Py_TPFLAGS_HEAPTYPE) || + !(old->tp_flags & Py_TPFLAGS_HEAPTYPE)) + { + PyErr_Format(PyExc_TypeError, + "__class__ assignment: only for heap types"); + return -1; + } newbase = new; oldbase = old; while (equiv_structs(newbase, newbase->tp_base)) @@ -1718,13 +1725,9 @@ object_set_class(PyObject *self, PyObject *value, void *closure) old->tp_name); return -1; } - if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) { - Py_INCREF(new); - } + Py_INCREF(new); self->ob_type = new; - if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) { - Py_DECREF(old); - } + Py_DECREF(old); return 0; } -- 2.47.3