diff -r 513f6dfd3173 Doc/library/socket.rst --- a/Doc/library/socket.rst Sun May 01 20:24:59 2011 -0500 +++ b/Doc/library/socket.rst Wed May 04 11:36:09 2011 +0200 @@ -662,8 +662,8 @@ .. 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='r', buffering=None, *, encoding=None, \ diff -r 513f6dfd3173 Lib/test/test_socket.py --- a/Lib/test/test_socket.py Sun May 01 20:24:59 2011 -0500 +++ b/Lib/test/test_socket.py Wed May 04 11:36:09 2011 +0200 @@ -2009,10 +2009,26 @@ self.checkNonblock(s, False) socket.setdefaulttimeout(t) +class SocketListenBacklog0Test(unittest.TestCase): + """Socket tests for client-server connection to verify whether + a backlog of 0 works for socket.listen(). + """ + + def __init__(self, methodName = 'runTest'): + unittest.TestCase.__init__(self, methodName = methodName) + + def testListenBacklog0(self): + srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + port = support.bind_port(srv) + # backlog = 0 + srv.listen(0) + srv.close() + def test_main(): tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, - TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeoutTest ] + TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeoutTest, + SocketListenBacklog0Test] tests.extend([ NonBlockingTCPTests, diff -r 513f6dfd3173 Modules/socketmodule.c --- a/Modules/socketmodule.c Sun May 01 20:24:59 2011 -0500 +++ b/Modules/socketmodule.c Wed May 04 11:36:09 2011 +0200 @@ -2220,8 +2220,10 @@ 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) @@ -2234,8 +2236,9 @@ "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."); /*