]> granicus.if.org Git - python/commitdiff
prevent unacceptable bases from becoming bases through multiple inheritance (#24806)
authorBenjamin Peterson <benjamin@python.org>
Wed, 7 Oct 2015 02:36:54 +0000 (19:36 -0700)
committerBenjamin Peterson <benjamin@python.org>
Wed, 7 Oct 2015 02:36:54 +0000 (19:36 -0700)
Lib/test/test_descr.py
Misc/NEWS
Objects/typeobject.c

index 9a60a12c2983dc178e3af8d1e99938876ae30394..adce6e590516188e53ffbf7fc134282992727187 100644 (file)
@@ -3735,6 +3735,37 @@ order (MRO) for bases """
         else:
             assert 0, "best_base calculation found wanting"
 
+    def test_unsubclassable_types(self):
+        with self.assertRaises(TypeError):
+            class X(type(None)):
+                pass
+        with self.assertRaises(TypeError):
+            class X(object, type(None)):
+                pass
+        with self.assertRaises(TypeError):
+            class X(type(None), object):
+                pass
+        class O(object):
+            pass
+        with self.assertRaises(TypeError):
+            class X(O, type(None)):
+                pass
+        with self.assertRaises(TypeError):
+            class X(type(None), O):
+                pass
+
+        class X(object):
+            pass
+        with self.assertRaises(TypeError):
+            X.__bases__ = type(None),
+        with self.assertRaises(TypeError):
+            X.__bases__ = object, type(None)
+        with self.assertRaises(TypeError):
+            X.__bases__ = type(None), object
+        with self.assertRaises(TypeError):
+            X.__bases__ = O, type(None)
+        with self.assertRaises(TypeError):
+            X.__bases__ = type(None), O
 
     def test_mutable_bases_with_failing_mro(self):
         # Testing mutable bases with failing mro...
index 70f3a5155a70ea66e3b3bf4d89b4b6f9f5e8582c..f393d68544a3e2d1f7a5b1762ed324a3bbc351a5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: tba
 Core and Builtins
 -----------------
 
+- Issue #24806: Prevent builtin types that are not allowed to be subclassed from
+  being subclassed through multiple inheritance.
+
 - Issue #24848: Fixed a number of bugs in UTF-7 decoding of misformed data.
 
 - Issue #25280: Import trace messages emitted in verbose (-v) mode are no
index d39c9431ea5aab8ad8cef7bd6a6d95b17e623809..cf4e4e57dac526e8f7692ca45691c7311427e670 100644 (file)
@@ -1937,6 +1937,12 @@ best_base(PyObject *bases)
             if (PyType_Ready(base_i) < 0)
                 return NULL;
         }
+        if (!PyType_HasFeature(base_i, Py_TPFLAGS_BASETYPE)) {
+            PyErr_Format(PyExc_TypeError,
+                         "type '%.100s' is not an acceptable base type",
+                         base_i->tp_name);
+            return NULL;
+        }
         candidate = solid_base(base_i);
         if (winner == NULL) {
             winner = candidate;
@@ -2317,12 +2323,6 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
     if (base == NULL) {
         goto error;
     }
-    if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
-        PyErr_Format(PyExc_TypeError,
-                     "type '%.100s' is not an acceptable base type",
-                     base->tp_name);
-        goto error;
-    }
 
     dict = PyDict_Copy(orig_dict);
     if (dict == NULL)