diff options
author | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2008-08-29 07:13:32 (GMT) |
---|---|---|
committer | Amaury Forgeot d'Arc <amauryfa@gmail.com> | 2008-08-29 07:13:32 (GMT) |
commit | db26f7c13748db526e52501d3ba2856a6e30c7a5 (patch) | |
tree | 543064580b5daedb94549cb8bcf5396172492f6b /Python | |
parent | 4f3c5616ccb880f8b5602c2da5c8951b55e4e9dd (diff) | |
download | cpython-db26f7c13748db526e52501d3ba2856a6e30c7a5.zip cpython-db26f7c13748db526e52501d3ba2856a6e30c7a5.tar.gz cpython-db26f7c13748db526e52501d3ba2856a6e30c7a5.tar.bz2 |
Issue 3611: in some cases (a __del__ re-raising an exception, when called from inside
an 'except' clause), the exception __context__ would be reset to None.
This crases the interpreter if this precisely happens inside PyErr_SetObject.
- now the __context__ is properly preserved
- in any case, PyErr_SetObject now saves the current exc_value in a local variable, to
avoid such crashes in the future.
Reviewer: Antoine Pitrou.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/ceval.c | 5 | ||||
-rw-r--r-- | Python/errors.c | 14 |
2 files changed, 9 insertions, 10 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 3af0cef..42df3cb 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2453,11 +2453,6 @@ fast_block_end: if (b->b_type == EXCEPT_HANDLER) { UNWIND_EXCEPT_HANDLER(b); - if (why == WHY_EXCEPTION && !throwflag) { - Py_CLEAR(tstate->exc_type); - Py_CLEAR(tstate->exc_value); - Py_CLEAR(tstate->exc_traceback); - } continue; } UNWIND_BLOCK(b); diff --git a/Python/errors.c b/Python/errors.c index d7aac6d..6335388 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -53,6 +53,7 @@ void PyErr_SetObject(PyObject *exception, PyObject *value) { PyThreadState *tstate = PyThreadState_GET(); + PyObject *exc_value; PyObject *tb = NULL; if (exception != NULL && @@ -63,8 +64,10 @@ PyErr_SetObject(PyObject *exception, PyObject *value) return; } Py_XINCREF(value); - if (tstate->exc_value != NULL && tstate->exc_value != Py_None) { + exc_value = tstate->exc_value; + if (exc_value != NULL && exc_value != Py_None) { /* Implicit exception chaining */ + Py_INCREF(exc_value); if (value == NULL || !PyExceptionInstance_Check(value)) { /* We must normalize the value right now */ PyObject *args, *fixed_value; @@ -88,8 +91,8 @@ PyErr_SetObject(PyObject *exception, PyObject *value) This is O(chain length) but context chains are usually very short. Sensitive readers may try to inline the call to PyException_GetContext. */ - if (tstate->exc_value != value) { - PyObject *o = tstate->exc_value, *context; + if (exc_value != value) { + PyObject *o = exc_value, *context; while ((context = PyException_GetContext(o))) { Py_DECREF(context); if (context == value) { @@ -98,8 +101,9 @@ PyErr_SetObject(PyObject *exception, PyObject *value) } o = context; } - Py_INCREF(tstate->exc_value); - PyException_SetContext(value, tstate->exc_value); + PyException_SetContext(value, exc_value); + } else { + Py_DECREF(exc_value); } } if (value != NULL && PyExceptionInstance_Check(value)) |