Skip to content

Commit

Permalink
fix contextlib.closing bug for sessions where content is not consumed…
Browse files Browse the repository at this point in the history
… (issue psf#2593)
  • Loading branch information
colindickson committed May 24, 2015
1 parent 3c850b3 commit 1da62a6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -158,3 +158,4 @@ Patches and Suggestions
- Ulrich Petri (`@ulope <https://github.com/ulope>`_)
- Muhammad Yasoob Ullah Khalid <yasoob.khld@gmail.com> (`@yasoob <https://github.com/yasoob>`_)
- Paul van der Linden (`@pvanderlinden <https://github.com/pvanderlinden>`_)
- Colin Dickson (`@colindickson <https://github.com/colindickson>`_)
3 changes: 3 additions & 0 deletions requests/models.py
Expand Up @@ -842,4 +842,7 @@ def close(self):
*Note: Should not normally need to be called explicitly.*
"""
if not self._content_consumed:
return self.raw.close()

return self.raw.release_conn()
10 changes: 10 additions & 0 deletions test_requests.py
Expand Up @@ -9,6 +9,7 @@
import pickle
import unittest
import collections
import contextlib

import io
import requests
Expand Down Expand Up @@ -1060,6 +1061,15 @@ def test_response_iter_lines(self):
next(it)
assert len(list(it)) == 3

def test_unconsumed_session_response_closes_connection(self):
s = requests.session()

with contextlib.closing(s.get(httpbin('stream/4'), stream=True)) as response:
pass

self.assertEqual(response._content_consumed, False)
self.assertTrue(response.raw.closed, True)

@pytest.mark.xfail
def test_response_iter_lines_reentrant(self):
"""Response.iter_lines() is not reentrant safe"""
Expand Down

0 comments on commit 1da62a6

Please sign in to comment.