From 1922ddac389270301e40a0589127cb87262f7cad Mon Sep 17 00:00:00 2001 From: Vincent Besanceney Date: Tue, 3 Dec 2013 15:25:55 +0100 Subject: [PATCH] Ensure connections are properly closed on object destruction Signed-off-by: Vincent Besanceney --- urllib3/_collections.py | 3 +++ urllib3/connectionpool.py | 3 +++ urllib3/poolmanager.py | 3 +++ 3 files changed, 9 insertions(+) diff --git a/urllib3/_collections.py b/urllib3/_collections.py index 5907b0dc7c..ff4316cce9 100644 --- a/urllib3/_collections.py +++ b/urllib3/_collections.py @@ -51,6 +51,9 @@ def __init__(self, maxsize=10, dispose_func=None): self._container = self.ContainerCls() self.lock = RLock() + def __del__(self): + self.clear() + def __getitem__(self, key): # Re-insert the item, moving it to the end of the eviction line. with self.lock: diff --git a/urllib3/connectionpool.py b/urllib3/connectionpool.py index 72011b5a33..8f45c73704 100644 --- a/urllib3/connectionpool.py +++ b/urllib3/connectionpool.py @@ -167,6 +167,9 @@ def __init__(self, host, port=None, strict=False, self.num_connections = 0 self.num_requests = 0 + def __del__(self): + self.close() + def _new_conn(self): """ Return a fresh :class:`httplib.HTTPConnection`. diff --git a/urllib3/poolmanager.py b/urllib3/poolmanager.py index c16519f883..ccf18a31fe 100644 --- a/urllib3/poolmanager.py +++ b/urllib3/poolmanager.py @@ -68,6 +68,9 @@ def __init__(self, num_pools=10, headers=None, **connection_pool_kw): self.pools = RecentlyUsedContainer(num_pools, dispose_func=lambda p: p.close()) + def __del__(self): + self.clear() + def _new_pool(self, scheme, host, port): """ Create a new :class:`ConnectionPool` based on host, port and scheme.