Commit 35734762 authored by Victor Stinner's avatar Victor Stinner

(Merge 3.2) Handle correctly _Py_fopen() error: don't replace the exception

parents bd0850b8 bd206e27
...@@ -742,7 +742,8 @@ read_directory(PyObject *archive) ...@@ -742,7 +742,8 @@ read_directory(PyObject *archive)
fp = _Py_fopen(archive, "rb"); fp = _Py_fopen(archive, "rb");
if (fp == NULL) { if (fp == NULL) {
PyErr_Format(ZipImportError, "can't open Zip file: %R", archive); if (!PyErr_Occurred())
PyErr_Format(ZipImportError, "can't open Zip file: %R", archive);
return NULL; return NULL;
} }
fseek(fp, -22, SEEK_END); fseek(fp, -22, SEEK_END);
...@@ -913,8 +914,9 @@ get_data(PyObject *archive, PyObject *toc_entry) ...@@ -913,8 +914,9 @@ get_data(PyObject *archive, PyObject *toc_entry)
fp = _Py_fopen(archive, "rb"); fp = _Py_fopen(archive, "rb");
if (!fp) { if (!fp) {
PyErr_Format(PyExc_IOError, if (!PyErr_Occurred())
"zipimport: can not open file %U", archive); PyErr_Format(PyExc_IOError,
"zipimport: can not open file %U", archive);
return NULL; return NULL;
} }
......
...@@ -3760,26 +3760,38 @@ get_file(PyObject *pathname, PyObject *fob, char *mode) ...@@ -3760,26 +3760,38 @@ get_file(PyObject *pathname, PyObject *fob, char *mode)
mode = "r" PY_STDIOTEXTMODE; mode = "r" PY_STDIOTEXTMODE;
if (fob == NULL) { if (fob == NULL) {
fp = _Py_fopen(pathname, mode); fp = _Py_fopen(pathname, mode);
if (!fp) {
if (!PyErr_Occurred())
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
return fp;
} }
else { else {
int fd = PyObject_AsFileDescriptor(fob); int fd = PyObject_AsFileDescriptor(fob);
if (fd == -1) if (fd == -1)
return NULL; return NULL;
if (!_PyVerify_fd(fd)) if (!_PyVerify_fd(fd)) {
goto error; PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
/* the FILE struct gets a new fd, so that it can be closed /* the FILE struct gets a new fd, so that it can be closed
* independently of the file descriptor given * independently of the file descriptor given
*/ */
fd = dup(fd); fd = dup(fd);
if (fd == -1) if (fd == -1) {
goto error; PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
fp = fdopen(fd, mode); fp = fdopen(fd, mode);
} if (!fp) {
if (fp) PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
return fp; return fp;
error: }
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
} }
static PyObject * static PyObject *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment