Skip to content

Commit 8a1c710

Browse files
authored
bpo-30225: Fix is_valid_fd() on macOS Tiger (#1443) (#1450)
is_valid_fd() now uses fstat() instead of dup() on macOS to return 0 on a pipe when the other side of the pipe is closed. fstat() fails with EBADF in that case, whereas dup() succeed. (cherry picked from commit 1c4670e)
1 parent 943861f commit 8a1c710

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Python/pylifecycle.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,14 @@ initsite(void)
968968
static int
969969
is_valid_fd(int fd)
970970
{
971+
#ifdef __APPLE__
972+
/* bpo-30225: On macOS Tiger, when stdout is redirected to a pipe
973+
and the other side of the pipe is closed, dup(1) succeed, whereas
974+
fstat(1, &st) fails with EBADF. Prefer fstat() over dup() to detect
975+
such error. */
976+
struct stat st;
977+
return (fstat(fd, &st) == 0);
978+
#else
971979
int fd2;
972980
if (fd < 0 || !_PyVerify_fd(fd))
973981
return 0;
@@ -977,6 +985,7 @@ is_valid_fd(int fd)
977985
close(fd2);
978986
_Py_END_SUPPRESS_IPH
979987
return fd2 >= 0;
988+
#endif
980989
}
981990

982991
/* returns Py_None if the fd is not valid */

0 commit comments

Comments
 (0)