[pypy-issue] Issue #2561: Passing a buffersize of 0 to socket.getsockopt should be equivalent to not passing it at all (pypy/pypy)
Nathaniel Smith
issues-reply at bitbucket.org
Fri May 19 02:01:44 EDT 2017
New issue 2561: Passing a buffersize of 0 to socket.getsockopt should be equivalent to not passing it at all
https://bitbucket.org/pypy/pypy/issues/2561/passing-a-buffersize-of-0-to
Nathaniel Smith:
With CPython:
```python
>>> import socket
>>> s = socket.socket()
>>> s.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 0)
0
```
With latest PyPy3 nightly build:
```python
>>>> import socket
>>>> s = socket.socket()
>>>> s.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 0)
b''
```
As a special case, CPython treats a zero value for the third argument ("buffersize") as equivalent to it not being passed at all. This isn't documented in the online docs, but it is in the docstring:
```python
>>> print(s.getsockopt.__doc__)
getsockopt(level, option[, buffersize]) -> value
Get a socket option. See the Unix manual for level and option.
If a nonzero buffersize argument is given, the return value is a
string of that length; otherwise it is an integer.
```
(notice key word "nonzero")
This is important because if you want to make a wrapper around getsockopt that pass arguments through, it's convenient to make the wrapper set `buffersize=0` and then unconditionally pass `buffersize` to `socket.getsockopt`.
More information about the pypy-issue
mailing list