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
1 changed files with 27 additions and 19 deletions

View File

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