From: Ori Livneh Date: Thu, 22 Dec 2011 21:52:29 +0000 (-0500) Subject: Make safe_mode play nice with timeout (fixes #312) X-Git-Tag: v0.8.7~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=12f3c65e5a426ce32808cc456387ca7d7e7c5126;p=services%2Fpython-requests.git Make safe_mode play nice with timeout (fixes #312) --- diff --git a/requests/models.py b/requests/models.py index 4bef293..f9a9626 100644 --- a/requests/models.py +++ b/requests/models.py @@ -438,6 +438,10 @@ class Request(object): # Attach Cookie header to request. self.headers['Cookie'] = cookie_header + # If the request fails but exceptions are suppressed, + # we'll build a blank response. + r = None + try: # Send the request. r = conn.urlopen( @@ -458,8 +462,6 @@ class Request(object): except MaxRetryError, e: if not self.config.get('safe_mode', False): raise ConnectionError(e) - else: - r = None except (_SSLError, _HTTPError), e: if not self.config.get('safe_mode', False): @@ -597,7 +599,7 @@ class Response(object): def generate(): chunk = [] - while 1: + while True: c = self.raw.read(1) if not c: break diff --git a/test_requests.py b/test_requests.py index ebcb596..e547aa6 100755 --- a/test_requests.py +++ b/test_requests.py @@ -619,6 +619,17 @@ class RequestsTestSuite(unittest.TestCase): lines = '\n'.join(r.iter_lines()) self.assertEqual(lines, quote) + def test_timeout(self): + + # When not in safe mode, should raise Timeout exception + with self.assertRaises(requests.exceptions.Timeout): + r = requests.get(httpbin('stream', '1000'), timeout=0.0001) + + # In safe mode, should return a blank response + r = requests.get(httpbin('stream', '1000'), timeout=0.0001, + config=dict(safe_mode=True)) + self.assertIsNone(r.content) + if __name__ == '__main__': unittest.main()