bpo-30225: Fix is_valid_fd() on macOS Tiger (#1443) · python/cpython@1c4670e (original) (raw)

Original file line number Diff line number Diff line change
@@ -1045,6 +1045,14 @@ initsite(void)
1045 1045 static int
1046 1046 is_valid_fd(int fd)
1047 1047 {
1048 +#ifdef __APPLE__
1049 +/* bpo-30225: On macOS Tiger, when stdout is redirected to a pipe
1050 + and the other side of the pipe is closed, dup(1) succeed, whereas
1051 + fstat(1, &st) fails with EBADF. Prefer fstat() over dup() to detect
1052 + such error. */
1053 +struct stat st;
1054 +return (fstat(fd, &st) == 0);
1055 +#else
1048 1056 int fd2;
1049 1057 if (fd < 0)
1050 1058 return 0;
@@ -1057,6 +1065,7 @@ is_valid_fd(int fd)
1057 1065 close(fd2);
1058 1066 _Py_END_SUPPRESS_IPH
1059 1067 return fd2 >= 0;
1068 +#endif
1060 1069 }
1061 1070
1062 1071 /* returns Py_None if the fd is not valid */