]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45919: Use WinAPI GetFileType() in is_valid_fd() (GH-30082)
authorDong-hee Na <donghee.na@python.org>
Mon, 13 Dec 2021 12:57:59 +0000 (21:57 +0900)
committerGitHub <noreply@github.com>
Mon, 13 Dec 2021 12:57:59 +0000 (21:57 +0900)
Python/pylifecycle.c

index b6d73a9ce22160f5bc262f7b20ae40ad535c51b7..a9eb38775c42ac370ff728ffb82c0e4499392b1c 100644 (file)
@@ -2182,23 +2182,21 @@ is_valid_fd(int fd)
 #if defined(F_GETFD) && ( \
         defined(__linux__) || \
         defined(__APPLE__) || \
-        defined(MS_WINDOWS) || \
         defined(__wasm__))
-    int res;
-    _Py_BEGIN_SUPPRESS_IPH
-    res = fcntl(fd, F_GETFD);
-    _Py_END_SUPPRESS_IPH
-    return res >= 0;
-#elif defined(__linux__) || defined(MS_WINDOWS)
-    int fd2;
-    _Py_BEGIN_SUPPRESS_IPH
-    fd2 = dup(fd);
+    return fcntl(fd, F_GETFD) >= 0;
+#elif defined(__linux__)
+    int fd2 = dup(fd);
     if (fd2 >= 0) {
         close(fd2);
     }
-    _Py_END_SUPPRESS_IPH
-
     return (fd2 >= 0);
+#elif defined(MS_WINDOWS)
+    HANDLE hfile;
+    _Py_BEGIN_SUPPRESS_IPH
+    hfile = (HANDLE)_get_osfhandle(fd);
+    _Py_END_SUPPRESS_IPH
+    return (hfile != INVALID_HANDLE_VALUE
+            && GetFileType(hfile) != FILE_TYPE_UNKNOWN);
 #else
     struct stat st;
     return (fstat(fd, &st) == 0);