Skip to content

Commit 70ca98b

Browse files
committed
Use F_GETFD on macOS, too
1 parent 0d3a911 commit 70ca98b

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
``is_valid_fd`` now uses faster ``fcntl(fd, F_GETFD)`` on Linux and Windows.
1+
``is_valid_fd`` now uses faster ``fcntl(fd, F_GETFD)`` on Linux, macOS, and Windows.

Python/pylifecycle.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,19 +2140,19 @@ is_valid_fd(int fd)
21402140
EBADF. FreeBSD has similar issue (bpo-32849).
21412141
21422142
Only use dup() on platforms where dup() is enough to detect invalid FD in
2143-
corner cases: on Linux and Windows (bpo-32849). */
2144-
#if defined(__linux__) || defined(MS_WINDOWS)
2143+
corner cases: on Linux and Windows (bpo-32849).
2144+
*/
21452145
if (fd < 0) {
21462146
return 0;
21472147
}
2148-
#ifdef F_GETFD
2149-
_Py_BEGIN_SUPPRESS_IPH
2150-
int res = fcntl(fd, F_GETFD);
2148+
#if defined(F_GETFD) && (defined(__linux__) || defined(__APPLE__) || defined(MS_WINDOWS))
2149+
int res;
2150+
_Py_BEGIN_SUPPRESS_IPH
2151+
res = fcntl(fd, F_GETFD);
21512152
_Py_END_SUPPRESS_IPH
21522153
return res >= 0;
2153-
#else
2154+
#elif defined(__linux__) || defined(MS_WINDOWS)
21542155
int fd2;
2155-
21562156
_Py_BEGIN_SUPPRESS_IPH
21572157
fd2 = dup(fd);
21582158
if (fd2 >= 0) {
@@ -2161,7 +2161,6 @@ is_valid_fd(int fd)
21612161
_Py_END_SUPPRESS_IPH
21622162

21632163
return (fd2 >= 0);
2164-
#endif /* F_GETFD */
21652164
#else
21662165
struct stat st;
21672166
return (fstat(fd, &st) == 0);

0 commit comments

Comments
 (0)