]> granicus.if.org Git - python/commitdiff
Avoid fcntl() if possible in set_inheritable()
authorVictor Stinner <victor.stinner@gmail.com>
Sun, 17 Apr 2016 14:51:52 +0000 (16:51 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Sun, 17 Apr 2016 14:51:52 +0000 (16:51 +0200)
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).

Python/fileutils.c

index a710c99129442ae7ebaacb16631164d222e0b61b..4a0ef48f120d702df1f706fb25b43c184e74be74 100644 (file)
@@ -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)