https://github.com/python/cpython/commit/b6c552f9e614bab4acf21584baed997f57e74114
commit: b6c552f9e614bab4acf21584baed997f57e74114
branch: main
author: sobolevn <m...@sobolevn.me>
committer: sobolevn <m...@sobolevn.me>
date: 2025-04-15T14:13:51+03:00
summary:

gh-132176: Fix crash on `type()` when `tuple` subclass passed as `bases` 
(#132212)

Co-authored-by: Victor Stinner <vstin...@python.org>

files:
M Lib/test/test_types.py
M Objects/typeobject.c

diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index d80d317aab1ddc..87081a6db4ea48 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -1838,6 +1838,15 @@ class Model(metaclass=ModelBase):
         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):
 
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index b817ae6e68438b..3a7fad4681b2a1 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -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) {

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to