diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-12-03 23:41:24 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-12-03 23:41:24 (GMT) |
commit | 9a954838aba9385618642342828b9dd7c21851d3 (patch) | |
tree | 368f47c94a6f2af86fd015659f13a2e5a9e4bc1e /Modules/socketmodule.c | |
parent | 99c2ab4b837288c0b29a1777988b07a77171c544 (diff) | |
download | cpython-9a954838aba9385618642342828b9dd7c21851d3.zip cpython-9a954838aba9385618642342828b9dd7c21851d3.tar.gz cpython-9a954838aba9385618642342828b9dd7c21851d3.tar.bz2 |
Close #19827: On UNIX, setblocking() and settimeout() methods of socket.socket
can now avoid a second syscall if the ioctl() function can be used, or if the
non-blocking flag of the socket is unchanged.
Diffstat (limited to 'Modules/socketmodule.c')
-rw-r--r-- | Modules/socketmodule.c | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 9e0da13..1444369 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -585,8 +585,9 @@ sendsegmented(int sock_fd, char *buf, int len, int flags) static int internal_setblocking(PySocketSockObject *s, int block) { -#ifndef MS_WINDOWS - int delay_flag; +#if !defined(MS_WINDOWS) \ + && !((defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) || defined(__VMS)) + int delay_flag, new_delay_flag; #endif #ifdef SOCK_NONBLOCK if (block) @@ -597,17 +598,18 @@ internal_setblocking(PySocketSockObject *s, int block) Py_BEGIN_ALLOW_THREADS #ifndef MS_WINDOWS -#if defined(__VMS) +#if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO)) || defined(__VMS) block = !block; ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block); -#else /* !__VMS */ +#else delay_flag = fcntl(s->sock_fd, F_GETFL, 0); if (block) - delay_flag &= (~O_NONBLOCK); + new_delay_flag = delay_flag & (~O_NONBLOCK); else - delay_flag |= O_NONBLOCK; - fcntl(s->sock_fd, F_SETFL, delay_flag); -#endif /* !__VMS */ + new_delay_flag = delay_flag | O_NONBLOCK; + if (new_delay_flag != delay_flag) + fcntl(s->sock_fd, F_SETFL, new_delay_flag); +#endif #else /* MS_WINDOWS */ block = !block; ioctlsocket(s->sock_fd, FIONBIO, (u_long*)&block); |