diff -r d8e49a2795e7 Modules/_io/fileio.c --- a/Modules/_io/fileio.c Sat Mar 07 18:14:07 2015 -0800 +++ b/Modules/_io/fileio.c Sun Mar 08 03:26:51 2015 +0100 @@ -182,13 +182,7 @@ check_fd(int fd) { #if defined(HAVE_FSTAT) || defined(MS_WINDOWS) struct _Py_stat_struct buf; - if (_Py_fstat(fd, &buf) < 0 && -#ifdef MS_WINDOWS - GetLastError() == ERROR_INVALID_HANDLE -#else - errno == EBADF -#endif - ) { + if (_Py_fstat(fd, &buf) < 0 && errno == EBADF) { PyObject *exc; char *msg = strerror(EBADF); exc = PyObject_CallFunction(PyExc_OSError, "(is)", diff -r d8e49a2795e7 Modules/signalmodule.c --- a/Modules/signalmodule.c Sat Mar 07 18:14:07 2015 -0800 +++ b/Modules/signalmodule.c Sun Mar 08 03:26:51 2015 +0100 @@ -560,7 +560,7 @@ signal_set_wakeup_fd(PyObject *self, PyO } if (_Py_fstat(fd, &st) != 0) { - PyErr_SetExcFromWindowsErr(PyExc_OSError, GetLastError()); + PyErr_SetFromErrno(PyExc_OSError); return NULL; } diff -r d8e49a2795e7 Python/fileutils.c --- a/Python/fileutils.c Sat Mar 07 18:14:07 2015 -0800 +++ b/Python/fileutils.c Sun Mar 08 03:26:51 2015 +0100 @@ -637,14 +637,11 @@ int else h = (HANDLE)_get_osfhandle(fd); - /* Protocol violation: we explicitly clear errno, instead of - setting it to a POSIX error. Callers should use GetLastError. */ errno = 0; if (h == INVALID_HANDLE_VALUE) { - /* This is really a C library error (invalid file handle). - We set the Win32 error to the closes one matching. */ SetLastError(ERROR_INVALID_HANDLE); + errno = EBADF; return -1; } memset(result, 0, sizeof(*result)); @@ -653,6 +650,7 @@ int if (type == FILE_TYPE_UNKNOWN) { DWORD error = GetLastError(); if (error != 0) { + errno = winerror_to_errno(error); return -1; } /* else: valid but unknown file */ @@ -667,6 +665,7 @@ int } if (!GetFileInformationByHandle(h, &info)) { + errno = winerror_to_errno(error); return -1; }