[Python-checkins] bpo-45919: Use WinAPI GetFileType() in is_valid_fd() (GH-30082)

corona10 webhook-mailer at python.org
Mon Dec 13 07:58:04 EST 2021


https://github.com/python/cpython/commit/191c431de7d9b23484dd16f67e62c6e85a1fac7f
commit: 191c431de7d9b23484dd16f67e62c6e85a1fac7f
branch: main
author: Dong-hee Na <donghee.na at python.org>
committer: corona10 <donghee.na92 at gmail.com>
date: 2021-12-13T21:57:59+09:00
summary:

bpo-45919: Use WinAPI GetFileType() in is_valid_fd() (GH-30082)

files:
M Python/pylifecycle.c

diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index b6d73a9ce2216..a9eb38775c42a 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -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);



More information about the Python-checkins mailing list