Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit e89f145

Browse files
committed
Context managers on all connections
1 parent 5fc3d77 commit e89f145

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

hyper/common/connection.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ def request(self, method, url, body=None, headers={}):
107107
method=method, url=url, body=body, headers=headers
108108
)
109109

110+
# The following two methods are the implementation of the context manager
111+
# protocol.
112+
def __enter__(self):
113+
return self
114+
115+
def __exit__(self, type, value, tb):
116+
self.close()
117+
return False # Never swallow exceptions.
118+
110119
# Can anyone say 'proxy object pattern'?
111120
def __getattr__(self, name):
112121
return getattr(self._conn, name)

hyper/http11/connection.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,12 @@ def close(self):
289289
"""
290290
self._sock.close()
291291
self._sock = None
292+
293+
# The following two methods are the implementation of the context manager
294+
# protocol.
295+
def __enter__(self):
296+
return self
297+
298+
def __exit__(self, type, value, tb):
299+
self.close()
300+
return False # Never swallow exceptions.

0 commit comments

Comments
 (0)