Close issue #6210: Implement PEP 409

This commit is contained in:
Nick Coghlan 2012-02-26 17:49:52 +10:00
parent cda6b6d60d
commit ab7bf2143e
15 changed files with 263 additions and 42 deletions

View file

@ -266,7 +266,24 @@ BaseException_get_cause(PyObject *self) {
PyObject *res = PyException_GetCause(self);
if (res)
return res; /* new reference already returned above */
Py_RETURN_NONE;
Py_INCREF(Py_Ellipsis);
return Py_Ellipsis;
}
int
_PyException_SetCauseChecked(PyObject *self, PyObject *arg) {
if (arg == Py_Ellipsis) {
arg = NULL;
} else if (arg != Py_None && !PyExceptionInstance_Check(arg)) {
PyErr_SetString(PyExc_TypeError, "exception cause must be None, "
"Ellipsis or derive from BaseException");
return -1;
} else {
/* PyException_SetCause steals a reference */
Py_INCREF(arg);
}
PyException_SetCause(self, arg);
return 0;
}
static int
@ -274,18 +291,8 @@ BaseException_set_cause(PyObject *self, PyObject *arg) {
if (arg == NULL) {
PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
return -1;
} else if (arg == Py_None) {
arg = NULL;
} else if (!PyExceptionInstance_Check(arg)) {
PyErr_SetString(PyExc_TypeError, "exception cause must be None "
"or derive from BaseException");
return -1;
} else {
/* PyException_SetCause steals this reference */
Py_INCREF(arg);
}
PyException_SetCause(self, arg);
return 0;
return _PyException_SetCauseChecked(self, arg);
}