Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a858bbd

Browse files
committed
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).
1 parent b6a9c97 commit a858bbd

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

Python/fileutils.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
798798
int request;
799799
int err;
800800
#endif
801-
int flags;
801+
int flags, new_flags;
802802
int res;
803803
#endif
804804

@@ -884,10 +884,18 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
884884
return -1;
885885
}
886886

887-
if (inheritable)
888-
flags &= ~FD_CLOEXEC;
889-
else
890-
flags |= FD_CLOEXEC;
887+
if (inheritable) {
888+
new_flags = flags & ~FD_CLOEXEC;
889+
}
890+
else {
891+
new_flags = flags | FD_CLOEXEC;
892+
}
893+
894+
if (new_flags == flags) {
895+
/* FD_CLOEXEC flag already set/cleared: nothing to do */
896+
return 0;
897+
}
898+
891899
res = fcntl(fd, F_SETFD, flags);
892900
if (res < 0) {
893901
if (raise)

0 commit comments

Comments
 (0)