From 662c3edacc76d6a0e7b55b569c9fea38f8b0bc78 Mon Sep 17 00:00:00 2001 From: Michael Komitee Date: Fri, 1 Jun 2012 15:53:23 -0400 Subject: [PATCH] Allowing hooks to return responses that indicate errors Since response objects for failures (4xx/5xx responses) evaluate to False in a boolean context, any hook which returns such a failure response will evaluate to False. The way hooks were setup, any failure response resulting from a hook would be ignored, and the initial response before it got processed by the hook would be substituted in its place. This commit changes that logic to test for None so that hooks that return failures can do so. --- requests/hooks.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/requests/hooks.py b/requests/hooks.py index 13d0eb5..272abb7 100644 --- a/requests/hooks.py +++ b/requests/hooks.py @@ -30,7 +30,6 @@ import traceback HOOKS = ('args', 'pre_request', 'pre_send', 'post_request', 'response') - def dispatch_hook(key, hooks, hook_data): """Dispatches a hook dictionary on a given piece of data.""" @@ -44,7 +43,10 @@ def dispatch_hook(key, hooks, hook_data): for hook in hooks: try: - hook_data = hook(hook_data) or hook_data + _hook_data = hook(hook_data) + if _hook_data is not None: + hook_data = _hook_data + except Exception: traceback.print_exc() -- 2.7.4