]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132176: Fix crash on `type()` when `tuple` subclass passed as `bases` (#132212)
authorsobolevn <mail@sobolevn.me>
Tue, 15 Apr 2025 11:13:51 +0000 (14:13 +0300)
committerGitHub <noreply@github.com>
Tue, 15 Apr 2025 11:13:51 +0000 (14:13 +0300)
Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/test/test_types.py
Objects/typeobject.c

index d80d317aab1ddc73a13e3b8129616d7637ea0456..87081a6db4ea48021188d007b93cb633f3038e0d 100644 (file)
@@ -1838,6 +1838,15 @@ class ClassCreationTests(unittest.TestCase):
         with self.assertRaises(RuntimeWarning):
             type("SouthPonies", (Model,), {})
 
+    def test_tuple_subclass_as_bases(self):
+        # gh-132176: it used to crash on using
+        # tuple subclass for as base classes.
+        class TupleSubclass(tuple): pass
+
+        typ = type("typ", TupleSubclass((int, object)), {})
+        self.assertEqual(typ.__bases__, (int, object))
+        self.assertEqual(type(typ.__bases__), TupleSubclass)
+
 
 class SimpleNamespaceTests(unittest.TestCase):
 
index b817ae6e68438b57b92f4a4e0718ad196e6e90e2..3a7fad4681b2a10468bb93fa87a89ea0a1591345 100644 (file)
@@ -497,10 +497,11 @@ _PyType_GetBases(PyTypeObject *self)
 static inline void
 set_tp_bases(PyTypeObject *self, PyObject *bases, int initial)
 {
-    assert(PyTuple_CheckExact(bases));
+    assert(PyTuple_Check(bases));
     if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
         // XXX tp_bases can probably be statically allocated for each
         // static builtin type.
+        assert(PyTuple_CheckExact(bases));
         assert(initial);
         assert(self->tp_bases == NULL);
         if (PyTuple_GET_SIZE(bases) == 0) {