diff Python/ceval.c @ 75292:b299c4f31ff2

Close issue #6210: Implement PEP 409
author Nick Coghlan <ncoghlan@gmail.com>
date Sun, 26 Feb 2012 17:49:52 +1000
parents 7d96679c5d46
children 72556ff86828
line wrap: on
line diff
--- a/Python/ceval.c	Sun Feb 26 09:39:55 2012 +0200
+++ b/Python/ceval.c	Sun Feb 26 17:49:52 2012 +1000
@@ -3567,22 +3567,23 @@
 
     if (cause) {
         PyObject *fixed_cause;
+        int result;
         if (PyExceptionClass_Check(cause)) {
             fixed_cause = PyObject_CallObject(cause, NULL);
             if (fixed_cause == NULL)
                 goto raise_error;
-            Py_DECREF(cause);
-        }
-        else if (PyExceptionInstance_Check(cause)) {
+            Py_CLEAR(cause);
+        } else {
+            /* Let "exc.__cause__ = cause" handle all further checks */
             fixed_cause = cause;
+            cause = NULL; /* Steal the reference */
         }
-        else {
-            PyErr_SetString(PyExc_TypeError,
-                            "exception causes must derive from "
-                            "BaseException");
+        /* We retain ownership of the reference to fixed_cause */
+        result = _PyException_SetCauseChecked(value, fixed_cause);
+        Py_DECREF(fixed_cause);
+        if (result < 0) {
             goto raise_error;
         }
-        PyException_SetCause(value, fixed_cause);
     }
 
     PyErr_SetObject(type, value);