Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.

This commit is contained in:
Giampaolo Rodolà 2010-09-08 22:44:12 +00:00
parent 7c9cf01238
commit b383dbb45e
4 changed files with 60 additions and 0 deletions

View file

@ -93,6 +93,13 @@ class socket(_socket.socket):
self._io_refs = 0
self._closed = False
def __enter__(self):
return self
def __exit__(self, *args):
if not self._closed:
self.close()
def __repr__(self):
"""Wrap __repr__() to reveal the real class name."""
s = _socket.socket.__repr__(self)