summaryrefslogtreecommitdiffstats
path: root/Lib/socket.py
diff options
context:
space:
mode:
authorGiampaolo Rodola' <g.rodola@gmail.com>2013-04-10 13:49:47 (GMT)
committerGiampaolo Rodola' <g.rodola@gmail.com>2013-04-10 13:49:47 (GMT)
commit50331cbf0894bd18d8c89669297776153036bbbb (patch)
treeb30a7b38583fb157f604a03450a7ecabddac4cc7 /Lib/socket.py
parent7d36e4f074a0141966394284f877bbe2270ccd93 (diff)
downloadcpython-50331cbf0894bd18d8c89669297776153036bbbb.zip
cpython-50331cbf0894bd18d8c89669297776153036bbbb.tar.gz
cpython-50331cbf0894bd18d8c89669297776153036bbbb.tar.bz2
Fix issue #17675: make socket repr() provide local and remote addresses (if any).
Diffstat (limited to 'Lib/socket.py')
-rw-r--r--Lib/socket.py33
1 files changed, 26 insertions, 7 deletions
diff --git a/Lib/socket.py b/Lib/socket.py
index da0c39a..96f8ed0 100644
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -103,13 +103,32 @@ class socket(_socket.socket):
self.close()
def __repr__(self):
- """Wrap __repr__() to reveal the real class name."""
- s = _socket.socket.__repr__(self)
- if s.startswith("<socket object"):
- s = "<%s.%s%s%s" % (self.__class__.__module__,
- self.__class__.__name__,
- getattr(self, '_closed', False) and " [closed] " or "",
- s[7:])
+ """Wrap __repr__() to reveal the real class name and socket
+ address(es).
+ """
+ closed = getattr(self, '_closed', False)
+ s = "<%s.%s%s fd=%i, family=%i, type=%i, proto=%i" \
+ % (self.__class__.__module__,
+ self.__class__.__name__,
+ " [closed]" if closed else "",
+ self.fileno(),
+ self.family,
+ self.type,
+ self.proto)
+ if not closed:
+ try:
+ laddr = self.getsockname()
+ if laddr:
+ s += ", laddr=%s" % str(laddr)
+ except error:
+ pass
+ try:
+ raddr = self.getpeername()
+ if raddr:
+ s += ", raddr=%s" % str(raddr)
+ except error:
+ pass
+ s += '>'
return s
def __getstate__(self):