diff -r 5b1a03a858e0 Python/errors.c --- a/Python/errors.c Wed Mar 25 01:20:30 2015 +0100 +++ b/Python/errors.c Wed Mar 25 01:44:51 2015 +0100 @@ -52,8 +52,8 @@ PyErr_Restore(PyObject *type, PyObject * Py_XDECREF(oldtraceback); } -void -PyErr_SetObject(PyObject *exception, PyObject *value) +static void +_PyErr_SetObject(PyObject *exception, PyObject *value) { PyThreadState *tstate = PyThreadState_GET(); PyObject *exc_value; @@ -75,10 +75,6 @@ PyErr_SetObject(PyObject *exception, PyO /* We must normalize the value right now */ PyObject *args, *fixed_value; - /* Issue #23571: PyEval_CallObject() must not be called with an - exception set */ - PyErr_Clear(); - if (value == NULL || value == Py_None) args = PyTuple_New(0); else if (PyTuple_Check(value)) { @@ -120,6 +116,17 @@ PyErr_SetObject(PyObject *exception, PyO PyErr_Restore(exception, value, tb); } +void +PyErr_SetObject(PyObject *exception, PyObject *value) +{ + PyObject *etype, *val, *tb; + PyErr_Fetch(&etype, &val, &tb); + _PyErr_SetObject(exception, value); + if (etype != NULL) + _PyErr_ChainExceptions(etype, val, tb); +} + + /* Set a key error with the specified argument, wrapping it in a * tuple automatically so that tuple keys are not unpacked as the * exception arguments. */ @@ -776,15 +783,17 @@ PyErr_BadInternalCall(void) PyObject * PyErr_FormatV(PyObject *exception, const char *format, va_list vargs) { + PyObject *etype, *val, *tb; PyObject* string; - /* Issue #23571: PyUnicode_FromFormatV() must not be called with an - exception set, it calls arbitrary Python code like PyObject_Repr() */ - PyErr_Clear(); + PyErr_Fetch(&etype, &val, &tb); string = PyUnicode_FromFormatV(format, vargs); - PyErr_SetObject(exception, string); + _PyErr_SetObject(exception, string); + if (etype != NULL) + _PyErr_ChainExceptions(etype, val, tb); + Py_XDECREF(string); return NULL; }