Skip to content

Commit 40bf263

Browse files
committed
gh-132176: Fix crash on type() when tuple subclass passed as bases
1 parent f2daa96 commit 40bf263

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Lib/test/test_types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,19 @@ class Model(metaclass=ModelBase):
18191819
with self.assertRaises(RuntimeWarning):
18201820
type("SouthPonies", (Model,), {})
18211821

1822+
def test_tuple_subclass_as_bases(self):
1823+
# gh-132176: it used to crash on using
1824+
# tuple subclass for as base classes.
1825+
class TupleSubclass(tuple): pass
1826+
1827+
typ = type("typ", TupleSubclass((int, object)), {})
1828+
self.assertTrue(issubclass(typ, int))
1829+
self.assertTrue(issubclass(typ, object))
1830+
self.assertFalse(issubclass(typ, tuple))
1831+
self.assertIsInstance(typ(), int)
1832+
self.assertIsInstance(typ(), object)
1833+
self.assertNotIsInstance(typ(), tuple)
1834+
18221835

18231836
class SimpleNamespaceTests(unittest.TestCase):
18241837

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash when :func:`type` receives a :class:`tuple` subclass as the
2+
second *bases* argument.

Objects/typeobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,11 @@ _PyType_GetBases(PyTypeObject *self)
497497
static inline void
498498
set_tp_bases(PyTypeObject *self, PyObject *bases, int initial)
499499
{
500-
assert(PyTuple_CheckExact(bases));
500+
assert(PyTuple_Check(bases));
501501
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
502502
// XXX tp_bases can probably be statically allocated for each
503503
// static builtin type.
504+
assert(PyTuple_CheckExact(bases));
504505
assert(initial);
505506
assert(self->tp_bases == NULL);
506507
if (PyTuple_GET_SIZE(bases) == 0) {

0 commit comments

Comments
 (0)