From: Jonatan Heyman Date: Fri, 23 Nov 2012 15:48:51 +0000 (+0100) Subject: Fixed so that safe_mode works for Sessions X-Git-Tag: v1.0.0~104^2~2^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8269ee726656cc76e67f02e7f37b90c8d01f5520;p=services%2Fpython-requests.git Fixed so that safe_mode works for Sessions --- diff --git a/AUTHORS.rst b/AUTHORS.rst index 3c074d1..e18d575 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -116,3 +116,4 @@ Patches and Suggestions - André Graf (dergraf) - Stephen Zhuang (everbird) - Martijn Pieters +- Jonatan Heyman diff --git a/requests/api.py b/requests/api.py index ded7935..297f4cb 100644 --- a/requests/api.py +++ b/requests/api.py @@ -12,10 +12,8 @@ This module implements the Requests API. """ from . import sessions -from .safe_mode import catch_exceptions_if_in_safe_mode -@catch_exceptions_if_in_safe_mode def request(method, url, **kwargs): """Constructs and sends a :class:`Request `. Returns :class:`Response ` object. diff --git a/requests/safe_mode.py b/requests/safe_mode.py index 0fb8d70..18808d7 100644 --- a/requests/safe_mode.py +++ b/requests/safe_mode.py @@ -18,17 +18,17 @@ import socket def catch_exceptions_if_in_safe_mode(function): - """New implementation of safe_mode. We catch all exceptions at the API level + """New implementation of safe_mode. We catch all exceptions at the Session level and then return a blank Response object with the error field filled. This decorator - wraps request() in api.py. + wraps Session._send_request() in sessions.py. """ - def wrapped(method, url, **kwargs): + def wrapped(*args, **kwargs): # if save_mode, we catch exceptions and fill error field if (kwargs.get('config') and kwargs.get('config').get('safe_mode')) or (kwargs.get('session') and kwargs.get('session').config.get('safe_mode')): try: - return function(method, url, **kwargs) + return function(*args, **kwargs) except (RequestException, ConnectionError, HTTPError, socket.timeout, socket.gaierror) as e: r = Response() @@ -36,5 +36,5 @@ def catch_exceptions_if_in_safe_mode(function): r.raw = HTTPResponse() # otherwise, tests fail r.status_code = 0 # with this status_code, content returns None return r - return function(method, url, **kwargs) + return function(*args, **kwargs) return wrapped diff --git a/requests/sessions.py b/requests/sessions.py index 0962d81..5d67b4d 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -17,6 +17,7 @@ from .models import Request from .hooks import dispatch_hook from .utils import header_expand, from_key_val_list from .packages.urllib3.poolmanager import PoolManager +from .safe_mode import catch_exceptions_if_in_safe_mode def merge_kwargs(local_kwarg, default_kwarg): @@ -265,7 +266,12 @@ class Session(object): return r # Send the HTTP Request. - r.send(prefetch=prefetch) + return self._send_request(r, **args) + + @catch_exceptions_if_in_safe_mode + def _send_request(self, r, **kwargs): + # Send the request. + r.send(prefetch=kwargs.get("prefetch")) # Return the response. return r.response diff --git a/tests/test_requests.py b/tests/test_requests.py index 6615678..cf326ac 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -929,6 +929,19 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): ds2 = pickle.loads(pickle.dumps(requests.session(prefetch=False))) self.assertTrue(ds1.prefetch) self.assertFalse(ds2.prefetch) + + def test_session_connection_error_with_safe_mode(self): + config = {"safe_mode":True} + + s = requests.session() + r = s.get("http://localhost:1/nope", timeout=0.1, config=config) + self.assertFalse(r.ok) + self.assertTrue(r.content is None) + + s2 = requests.session(config=config) + r2 = s2.get("http://localhost:1/nope", timeout=0.1) + self.assertFalse(r2.ok) + self.assertTrue(r2.content is None) def test_connection_error(self): try: