]> granicus.if.org Git - python/commitdiff
Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 10 May 2011 17:16:03 +0000 (19:16 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 10 May 2011 17:16:03 +0000 (19:16 +0200)
order to accept exactly one connection.  Patch by Daniel Evers.

Doc/library/socket.rst
Lib/test/test_socket.py
Misc/ACKS
Misc/NEWS
Modules/socketmodule.c

index 96152f9d395f382c7ad5be2b82155a2d70f5205b..d6b1e4c5e5f44746713e0534fa50a4e53098701a 100644 (file)
@@ -644,8 +644,8 @@ correspond to Unix system calls applicable to sockets.
 .. method:: socket.listen(backlog)
 
    Listen for connections made to the socket.  The *backlog* argument specifies the
-   maximum number of queued connections and should be at least 1; the maximum value
-   is system-dependent (usually 5).
+   maximum number of queued connections and should be at least 0; the maximum value
+   is system-dependent (usually 5), the minimum value is forced to 0.
 
 
 .. method:: socket.makefile([mode[, bufsize]])
index 7ee16ebced72d93e754d75dc0d4f6fc2dd341a7b..12e09dddeb6102dfd58a25a1bb493584d0a5af9c 100644 (file)
@@ -700,6 +700,13 @@ class GeneralModuleTests(unittest.TestCase):
     def test_sendall_interrupted_with_timeout(self):
         self.check_sendall_interrupted(True)
 
+    def testListenBacklog0(self):
+        srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        srv.bind((HOST, 0))
+        # backlog = 0
+        srv.listen(0)
+        srv.close()
+
 
 @unittest.skipUnless(thread, 'Threading required for this test.')
 class BasicTCPTest(SocketConnectedTest):
index 9b9ff6852352d5ed2604dd4f0499eca277aeb07d..4270934f234f73dc2ddfc5963bdf9d0a85c0022c 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -243,6 +243,7 @@ Carey Evans
 Tim Everett
 Paul Everitt
 David Everly
+Daniel Evers
 Greg Ewing
 Martijn Faassen
 Clovis Fabricio
index 0214c0befb3e5fd1a5b5d1df0c2278d175371de8..7517baf64874193d45e6108e10b47555f9bfa0ae 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -80,6 +80,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
+  order to accept exactly one connection.  Patch by Daniel Evers.
+
 - Issue #12012: ssl.PROTOCOL_SSLv2 becomes optional.
 
 - Issue #11164: Remove obsolete allnodes test from minidom test.
index 96b83b1150be683db3976faeb09dfcc46f8dc8d5..76d860cac50e81b3744a12d9b47478373350346c 100644 (file)
@@ -2244,8 +2244,10 @@ sock_listen(PySocketSockObject *s, PyObject *arg)
     if (backlog == -1 && PyErr_Occurred())
         return NULL;
     Py_BEGIN_ALLOW_THREADS
-    if (backlog < 1)
-        backlog = 1;
+    /* To avoid problems on systems that don't allow a negative backlog
+     * (which doesn't make sense anyway) we force a minimum value of 0. */
+    if (backlog < 0)
+        backlog = 0;
     res = listen(s->sock_fd, backlog);
     Py_END_ALLOW_THREADS
     if (res < 0)
@@ -2258,8 +2260,9 @@ PyDoc_STRVAR(listen_doc,
 "listen(backlog)\n\
 \n\
 Enable a server to accept connections.  The backlog argument must be at\n\
-least 1; it specifies the number of unaccepted connection that the system\n\
-will allow before refusing new connections.");
+least 0 (if it is lower, it is set to 0); it specifies the number of\n\
+unaccepted connections that the system will allow before refusing new\n\
+connections.");
 
 
 #ifndef NO_DUP