Skip to content
Myr

Merge pull request #4137 from edmorley/response-context-manager

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Allow Requests.Response to be used as a context manager

authored by Cory Benfield · committed by GitHub · parents 40ce814, 4847f5b · Browse files · 5 files+20 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+20 added−6 removed

  • tests:+6 added−0 removed
  • other:+14 added−6 removed

5 files+20 added−6 removed

modified:docs/user/advanced.rst+2 added-6 removed
299 unmodified lines
300release the connection back to the pool unless you consume all the data or call301:meth:`Response.close <requests.Response.close>`. This can lead to302inefficiency with connections. If you find yourself partially reading request303bodies (or not reading them at all) while using ``stream=True``, you should304consider using ``contextlib.closing`` (`documented here`_), like this::304make the request within a ``with`` statement to ensure it's always closed::305306 from contextlib import closing307308 with closing(requests.get('http://httpbin.org/get', stream=True)) as r:306 with requests.get('http://httpbin.org/get', stream=True) as r:307 # Do things with the response here.308311.. _`documented here`: http://docs.python.org/2/library/contextlib.html#contextlib.closing312309.. _keep-alive:310311Keep-Alive312----------
676 unmodified lines
modified:requests/models.py+6 added
632 unmodified lines
633 #: The :class:`PreparedRequest <PreparedRequest>` object to which this634 #: is a response.635 self.request = None636637 def __enter__(self):638 return self639640 def __exit__(self, *args):641 self.close()642643 def __getstate__(self):644 # Consume everything; accessing the content attribute makes645 # sure the content has been fully read.646 if not self._content_consumed:
304 unmodified lines
modified:tests/test_requests.py+6 added
1666 unmodified lines
1667 it = r.iter_lines()1668 next(it)1669 assert len(list(it)) == 316701671 def test_response_context_manager(self, httpbin):1672 with requests.get(httpbin('stream/4'), stream=True) as response:1673 assert isinstance(response, requests.Response)16741675 assert response.raw.closed16761677 def test_unconsumed_session_response_closes_connection(self, httpbin):1678 s = requests.session()16791680 with contextlib.closing(s.get(httpbin('stream/4'), stream=True)) as response:
698 unmodified lines
modified:AUTHORS.rst+1 added
181 unmodified lines
182- David Fontenot (`@davidfontenot <https://github.com/davidfontenot>`_)183- Shmuel Amar (`@shmuelamar <https://github.com/shmuelamar>`_)184- Gary Wu (`@garywu <https://github.com/garywu>`_)185- Ryan Pineo (`@ryanpineo <https://github.com/ryanpineo>`_)186- Ed Morley (`@edmorley <https://github.com/edmorley>`_)
modified:HISTORY.rst+5 added
4 unmodified lines
56dev7+++89**Improvements**1011- ``Response`` is now a context manager, so can be used directly in a `with` statement12 without first having to be wrapped by ``contextlib.closing()``.1314**Bugfixes**1516- Resolve installation failure if multiprocessing is not available17- Resolve tests crash if multiprocessing is not able to determine the number of CPU cores
1467 unmodified lines