Skip to content

Commit 191c431

Browse files
authored
bpo-45919: Use WinAPI GetFileType() in is_valid_fd() (GH-30082)
1 parent e09705f commit 191c431

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

Python/pylifecycle.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,23 +2182,21 @@ is_valid_fd(int fd)
21822182
#if defined(F_GETFD) && ( \
21832183
defined(__linux__) || \
21842184
defined(__APPLE__) || \
2185-
defined(MS_WINDOWS) || \
21862185
defined(__wasm__))
2187-
int res;
2188-
_Py_BEGIN_SUPPRESS_IPH
2189-
res = fcntl(fd, F_GETFD);
2190-
_Py_END_SUPPRESS_IPH
2191-
return res >= 0;
2192-
#elif defined(__linux__) || defined(MS_WINDOWS)
2193-
int fd2;
2194-
_Py_BEGIN_SUPPRESS_IPH
2195-
fd2 = dup(fd);
2186+
return fcntl(fd, F_GETFD) >= 0;
2187+
#elif defined(__linux__)
2188+
int fd2 = dup(fd);
21962189
if (fd2 >= 0) {
21972190
close(fd2);
21982191
}
2199-
_Py_END_SUPPRESS_IPH
2200-
22012192
return (fd2 >= 0);
2193+
#elif defined(MS_WINDOWS)
2194+
HANDLE hfile;
2195+
_Py_BEGIN_SUPPRESS_IPH
2196+
hfile = (HANDLE)_get_osfhandle(fd);
2197+
_Py_END_SUPPRESS_IPH
2198+
return (hfile != INVALID_HANDLE_VALUE
2199+
&& GetFileType(hfile) != FILE_TYPE_UNKNOWN);
22022200
#else
22032201
struct stat st;
22042202
return (fstat(fd, &st) == 0);

0 commit comments

Comments
 (0)