diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 7c8ba37c4fe..198df4bb898 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -370,6 +370,9 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode, } errno = 0; +#ifdef MS_WINDOWS + _doserrno = 0; +#endif if (opener == Py_None) { do { Py_BEGIN_ALLOW_THREADS @@ -418,7 +421,12 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode, fd_is_own = 1; if (self->fd < 0) { +#ifdef MS_WINDOWS + PyErr_SetExcFromWindowsErrWithFilenameObject( + PyExc_OSError, _doserrno, nameobj); +#else PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj); +#endif goto error; } diff --git a/Python/errors.c b/Python/errors.c index 31de0dbce1e..5d1725679c4 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -700,11 +700,7 @@ PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, P table, we use it, otherwise we assume it really _is_ a Win32 error code */ - if (i == ENOENT && GetLastError() == ERROR_PATH_NOT_FOUND) { - message = PyUnicode_FromString("No such path or path exceeds " - "maximum allowed length"); - } - else if (i > 0 && i < _sys_nerr) { + if (i > 0 && i < _sys_nerr) { message = PyUnicode_FromString(_sys_errlist[i]); } else {