[Python-checkins] cpython (merge 3.6 -> default): Merge 3.6 (issue #28471)

yury.selivanov python-checkins at python.org
Tue Oct 18 16:04:57 EDT 2016


https://hg.python.org/cpython/rev/554fb699af8c
changeset:   104544:554fb699af8c
parent:      104542:40e97c9dae7a
parent:      104543:4b5b233d4c98
user:        Yury Selivanov <yury at magic.io>
date:        Tue Oct 18 16:04:40 2016 -0400
summary:
  Merge 3.6 (issue #28471)

files:
  Lib/test/test_socket.py |  12 ++++++++++++
  Modules/socketmodule.c  |  26 ++++++++++++++++----------
  2 files changed, 28 insertions(+), 10 deletions(-)


diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -4562,6 +4562,18 @@
         self.assertTrue(issubclass(socket.gaierror, OSError))
         self.assertTrue(issubclass(socket.timeout, OSError))
 
+    def test_setblocking_invalidfd(self):
+        # Regression test for issue #28471
+
+        sock0 = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
+        sock = socket.socket(
+            socket.AF_INET, socket.SOCK_STREAM, 0, sock0.fileno())
+        sock0.close()
+
+        with self.assertRaises(OSError):
+            sock.setblocking(False)
+
+
 @unittest.skipUnless(sys.platform == 'linux', 'Linux specific test')
 class TestLinuxAbstractNamespace(unittest.TestCase):
 
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -622,6 +622,7 @@
 static int
 internal_setblocking(PySocketSockObject *s, int block)
 {
+    int result = -1;
 #ifdef MS_WINDOWS
     u_long arg;
 #endif
@@ -641,34 +642,39 @@
 #if (defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO))
     block = !block;
     if (ioctl(s->sock_fd, FIONBIO, (unsigned int *)&block) == -1)
-        goto error;
+        goto done;
 #else
     delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
     if (delay_flag == -1)
-        goto error;
+        goto done;
     if (block)
         new_delay_flag = delay_flag & (~O_NONBLOCK);
     else
         new_delay_flag = delay_flag | O_NONBLOCK;
     if (new_delay_flag != delay_flag)
         if (fcntl(s->sock_fd, F_SETFL, new_delay_flag) == -1)
-            goto error;
+            goto done;
 #endif
 #else /* MS_WINDOWS */
     arg = !block;
     if (ioctlsocket(s->sock_fd, FIONBIO, &arg) != 0)
-        goto error;
+        goto done;
 #endif /* MS_WINDOWS */
+
+    result = 0;
+
+  done:
     Py_END_ALLOW_THREADS
 
-    return 0;
-  error:
+    if (result) {
 #ifndef MS_WINDOWS
-    PyErr_SetFromErrno(PyExc_OSError);
+        PyErr_SetFromErrno(PyExc_OSError);
 #else
-    PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
-#endif
-    return -1;
+        PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
+#endif
+    }
+
+    return result;
 }
 
 static int

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


More information about the Python-checkins mailing list