[Python-checkins] cpython (merge 3.4 -> 3.5): merge 3.4 (#24806)

benjamin.peterson python-checkins at python.org
Tue Oct 6 22:42:56 EDT 2015


https://hg.python.org/cpython/rev/e02e4afcce6a
changeset:   98574:e02e4afcce6a
branch:      3.5
parent:      98569:95a26798819b
parent:      98573:e670b37e7b14
user:        Benjamin Peterson <benjamin at python.org>
date:        Tue Oct 06 19:42:02 2015 -0700
summary:
  merge 3.4 (#24806)

files:
  Lib/test/test_descr.py |  31 ++++++++++++++++++++++++++++++
  Misc/NEWS              |   3 ++
  Objects/typeobject.c   |  12 +++++-----
  3 files changed, 40 insertions(+), 6 deletions(-)


diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -3798,6 +3798,37 @@
         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...
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -11,6 +11,9 @@
 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
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -1965,6 +1965,12 @@
             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;
@@ -2345,12 +2351,6 @@
     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)

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list