diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2016-04-17 14:51:52 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2016-04-17 14:51:52 (GMT) |
commit | a858bbde03638e3145894029dbc40d3d777be24f (patch) | |
tree | 31a6a4da0c0e64ee88a8ce0f26a1ee8ae2fb06cd /Python/fileutils.c | |
parent | b6a9c9761ca988e1ab69defd45433fac0b2ff89c (diff) | |
download | cpython-a858bbde03638e3145894029dbc40d3d777be24f.zip cpython-a858bbde03638e3145894029dbc40d3d777be24f.tar.gz cpython-a858bbde03638e3145894029dbc40d3d777be24f.tar.bz2 |
Avoid fcntl() if possible in set_inheritable()
Issue #26770: set_inheritable() avoids calling fcntl() twice if the FD_CLOEXEC
is already set/cleared. This change only impacts platforms using the fcntl()
implementation of set_inheritable() (not Linux nor Windows).
Diffstat (limited to 'Python/fileutils.c')
-rw-r--r-- | Python/fileutils.c | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c index a710c99..4a0ef48 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -798,7 +798,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) int request; int err; #endif - int flags; + int flags, new_flags; int res; #endif @@ -884,10 +884,18 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) return -1; } - if (inheritable) - flags &= ~FD_CLOEXEC; - else - flags |= FD_CLOEXEC; + if (inheritable) { + new_flags = flags & ~FD_CLOEXEC; + } + else { + new_flags = flags | FD_CLOEXEC; + } + + if (new_flags == flags) { + /* FD_CLOEXEC flag already set/cleared: nothing to do */ + return 0; + } + res = fcntl(fd, F_SETFD, flags); if (res < 0) { if (raise) |