diff --git a/Python/fileutils.c b/Python/fileutils.c --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -266,8 +266,8 @@ _Py_stat(PyObject *path, struct stat *st FILE * _Py_wfopen(const wchar_t *path, const wchar_t *mode) { + FILE *f; #ifndef MS_WINDOWS - FILE *f; char *cpath; char cmode[10]; size_t r; @@ -281,10 +281,14 @@ _Py_wfopen(const wchar_t *path, const wc return NULL; f = fopen(cpath, cmode); PyMem_Free(cpath); +#else + f = _wfopen(path, mode); +#endif + if (f == NULL && errno != ENOENT) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } return f; -#else - return _wfopen(path, mode); -#endif } /* Call _wfopen() on Windows, or encode the path to the filesystem encoding and @@ -296,6 +300,7 @@ _Py_wfopen(const wchar_t *path, const wc FILE* _Py_fopen(PyObject *path, const char *mode) { + FILE* f; #ifdef MS_WINDOWS wchar_t wmode[10]; int usize; @@ -304,16 +309,19 @@ _Py_fopen(PyObject *path, const char *mo if (usize == 0) return NULL; - return _wfopen(PyUnicode_AS_UNICODE(path), wmode); + f = _wfopen(PyUnicode_AS_UNICODE(path), wmode); #else - FILE *f; PyObject *bytes = PyUnicode_EncodeFSDefault(path); if (bytes == NULL) return NULL; f = fopen(PyBytes_AS_STRING(bytes), mode); Py_DECREF(bytes); +#endif + if (f == NULL && errno != ENOENT) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } return f; -#endif } #ifdef HAVE_READLINK