summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-03-30 09:48:42 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2015-03-30 09:48:42 +0300
commitaa64c46ac9ff168d7730182d48665b0b51b4f69d (patch)
treefce292f7bbe0c540de322ec06e96ba0115221f9b
parentd06c201e5bf74dd1ff227ab7bea73c8c790d234b (diff)
downloadcpython-git-aa64c46ac9ff168d7730182d48665b0b51b4f69d.tar.gz
Issue #23781: Add private helper function _PyErr_ReplaceException() that
corresponds _PyErr_ChainExceptions() in Python 3 to help porting patches from Python 3.
-rw-r--r--Include/pyerrors.h1
-rw-r--r--Modules/_io/_iomodule.c10
-rw-r--r--Modules/_io/bufferedio.c11
-rw-r--r--Modules/_io/textio.c11
-rw-r--r--Python/errors.c20
5 files changed, 27 insertions, 26 deletions
diff --git a/Include/pyerrors.h b/Include/pyerrors.h
index dbe3bfa5f2..2ef205ea4b 100644
--- a/Include/pyerrors.h
+++ b/Include/pyerrors.h
@@ -91,6 +91,7 @@ PyAPI_FUNC(void) PyErr_Restore(PyObject *, PyObject *, 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 --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index 29db1642c4..04c444552d 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -529,14 +529,8 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
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 f146958b80..b4632edf44 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -483,15 +483,8 @@ buffered_close(buffered *self, PyObject *args)
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 48e0c66564..65c8d8d4e8 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -2480,15 +2480,8 @@ textiowrapper_close(textio *self, PyObject *args)
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 --git a/Python/errors.c b/Python/errors.c
index 64ba05dd6c..00dfd3ec54 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -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