From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Tue, 15 Apr 2025 11:38:20 +0000 (+0200) Subject: [3.13] gh-132176: Fix crash on `type()` when `tuple` subclass passed as `bases` ... X-Git-Tag: v3.13.4~271 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6d48194d9f14b7d6a5ee069a7bd269c124c17d59;p=thirdparty%2FPython%2Fcpython.git [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 Co-authored-by: Victor Stinner --- diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 5e870e5545e4..332f0479c373 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -1778,6 +1778,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): diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 6e8064540e51..362ed1772e24 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) {