summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-05-10 19:19:13 +0200
committerAntoine Pitrou <solipsis@pitrou.net>2011-05-10 19:19:13 +0200
commit462bdd8cc05e12fb2a68f0329ccd2e42a8e17c47 (patch)
tree4145b454ce18e89412cda8884e5c2efc116775e8
parent42bd637389cf900ae1fdca47b3fb688b39679fdf (diff)
parent05b2fcfa19d374e219d5f0250bf3bb100797a5c2 (diff)
downloadcpython-462bdd8cc05e12fb2a68f0329ccd2e42a8e17c47.tar.gz
Issue #8498: In socket.accept(), allow to specify 0 as a backlog value in
order to accept exactly one connection. Patch by Daniel Evers.
-rw-r--r--Doc/library/socket.rst4
-rw-r--r--Lib/test/test_socket.py7
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/socketmodule.c11
5 files changed, 20 insertions, 6 deletions
diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst
index 65533df2cd..0357be79c0 100644
--- a/Doc/library/socket.rst
+++ b/Doc/library/socket.rst
@@ -662,8 +662,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='r', buffering=None, *, encoding=None, \
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index c811ec445c..3efe1bbc0c 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -797,6 +797,13 @@ class GeneralModuleTests(unittest.TestCase):
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
self.assertRaises(TypeError, pickle.dumps, sock, protocol)
+ def test_listen_backlog0(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):
diff --git a/Misc/ACKS b/Misc/ACKS
index 297dccac17..36778ff2db 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -270,6 +270,7 @@ Carey Evans
Tim Everett
Paul Everitt
David Everly
+Daniel Evers
Winston Ewert
Greg Ewing
Martijn Faassen
diff --git a/Misc/NEWS b/Misc/NEWS
index 2b0529e789..6b9da84672 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -143,6 +143,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 #12011: signal.signal() and signal.siginterrupt() raise an OSError,
instead of a RuntimeError: OSError has an errno attribute.
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 1631363739..b05f089a70 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -2220,8 +2220,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)
@@ -2234,8 +2236,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.");
/*