diff options
author | Giampaolo Rodola' <g.rodola@gmail.com> | 2013-04-10 13:49:47 (GMT) |
---|---|---|
committer | Giampaolo Rodola' <g.rodola@gmail.com> | 2013-04-10 13:49:47 (GMT) |
commit | 50331cbf0894bd18d8c89669297776153036bbbb (patch) | |
tree | b30a7b38583fb157f604a03450a7ecabddac4cc7 /Lib/socket.py | |
parent | 7d36e4f074a0141966394284f877bbe2270ccd93 (diff) | |
download | cpython-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.py | 33 |
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): |