Skip to content

Commit

Permalink
Assure session is closed on exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
aartur committed Nov 6, 2015
1 parent f7e0494 commit 1ced004
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions requests/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ def request(method, url, **kwargs):
<Response [200]>
"""

session = sessions.Session()
response = session.request(method=method, url=url, **kwargs)
# By explicitly closing the session, we avoid leaving sockets open which
# can trigger a ResourceWarning in some cases, and look like a memory leak
# in others.
session.close()
return response
# By using the 'with' statement we are sure the session is closed, thus we
# avoid leaving sockets open which can trigger a ResourceWarning in some
# cases, and look like a memory leak in others.
with sessions.Session() as session:
return session.request(method=method, url=url, **kwargs)


def get(url, params=None, **kwargs):
Expand Down

0 comments on commit 1ced004

Please sign in to comment.