From 77677eb71d41f20d657254b7d88c17337369e600 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Mon, 1 Oct 2012 13:09:02 -0400 Subject: [PATCH] Only register callable items in lists Prior to this, you could sneak a list of anything to register_hook and it would accept it. This will check if the items in the list are callable before registering them. Also added a regression test to make sure if this gets changed it will be noticed. --- requests/models.py | 6 +++--- tests/test_requests.py | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/requests/models.py b/requests/models.py index 2193c6e..7831149 100644 --- a/requests/models.py +++ b/requests/models.py @@ -462,10 +462,10 @@ class Request(object): def register_hook(self, event, hook): """Properly register a hook.""" - if isinstance(hook, (list, tuple, set)): - self.hooks[event].extend(hook) - else: + if callable(hook): self.hooks[event].append(hook) + elif hasattr(hook, '__iter__'): + self.hooks[event].extend(h for h in hook if callable(h)) def deregister_hook(self, event, hook): """Deregister a previously registered hook. diff --git a/tests/test_requests.py b/tests/test_requests.py index 0f67618..3d6f49c 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -778,6 +778,10 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): r = requests.models.Request(hooks={'args': hooks}) assert_hooks_are_callable(r.hooks) + hooks.append('string that should not be registered') + r = requests.models.Request(hooks={'args': hooks}) + assert_hooks_are_callable(r.hooks) + def test_session_persistent_cookies(self): s = requests.session() -- 2.7.4