[New-bugs-announce] [issue22081] Backport repr(socket.socket) from Python 3.5 to Python 2.7

STINNER Victor report at bugs.python.org
Sat Jul 26 15:06:02 CEST 2014

New submission from STINNER Victor:

Currently, the C module _socket has an useful representation of socket: it gives the file descriptor, family, type, etc. The Python socket module only shows the memory address. Example:

$ ./python -c 'import _socket; s=_socket.socket(); print(repr(s));'
<socket object, fd=3, family=2, type=1, protocol=0>

$ ./python -c 'import socket; s=socket.socket(); print(repr(s));'
<socket._socketobject object at 0x7fad1fdcbba0>

I propose to backport repr(socket.socket) from Python 3.5 to Python 2.7. With the patch, the Python socket even contains *more* information than the C module (laddr and raddr). Example with the patch applied:

$ ./python -c 'import socket; s=socket.socket(); print(repr(s));'
<socket._socketobject fd=3, family=2, type=1, proto=0, laddr=('', 0)>

In Python 2.7, when a socket is closed, it drops the underlying C _socket object. So it's not possible to provide a better representation than:

$ ./python -c 'import socket; s=socket.socket(); s.close(); print(repr(s));'

I don't want to change the design of the Python module, Python 2.7 is very stable. I don't want to take the risk of breaking anything.

files: socket_repr.patch
keywords: patch
messages: 224053
nosy: haypo
priority: normal
severity: normal
status: open
title: Backport repr(socket.socket) from Python 3.5 to Python 2.7
type: enhancement
versions: Python 2.7
Added file: http://bugs.python.org/file36109/socket_repr.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list