summaryrefslogtreecommitdiffstats
path: root/Lib/socket.py
diff options
context:
space:
mode:
authorGiampaolo RodolĂ  <g.rodola@gmail.com>2010-09-08 22:44:12 (GMT)
committerGiampaolo RodolĂ  <g.rodola@gmail.com>2010-09-08 22:44:12 (GMT)
commitb383dbb45e62054e3a73792b7e903178bee18aa8 (patch)
tree929fb3d74b0bfb2d5427789722ee3363b535e7c4 /Lib/socket.py
parent7c9cf01238cdfb597041d12e71d5a11a98402b34 (diff)
downloadcpython-b383dbb45e62054e3a73792b7e903178bee18aa8.zip
cpython-b383dbb45e62054e3a73792b7e903178bee18aa8.tar.gz
cpython-b383dbb45e62054e3a73792b7e903178bee18aa8.tar.bz2
Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Diffstat (limited to 'Lib/socket.py')
-rw-r--r--Lib/socket.py7
1 files changed, 7 insertions, 0 deletions
diff --git a/Lib/socket.py b/Lib/socket.py
index 004d6a9..bfc9a726 100644
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -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)