From: Ian Cordasco Date: Mon, 1 Oct 2012 17:09:02 +0000 (-0400) Subject: Only register callable items in lists X-Git-Tag: v0.14.2~17^2^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=77677eb71d41f20d657254b7d88c17337369e600;p=services%2Fpython-requests.git 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. --- 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()