[pypy-commit] pypy py3.5: SOMAXCONN, socket.listen(no argument)

arigo pypy.commits at gmail.com
Tue Sep 13 13:31:28 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r87091:22dc852ebf3b
Date: 2016-09-13 19:30 +0200
http://bitbucket.org/pypy/pypy/changeset/22dc852ebf3b/

Log:	SOMAXCONN, socket.listen(no argument)

diff --git a/pypy/module/_socket/__init__.py b/pypy/module/_socket/__init__.py
--- a/pypy/module/_socket/__init__.py
+++ b/pypy/module/_socket/__init__.py
@@ -1,4 +1,6 @@
 from pypy.interpreter.mixedmodule import MixedModule
+from rpython.rlib.rsocket import SOMAXCONN
+
 
 class Module(MixedModule):
 
@@ -12,6 +14,7 @@
         'herror'    :  'interp_socket.get_error(space, "herror")',
         'gaierror'  :  'interp_socket.get_error(space, "gaierror")',
         'timeout'   :  'interp_socket.get_error(space, "timeout")',
+        'SOMAXCONN' :  'space.wrap(%d)' % SOMAXCONN,
     }
 
     def startup(self, space):
diff --git a/pypy/module/_socket/interp_socket.py b/pypy/module/_socket/interp_socket.py
--- a/pypy/module/_socket/interp_socket.py
+++ b/pypy/module/_socket/interp_socket.py
@@ -3,7 +3,7 @@
 from rpython.rlib.rarithmetic import intmask
 from rpython.rlib.rsocket import (
     RSocket, AF_INET, SOCK_STREAM, SocketError, SocketErrorWithErrno,
-    RSocketError
+    RSocketError, SOMAXCONN
 )
 from rpython.rtyper.lltypesystem import lltype, rffi
 
@@ -359,13 +359,16 @@
         return space.wrap(timeout)
 
     @unwrap_spec(backlog="c_int")
-    def listen_w(self, space, backlog):
+    def listen_w(self, space, backlog=min(SOMAXCONN, 128)):
         """listen(backlog)
 
         Enable a server to accept connections.  The backlog argument must be at
-        least 1; it specifies the number of unaccepted connection that the system
-        will allow before refusing new connections.
+        least 0 (if it is lower, it is set to 0); it specifies the number of
+        unaccepted connection that the system will allow before refusing new
+        connections. If not specified, a default reasonable value is chosen.
         """
+        if backlog < 0:
+            backlog = 0
         try:
             self.sock.listen(backlog)
         except SocketError as e:
diff --git a/pypy/module/_socket/test/test_sock_app.py b/pypy/module/_socket/test/test_sock_app.py
--- a/pypy/module/_socket/test/test_sock_app.py
+++ b/pypy/module/_socket/test/test_sock_app.py
@@ -584,6 +584,11 @@
         s.sendto(memoryview(b''), ('localhost', 9)) # Send to discard port.
         s.close()
 
+    def test_listen_default(self):
+        import _socket
+        _socket.socket().listen()
+        assert isinstance(_socket.SOMAXCONN, int)
+
     def test_unix_socket_connect(self):
         import _socket, os
         if not hasattr(_socket, 'AF_UNIX'):


More information about the pypy-commit mailing list