Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
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 @@
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)