This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Backport repr(socket.socket) from Python 3.5 to Python 2.7
Type: enhancement Stage:
Components: Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: alex, gvanrossum, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2014-07-26 13:06 by vstinner, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
socket_repr.patch vstinner, 2014-07-26 13:06 review
Messages (9)
msg224053 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-07-26 13:06
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.0.0.0', 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));'
<socket._socketobject[closed]>

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.
msg224054 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-07-26 13:06
I also fixed repr(_socket.socket) on Windows 64-bit for closed sockets (on Python 2.7, 3.4 and 3.5):

changeset:   91881:04c916a1e82f
branch:      2.7
tag:         tip
user:        Victor Stinner <victor.stinner@gmail.com>
date:        Sat Jul 26 14:52:55 2014 +0200
files:       Lib/test/test_socket.py Misc/NEWS Modules/socketmodule.c
description:
Fix repr(_socket.socket) on Windows 64-bit: don't fail with OverflowError
on closed socket.


changeset:   91880:a86c273a1270
branch:      2.7
user:        Victor Stinner <victor.stinner@gmail.com>
date:        Sat Jul 26 14:47:56 2014 +0200
files:       Modules/socketmodule.c
description:
socketmodule.c: backport INVALID_SOCKET from Python 3.5 to simplify the code
msg224061 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2014-07-26 14:54
Antoine, what do you want me to do?  I think improving __repr__ of a socket sounds fine for some Python 2.7 bugfix release.
msg224065 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-07-26 15:09
I afraid this can break doctests. Isn't this against policy?
msg224069 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2014-07-26 15:32
I don't think it's against policy. Do doctests even work for objects that have an address as part of their repr()?
msg224070 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-07-26 15:47
See for example test_generators, test_genexps, test_xml_etree or 
ctypes.test.test_objects.

    >>> dict(a = (i for i in xrange(10))) #doctest: +ELLIPSIS
    {'a': <generator object <genexpr> at ...>}

    >>> repr(element)   # doctest: +ELLIPSIS
    "<Element 't\\xe4g' at 0x...>"

But unit tests can be broken too. When I enhanced reprs this week (issue22031, 
issue22032), I needed to correct failed tests. Due to this facts I applied 
patches only to 3.5.
msg224074 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2014-07-26 16:42
OK, I'm convinced. Sorry Victor.

On Saturday, July 26, 2014, Serhiy Storchaka <report@bugs.python.org> wrote:

>
> Serhiy Storchaka added the comment:
>
> See for example test_generators, test_genexps, test_xml_etree or
> ctypes.test.test_objects.
>
>     >>> dict(a = (i for i in xrange(10))) #doctest: +ELLIPSIS
>     {'a': <generator object <genexpr> at ...>}
>
>     >>> repr(element)   # doctest: +ELLIPSIS
>     "<Element 't\\xe4g' at 0x...>"
>
> But unit tests can be broken too. When I enhanced reprs this week
> (issue22031,
> issue22032), I needed to correct failed tests. Due to this facts I applied
> patches only to 3.5.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org <javascript:;>>
> <http://bugs.python.org/issue22081>
> _______________________________________
>
msg224075 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2014-07-26 16:44
Personally I don't think it is (or should) be against policy to change reprs, there's not really any way to improve them otherwise.

That said, my excitement level about this issue is pretty low, so I won't argue more than this :-)
msg224079 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-07-26 17:33
> I afraid this can break doctests. Isn't this against policy?

Ok, I close the issue. A workaround is to use repr(sock._sock) to use repr(_socket.socket) which contains a lot of information.
History
Date User Action Args
2022-04-11 14:58:06adminsetgithub: 66279
2014-07-26 17:33:25vstinnersetstatus: open -> closed
resolution: wont fix
messages: + msg224079
2014-07-26 16:44:57alexsetmessages: + msg224075
2014-07-26 16:42:26gvanrossumsetmessages: + msg224074
2014-07-26 15:47:29serhiy.storchakasetmessages: + msg224070
2014-07-26 15:32:51gvanrossumsetmessages: + msg224069
2014-07-26 15:09:24serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg224065
2014-07-26 14:54:03gvanrossumsetmessages: + msg224061
2014-07-26 14:31:03pitrousetnosy: + gvanrossum
2014-07-26 13:07:00vstinnersetmessages: + msg224054
2014-07-26 13:06:09vstinnersetnosy: + alex
2014-07-26 13:06:02vstinnercreate