comparison Python/errors.c @ 47722:7ba7901dad7f

Implicit exception chaining via __context__ (PEP 3134). Patch 3108 by Antooine Pitrou.
author Guido van Rossum <guido@python.org>
date Sat, 14 Jun 2008 20:20:24 +0000
parents 47d9bbe10897
children 2ee09afee126
comparison
equal deleted inserted replaced
47719:4b1e01505d4d 47722:7ba7901dad7f
50 } 50 }
51 51
52 void 52 void
53 PyErr_SetObject(PyObject *exception, PyObject *value) 53 PyErr_SetObject(PyObject *exception, PyObject *value)
54 { 54 {
55 PyThreadState *tstate = PyThreadState_GET();
56 PyObject *tb = NULL;
57
55 if (exception != NULL && 58 if (exception != NULL &&
56 !PyExceptionClass_Check(exception)) { 59 !PyExceptionClass_Check(exception)) {
57 PyErr_Format(PyExc_SystemError, 60 PyErr_Format(PyExc_SystemError,
58 "exception %R not a BaseException subclass", 61 "exception %R not a BaseException subclass",
59 exception); 62 exception);
60 return; 63 return;
61 } 64 }
65 Py_XINCREF(value);
66 if (tstate->exc_value != NULL && tstate->exc_value != Py_None) {
67 /* Implicit exception chaining */
68 if (value == NULL || !PyExceptionInstance_Check(value)) {
69 /* We must normalize the value right now */
70 PyObject *args, *fixed_value;
71 if (value == NULL || value == Py_None)
72 args = PyTuple_New(0);
73 else if (PyTuple_Check(value)) {
74 Py_INCREF(value);
75 args = value;
76 }
77 else
78 args = PyTuple_Pack(1, value);
79 fixed_value = args ?
80 PyEval_CallObject(exception, args) : NULL;
81 Py_XDECREF(args);
82 Py_XDECREF(value);
83 if (fixed_value == NULL)
84 return;
85 value = fixed_value;
86 }
87 Py_INCREF(tstate->exc_value);
88 PyException_SetContext(value, tstate->exc_value);
89 }
90 if (value != NULL && PyExceptionInstance_Check(value))
91 tb = PyException_GetTraceback(value);
62 Py_XINCREF(exception); 92 Py_XINCREF(exception);
63 Py_XINCREF(value); 93 PyErr_Restore(exception, value, tb);
64 PyErr_Restore(exception, value, (PyObject *)NULL);
65 } 94 }
66 95
67 void 96 void
68 PyErr_SetNone(PyObject *exception) 97 PyErr_SetNone(PyObject *exception)
69 { 98 {