From: Victor Stinner Date: Thu, 4 May 2017 11:21:10 +0000 (+0200) Subject: bpo-30225: Fix is_valid_fd() on macOS Tiger (#1443) (#1449) X-Git-Tag: v3.6.2rc1~176 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=31906b42fd71c6baaf80429005c720b40dfe8fae;p=thirdparty%2FPython%2Fcpython.git 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 1c4670ea0cc3d208121af11b9b973e6bb268e570) --- diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index af96d6d5fcb6..640271fd20b7 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1044,6 +1044,14 @@ initsite(void) static int is_valid_fd(int fd) { +#ifdef __APPLE__ + /* bpo-30225: On macOS Tiger, when stdout is redirected to a pipe + and the other side of the pipe is closed, dup(1) succeed, whereas + fstat(1, &st) fails with EBADF. Prefer fstat() over dup() to detect + such error. */ + struct stat st; + return (fstat(fd, &st) == 0); +#else int fd2; if (fd < 0) return 0; @@ -1056,6 +1064,7 @@ is_valid_fd(int fd) close(fd2); _Py_END_SUPPRESS_IPH return fd2 >= 0; +#endif } /* returns Py_None if the fd is not valid */