Skip to content
Browse files

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.
  • Loading branch information...
1 parent 7c520ec commit 77677eb71d41f20d657254b7d88c17337369e600 @sigmavirus24 sigmavirus24 committed
Showing with 7 additions and 3 deletions.
  1. +3 −3 requests/models.py
  2. +4 −0 tests/test_requests.py
View
6 requests/models.py
@@ -462,10 +462,10 @@ def path_url(self):
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.
View
4 tests/test_requests.py
@@ -778,6 +778,10 @@ def assert_hooks_are_callable(hooks):
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()

0 comments on commit 77677eb

Please sign in to comment.
Something went wrong with that request. Please try again.