Issue #23781: Add private helper function _PyErr_ReplaceException() that
corresponds _PyErr_ChainExceptions() in Python 3 to help porting patches
from Python 3.
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index 29db164..04c4445 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -529,14 +529,8 @@
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 --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index f146958..b4632ed 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -483,15 +483,8 @@
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 --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 48e0c66..65c8d8d 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -2480,15 +2480,8 @@
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;
}