type_call() now detect bugs in type new and init

* Call _Py_CheckFunctionResult() to check for bugs in type
  constructors (tp_new)
* Add assertions to ensure an exception was raised if tp_init failed
  or that no exception was raised if tp_init succeed

Refactor also the function to have less indentation.
This commit is contained in:
Victor Stinner 2015-09-03 12:16:49 +02:00
parent 31eb5fbe89
commit 99bb14bf0c

View file

@ -906,7 +906,10 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
#endif #endif
obj = type->tp_new(type, args, kwds); obj = type->tp_new(type, args, kwds);
if (obj != NULL) { obj = _Py_CheckFunctionResult((PyObject*)type, obj, NULL);
if (obj == NULL)
return NULL;
/* Ugly exception: when the call was type(something), /* Ugly exception: when the call was type(something),
don't call tp_init on the result. */ don't call tp_init on the result. */
if (type == &PyType_Type && if (type == &PyType_Type &&
@ -914,17 +917,22 @@ type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
(kwds == NULL || (kwds == NULL ||
(PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
return obj; return obj;
/* If the returned object is not an instance of type, /* If the returned object is not an instance of type,
it won't be initialized. */ it won't be initialized. */
if (!PyType_IsSubtype(Py_TYPE(obj), type)) if (!PyType_IsSubtype(Py_TYPE(obj), type))
return obj; return obj;
type = Py_TYPE(obj); type = Py_TYPE(obj);
if (type->tp_init != NULL) { if (type->tp_init != NULL) {
int res = type->tp_init(obj, args, kwds); int res = type->tp_init(obj, args, kwds);
if (res < 0) { if (res < 0) {
assert(PyErr_Occurred());
Py_DECREF(obj); Py_DECREF(obj);
obj = NULL; obj = NULL;
} }
else {
assert(!PyErr_Occurred());
} }
} }
return obj; return obj;