From: Neal Norwitz Date: Tue, 24 Dec 2002 14:48:41 +0000 (+0000) Subject: Fix SF #658106, Setting __class__ to NoneType X-Git-Tag: v2.2.3c1~197 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=89b8e840726799007fd62ab4f7233ddd6662f102;p=thirdparty%2FPython%2Fcpython.git 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. --- 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; }