]> granicus.if.org Git - python/commitdiff
Close #19827: On UNIX, setblocking() and settimeout() methods of socket.socket
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 3 Dec 2013 23:41:24 +0000 (00:41 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Tue, 3 Dec 2013 23:41:24 +0000 (00:41 +0100)
can now avoid a second syscall if the ioctl() function can be used, or if the
non-blocking flag of the socket is unchanged.

Misc/NEWS
Modules/socketmodule.c

index 3bd4db4223327081b7579e3c198816878f3a969e..45f09b1e0b729509a3a0a2aea755d5811b9fe147 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #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.
+
 - Issue #19785: smtplib now supports SSLContext.check_hostname and server name
   indication for TLS/SSL connections.
 
index 9e0da132688c5b4766606bf48d68d15ff1201aa7..1444369e1b0a0d466e3956bba5eabd5aeb924ac0 100644 (file)
@@ -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);