]> granicus.if.org Git - python/commitdiff
Issue #23524: Change back to using Windows errors for _Py_fstat instead of the errno...
authorSteve Dower <steve.dower@microsoft.com>
Sun, 8 Mar 2015 02:14:07 +0000 (18:14 -0800)
committerSteve Dower <steve.dower@microsoft.com>
Sun, 8 Mar 2015 02:14:07 +0000 (18:14 -0800)
Modules/_io/fileio.c
Modules/signalmodule.c
Python/fileutils.c

index ab9eb8c061af78f4591d623ebd13fa50ab30260e..0f226eafbf1eebe322536268f59aa526251c30cd 100644 (file)
@@ -182,7 +182,13 @@ check_fd(int fd)
 {
 #if defined(HAVE_FSTAT) || defined(MS_WINDOWS)
     struct _Py_stat_struct buf;
-    if (_Py_fstat(fd, &buf) < 0 && errno == EBADF) {
+    if (_Py_fstat(fd, &buf) < 0 &&
+#ifdef MS_WINDOWS
+        GetLastError() == ERROR_INVALID_HANDLE
+#else
+        errno == EBADF
+#endif
+        ) {
         PyObject *exc;
         char *msg = strerror(EBADF);
         exc = PyObject_CallFunction(PyExc_OSError, "(is)",
index 598bc8a34290819004ab341ac4b421dd7fd031a1..3ad8ebb8cd12fc7cbbd444bc68e0256f4d4874ef 100644 (file)
@@ -560,7 +560,7 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args)
             }
 
             if (_Py_fstat(fd, &st) != 0) {
-                PyErr_SetFromErrno(PyExc_OSError);
+                PyErr_SetExcFromWindowsErr(PyExc_OSError, GetLastError());
                 return NULL;
             }
 
index c0dbc86a93208a72a2bdf24cd8dae1309a8d0e57..6502823535152d94353daaf8f215b45aed73550b 100644 (file)
@@ -637,10 +637,14 @@ _Py_fstat(int fd, struct _Py_stat_struct *result)
     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) {
-        errno = EBADF;
+        /* This is really a C library error (invalid file handle).
+           We set the Win32 error to the closes one matching. */
+        SetLastError(ERROR_INVALID_HANDLE);
         return -1;
     }
     memset(result, 0, sizeof(*result));
@@ -649,7 +653,6 @@ _Py_fstat(int fd, struct _Py_stat_struct *result)
     if (type == FILE_TYPE_UNKNOWN) {
         DWORD error = GetLastError();
         if (error != 0) {
-            errno = EINVAL;
             return -1;
         }
         /* else: valid but unknown file */
@@ -664,7 +667,6 @@ _Py_fstat(int fd, struct _Py_stat_struct *result)
     }
 
     if (!GetFileInformationByHandle(h, &info)) {
-        errno = EINVAL;
         return -1;
     }