mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
bpo-45915: use fcntl(fd, F_GETFD) in is_valid_fd() (GH-29821)
This commit is contained in:
parent
aaf42222cf
commit
f87ea03502
2 changed files with 16 additions and 3 deletions
|
@ -0,0 +1 @@
|
||||||
|
``is_valid_fd`` now uses faster ``fcntl(fd, F_GETFD)`` on Linux, macOS, and Windows.
|
|
@ -30,6 +30,10 @@
|
||||||
# include <langinfo.h> // nl_langinfo(CODESET)
|
# include <langinfo.h> // nl_langinfo(CODESET)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_FCNTL_H
|
||||||
|
# include <fcntl.h> // F_GETFD
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
# undef BYTE
|
# undef BYTE
|
||||||
# include "windows.h"
|
# include "windows.h"
|
||||||
|
@ -2129,18 +2133,26 @@ is_valid_fd(int fd)
|
||||||
startup. Problem: dup() doesn't check if the file descriptor is valid on
|
startup. Problem: dup() doesn't check if the file descriptor is valid on
|
||||||
some platforms.
|
some platforms.
|
||||||
|
|
||||||
|
fcntl(fd, F_GETFD) is even faster, because it only checks the process table.
|
||||||
|
|
||||||
bpo-30225: On macOS Tiger, when stdout is redirected to a pipe and the other
|
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
|
side of the pipe is closed, dup(1) succeed, whereas fstat(1, &st) fails with
|
||||||
EBADF. FreeBSD has similar issue (bpo-32849).
|
EBADF. FreeBSD has similar issue (bpo-32849).
|
||||||
|
|
||||||
Only use dup() on platforms where dup() is enough to detect invalid FD in
|
Only use dup() on platforms where dup() is enough to detect invalid FD in
|
||||||
corner cases: on Linux and Windows (bpo-32849). */
|
corner cases: on Linux and Windows (bpo-32849).
|
||||||
#if defined(__linux__) || defined(MS_WINDOWS)
|
*/
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
#if defined(F_GETFD) && (defined(__linux__) || defined(__APPLE__) || defined(MS_WINDOWS))
|
||||||
|
int res;
|
||||||
|
_Py_BEGIN_SUPPRESS_IPH
|
||||||
|
res = fcntl(fd, F_GETFD);
|
||||||
|
_Py_END_SUPPRESS_IPH
|
||||||
|
return res >= 0;
|
||||||
|
#elif defined(__linux__) || defined(MS_WINDOWS)
|
||||||
int fd2;
|
int fd2;
|
||||||
|
|
||||||
_Py_BEGIN_SUPPRESS_IPH
|
_Py_BEGIN_SUPPRESS_IPH
|
||||||
fd2 = dup(fd);
|
fd2 = dup(fd);
|
||||||
if (fd2 >= 0) {
|
if (fd2 >= 0) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue