Skip to content

Commit 1c4670e

Browse files
authored
bpo-30225: Fix is_valid_fd() on macOS Tiger (#1443)
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.
1 parent 5f161fd commit 1c4670e

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
@@ -1045,6 +1045,14 @@ initsite(void)
10451045
static int
10461046
is_valid_fd(int fd)
10471047
{
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
10481056
int fd2;
10491057
if (fd < 0)
10501058
return 0;
@@ -1057,6 +1065,7 @@ is_valid_fd(int fd)
10571065
close(fd2);
10581066
_Py_END_SUPPRESS_IPH
10591067
return fd2 >= 0;
1068+
#endif
10601069
}
10611070

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

0 commit comments

Comments
 (0)