[Python-checkins] cpython: Close #19827: On UNIX, setblocking() and settimeout() methods of socket.socket

victor.stinner python-checkins at python.org
Wed Dec 4 00:45:19 CET 2013


http://hg.python.org/cpython/rev/5f0d1aad7322
changeset:   87747:5f0d1aad7322
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Dec 04 00:41:24 2013 +0100
summary:
  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.

files:
  Misc/NEWS              |   4 ++++
  Modules/socketmodule.c |  18 ++++++++++--------
  2 files changed, 14 insertions(+), 8 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,10 @@
 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.
 
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -585,8 +585,9 @@
 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 @@
 
     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);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list