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

This commit is contained in:
Victor Stinner 2011-12-18 21:05:22 +01:00
commit 3573476271
2 changed files with 26 additions and 12 deletions

View file

@ -3760,26 +3760,38 @@ get_file(PyObject *pathname, PyObject *fob, char *mode)
mode = "r" PY_STDIOTEXTMODE;
if (fob == NULL) {
fp = _Py_fopen(pathname, mode);
if (!fp) {
if (!PyErr_Occurred())
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
return fp;
}
else {
int fd = PyObject_AsFileDescriptor(fob);
if (fd == -1)
return NULL;
if (!_PyVerify_fd(fd))
goto error;
if (!_PyVerify_fd(fd)) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
/* the FILE struct gets a new fd, so that it can be closed
* independently of the file descriptor given
*/
fd = dup(fd);
if (fd == -1)
goto error;
if (fd == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
fp = fdopen(fd, mode);
}
if (fp)
if (!fp) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
return fp;
error:
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
}
static PyObject *