self.assertIs(ns, expected_ns)
self.assertEqual(len(kwds), 0)
+ def test_bad___prepare__(self):
+ # __prepare__() must return a mapping.
+ class BadMeta(type):
+ @classmethod
+ def __prepare__(*args):
+ return None
+ with self.assertRaisesRegex(TypeError,
+ r'^BadMeta\.__prepare__\(\) must '
+ r'return a mapping, not NoneType$'):
+ class Foo(metaclass=BadMeta):
+ pass
+ # Also test the case in which the metaclass is not a type.
+ class BadMeta:
+ @classmethod
+ def __prepare__(*args):
+ return None
+ with self.assertRaisesRegex(TypeError,
+ r'^<metaclass>\.__prepare__\(\) must '
+ r'return a mapping, not NoneType$'):
+ class Bar(metaclass=BadMeta()):
+ pass
+
def test_metaclass_derivation(self):
# issue1294232: correct metaclass calculation
new_calls = [] # to check the order of __new__ calls
Py_DECREF(bases);
return NULL;
}
+ if (!PyMapping_Check(ns)) {
+ PyErr_Format(PyExc_TypeError,
+ "%.200s.__prepare__() must return a mapping, not %.200s",
+ isclass ? ((PyTypeObject *)meta)->tp_name : "<metaclass>",
+ Py_TYPE(ns)->tp_name);
+ goto error;
+ }
cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), ns,
NULL, 0, NULL, 0, NULL, 0, NULL,
PyFunction_GET_CLOSURE(func));