diff -r 217f95d07927 Include/pyerrors.h --- a/Include/pyerrors.h Wed Mar 25 19:15:56 2015 +0200 +++ b/Include/pyerrors.h Thu Mar 26 11:45:00 2015 +0200 @@ -91,6 +91,7 @@ PyAPI_FUNC(void) PyErr_Restore(PyObject PyAPI_FUNC(int) PyErr_GivenExceptionMatches(PyObject *, PyObject *); PyAPI_FUNC(int) PyErr_ExceptionMatches(PyObject *); PyAPI_FUNC(void) PyErr_NormalizeException(PyObject**, PyObject**, PyObject**); +PyAPI_FUNC(void) _PyErr_ReplaceException(PyObject *, PyObject *, PyObject *); /* */ diff -r 217f95d07927 Modules/_io/_iomodule.c --- a/Modules/_io/_iomodule.c Wed Mar 25 19:15:56 2015 +0200 +++ b/Modules/_io/_iomodule.c Thu Mar 26 11:45:00 2015 +0200 @@ -529,14 +529,8 @@ io_open(PyObject *self, PyObject *args, PyObject *exc, *val, *tb, *close_result; PyErr_Fetch(&exc, &val, &tb); close_result = PyObject_CallMethod(result, "close", NULL); - if (close_result != NULL) { - Py_DECREF(close_result); - PyErr_Restore(exc, val, tb); - } else { - Py_XDECREF(exc); - Py_XDECREF(val); - Py_XDECREF(tb); - } + _PyErr_ReplaceException(exc, val, tb); + Py_XDECREF(close_result); Py_DECREF(result); } Py_XDECREF(modeobj); diff -r 217f95d07927 Modules/_io/bufferedio.c --- a/Modules/_io/bufferedio.c Wed Mar 25 19:15:56 2015 +0200 +++ b/Modules/_io/bufferedio.c Thu Mar 26 11:45:00 2015 +0200 @@ -483,15 +483,8 @@ buffered_close(buffered *self, PyObject res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_close, NULL); if (exc != NULL) { - if (res != NULL) { - Py_CLEAR(res); - PyErr_Restore(exc, val, tb); - } - else { - Py_DECREF(exc); - Py_XDECREF(val); - Py_XDECREF(tb); - } + _PyErr_ReplaceException(exc, val, tb); + Py_CLEAR(res); } end: diff -r 217f95d07927 Modules/_io/textio.c --- a/Modules/_io/textio.c Wed Mar 25 19:15:56 2015 +0200 +++ b/Modules/_io/textio.c Thu Mar 26 11:45:00 2015 +0200 @@ -2480,15 +2480,8 @@ textiowrapper_close(textio *self, PyObje res = PyObject_CallMethod(self->buffer, "close", NULL); if (exc != NULL) { - if (res != NULL) { - Py_CLEAR(res); - PyErr_Restore(exc, val, tb); - } - else { - Py_DECREF(exc); - Py_XDECREF(val); - Py_XDECREF(tb); - } + _PyErr_ReplaceException(exc, val, tb); + Py_CLEAR(res); } return res; } diff -r 217f95d07927 Python/errors.c --- a/Python/errors.c Wed Mar 25 19:15:56 2015 +0200 +++ b/Python/errors.c Thu Mar 26 11:45:00 2015 +0200 @@ -263,6 +263,26 @@ PyErr_Clear(void) PyErr_Restore(NULL, NULL, NULL); } +/* Restore previously fetched exception if an exception is not set, + otherwise drop previously fetched exception. + Like _PyErr_ChainExceptions() in Python 3, but doesn't set the context. + */ +void +_PyErr_ReplaceException(PyObject *exc, PyObject *val, PyObject *tb) +{ + if (exc == NULL) + return; + + if (PyErr_Occurred()) { + Py_DECREF(exc); + Py_XDECREF(val); + Py_XDECREF(tb); + } + else { + PyErr_Restore(exc, val, tb); + } +} + /* Convenience functions to set a type error exception and return 0 */ int