Skip to content

Commit

Permalink
Fixed so that safe_mode works for Sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
heyman committed Nov 23, 2012
1 parent 991f47a commit 8269ee7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,4 @@ Patches and Suggestions
- André Graf (dergraf)
- Stephen Zhuang (everbird)
- Martijn Pieters
- Jonatan Heyman
2 changes: 0 additions & 2 deletions requests/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
"""

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 <Request>`.
Returns :class:`Response <Response>` object.
Expand Down
10 changes: 5 additions & 5 deletions requests/safe_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@


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()
r.error = e
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
8 changes: 7 additions & 1 deletion requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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):
Expand Down Expand Up @@ -265,7 +266,12 @@ def request(self, method, url,
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
Expand Down
13 changes: 13 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,19 @@ def test_unpickled_session_requests(self):
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:
Expand Down

0 comments on commit 8269ee7

Please sign in to comment.