Skip to content

Commit 31906b4

Browse files
authored
bpo-30225: Fix is_valid_fd() on macOS Tiger (#1443) (#1449)
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 1bebd8a commit 31906b4

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
@@ -1044,6 +1044,14 @@ initsite(void)
10441044
static int
10451045
is_valid_fd(int fd)
10461046
{
1047+
#ifdef __APPLE__
1048+
/* bpo-30225: On macOS Tiger, when stdout is redirected to a pipe
1049+
and the other side of the pipe is closed, dup(1) succeed, whereas
1050+
fstat(1, &st) fails with EBADF. Prefer fstat() over dup() to detect
1051+
such error. */
1052+
struct stat st;
1053+
return (fstat(fd, &st) == 0);
1054+
#else
10471055
int fd2;
10481056
if (fd < 0)
10491057
return 0;
@@ -1056,6 +1064,7 @@ is_valid_fd(int fd)
10561064
close(fd2);
10571065
_Py_END_SUPPRESS_IPH
10581066
return fd2 >= 0;
1067+
#endif
10591068
}
10601069

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

0 commit comments

Comments
 (0)