diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index a17adf7..fa9ffae 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1065,15 +1065,27 @@ error: static int is_valid_fd(int fd) { - int dummy_fd; if (fd < 0 || !_PyVerify_fd(fd)) return 0; - _Py_BEGIN_SUPPRESS_IPH - dummy_fd = dup(fd); - if (dummy_fd >= 0) - close(dummy_fd); - _Py_END_SUPPRESS_IPH - return dummy_fd >= 0; +#if defined(MS_WINDOWS) && defined(_MSC_VER) && (_MSC_VER >= 1700 && _MSC_VER < 1900) + /* dup (DuplicateHandle) doesn't say fd is a valid *file* handle. + * It could be a current thread pseudo-handle. + */ + { + struct stat buf; + if (fstat(fd, &buf) < 0 && (errno == EBADF || errno == ENOENT)) + return 0; + } +#else + { + int dummy_fd; + dummy_fd = dup(fd); + if (dummy_fd < 0) + return 0; + close(dummy_fd); + } +#endif + return 1; } /* Initialize sys.stdin, stdout, stderr and builtins.open */