mirror of
https://github.com/python/cpython.git
synced 2025-08-25 03:04:55 +00:00
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:
parent
7c9cf01238
commit
b383dbb45e
4 changed files with 60 additions and 0 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue