diff -r 570ada02d0f0 Python/fileutils.c --- a/Python/fileutils.c Fri Apr 15 02:27:11 2016 +0000 +++ b/Python/fileutils.c Fri Apr 15 15:19:53 2016 +0200 @@ -798,7 +798,7 @@ set_inheritable(int fd, int inheritable, int request; int err; #endif - int flags; + int flags, new_flags; int res; #endif @@ -884,10 +884,18 @@ set_inheritable(int fd, int inheritable, 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)