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.
This commit is contained in:
Ian Cordasco
2012-10-01 13:09:02 -04:00
committed by Radu Voicilas
parent 46fd297c3a
commit 4e6cf21d82
2 changed files with 7 additions and 3 deletions
+3 -3
View File
@@ -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.
+4
View File
@@ -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()