[pypy-commit] pypy sockopt_zero: added the possibility to have zero sized buffer length

Alecsandru Patrascu pypy.commits at gmail.com
Mon May 22 09:52:08 EDT 2017


Author: Alecsandru Patrascu <alecsandru.patrascu at rinftech.com>
Branch: sockopt_zero
Changeset: r91365:74c6874f803f
Date: 2017-05-22 11:02 +0300
http://bitbucket.org/pypy/pypy/changeset/74c6874f803f/

Log:	added the possibility to have zero sized buffer length

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
@@ -311,6 +311,8 @@
             except SocketError as e:
                 raise converted_error(space, e)
         buflen = space.int_w(w_buflen)
+        if buflen == 0:
+            return space.newint(0)
         return space.newbytes(self.sock.getsockopt(level, optname, buflen))
 
     def gettimeout_w(self, space):
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
@@ -582,6 +582,16 @@
         (reuse,) = struct.unpack('i', reusestr)
         assert reuse != 0
 
+    def test_getsockopt_zero(self):
+        # related to issue #2561: in CPython, when setting the buffer size:
+        # if 0, should return 0,
+        # otherwise an empty buffer of the specified size
+        import _socket
+        s = _socket.socket()
+
+        assert s.getsockopt(_socket.IPPROTO_TCP, _socket.TCP_NODELAY, 0) == 0
+        assert s.getsockopt(_socket.IPPROTO_TCP, _socket.TCP_NODELAY, 2) == b'\x00\x00'
+
     def test_socket_ioctl(self):
         import _socket, sys
         if sys.platform != 'win32':


More information about the pypy-commit mailing list