Close issue #6210: Implement PEP 409
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index e9522e8..bc43799 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -266,7 +266,24 @@
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 @@
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);
}