Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in

order to accept exactly one connection.  Patch by Daniel Evers.
This commit is contained in:
Antoine Pitrou 2011-05-10 19:16:29 +02:00
parent cda41d3bf7
commit 1be815aac4
5 changed files with 20 additions and 6 deletions

View File

@ -652,8 +652,8 @@ correspond to Unix system calls applicable to sockets.
.. method:: socket.listen(backlog) .. method:: socket.listen(backlog)
Listen for connections made to the socket. The *backlog* argument specifies the 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 maximum number of queued connections and should be at least 0; the maximum value
is system-dependent (usually 5). is system-dependent (usually 5), the minimum value is forced to 0.
.. method:: socket.makefile(mode='r', buffering=None, *, encoding=None, \ .. method:: socket.makefile(mode='r', buffering=None, *, encoding=None, \

View File

@ -788,6 +788,13 @@ def test_name_closed_socketio(self):
fp.close() fp.close()
self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>") self.assertEqual(repr(fp), "<_io.BufferedReader name=-1>")
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.') @unittest.skipUnless(thread, 'Threading required for this test.')
class BasicTCPTest(SocketConnectedTest): class BasicTCPTest(SocketConnectedTest):

View File

@ -268,6 +268,7 @@ Carey Evans
Tim Everett Tim Everett
Paul Everitt Paul Everitt
David Everly David Everly
Daniel Evers
Greg Ewing Greg Ewing
Martijn Faassen Martijn Faassen
Clovis Fabricio Clovis Fabricio

View File

@ -86,6 +86,9 @@ Core and Builtins
Library 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 #11164: Stop trying to use _xmlplus in the xml module. - Issue #11164: Stop trying to use _xmlplus in the xml module.
- Issue #11927: SMTP_SSL now uses port 465 by default as documented. Patch - Issue #11927: SMTP_SSL now uses port 465 by default as documented. Patch

View File

@ -2220,8 +2220,10 @@ sock_listen(PySocketSockObject *s, PyObject *arg)
if (backlog == -1 && PyErr_Occurred()) if (backlog == -1 && PyErr_Occurred())
return NULL; return NULL;
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
if (backlog < 1) /* To avoid problems on systems that don't allow a negative backlog
backlog = 1; * (which doesn't make sense anyway) we force a minimum value of 0. */
if (backlog < 0)
backlog = 0;
res = listen(s->sock_fd, backlog); res = listen(s->sock_fd, backlog);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if (res < 0) if (res < 0)
@ -2234,8 +2236,9 @@ PyDoc_STRVAR(listen_doc,
"listen(backlog)\n\ "listen(backlog)\n\
\n\ \n\
Enable a server to accept connections. The backlog argument must be at\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\ least 0 (if it is lower, it is set to 0); it specifies the number of\n\
will allow before refusing new connections."); unaccepted connections that the system will allow before refusing new\n\
connections.");
/* /*