Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import types
import unittest
import warnings
from contextlib import ExitStack
from contextlib import ExitStack, suppress
from functools import partial
from inspect import CO_COROUTINE
from itertools import product
Expand Down Expand Up @@ -2343,6 +2343,21 @@ def test_namespace_order(self):
C = type('C', (), od)
self.assertEqual(list(C.__dict__.items())[:2], [('b', 2), ('a', 1)])

def test_new_type_bad_tp_new(self):
# bpo-44232: Bad tp_new in bases tuple should just return NULL
# instead of causing an interpreter crash in debug mode.
class XBase(type):
def __new__(cls, name, bases, attrs, **kwargs):
attrs.pop('__module__')
return super().__new__(cls, name, bases, attrs, **kwargs)

class X(metaclass=XBase): ...

# The actual error here doesn't matter. As long as the interpreter
# doesn't crash.
with suppress(Exception):
type('A', (X,), {})


def load_tests(loader, tests, pattern):
from doctest import DocTestSuite
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed a crash caused by bad metaclass bases being passed into :func:`type`.
This only affected debug builds of Python -- release builds are unaffected.
1 change: 0 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3310,7 +3310,6 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
return NULL;
}
if (res == 1) {
assert(type != NULL);
return type;
}
assert(ctx.base != NULL);
Expand Down