https://github.com/python/cpython/commit/6d48194d9f14b7d6a5ee069a7bd269c124c17d59
commit: 6d48194d9f14b7d6a5ee069a7bd269c124c17d59
branch: 3.13
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: sobolevn <m...@sobolevn.me>
date: 2025-04-15T11:38:20Z
summary:

[3.13] gh-132176: Fix crash on `type()` when `tuple` subclass passed as `bases` 
(GH-132212) (#132548)

gh-132176: Fix crash on `type()` when `tuple` subclass passed as `bases` 
(GH-132212)
(cherry picked from commit b6c552f9e614bab4acf21584baed997f57e74114)

Co-authored-by: sobolevn <m...@sobolevn.me>
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 5e870e5545e48c..332f0479c3732e 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -1778,6 +1778,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 6e8064540e5179..362ed1772e24f0 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -439,10 +439,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