From 6ffe6f312dca283779527c430320717170979864 Mon Sep 17 00:00:00 2001 From: Jeff Hammel Date: Wed, 19 Sep 2012 09:44:42 -0700 Subject: [PATCH 1/4] a better way of detecting if things are iterable --- oauth2/__init__.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index 835270e3..a4890271 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -129,13 +129,10 @@ def to_unicode_optional_iterator(x): if isinstance(x, basestring): return to_unicode(x) - try: - l = list(x) - except TypeError, e: - assert 'is not iterable' in str(e) + if not hasattr(x, '__iter__'): return x - else: - return [ to_unicode(e) for e in l ] + + return [ to_unicode(e) for e in list(x) ] def to_utf8_optional_iterator(x): """ @@ -145,13 +142,10 @@ def to_utf8_optional_iterator(x): if isinstance(x, basestring): return to_utf8(x) - try: - l = list(x) - except TypeError, e: - assert 'is not iterable' in str(e) + if not hasattr(x, '__iter__'): return x - else: - return [ to_utf8_if_string(e) for e in l ] + + return [ to_utf8_if_string(e) for e in list(x) ] def escape(s): """Escape a URL including any /.""" From 7cce98a46a7f30c884ca31ea9b36685821aaec65 Mon Sep 17 00:00:00 2001 From: Jeff Hammel Date: Wed, 19 Sep 2012 10:37:01 -0700 Subject: [PATCH 2/4] replaced another way of checking for iterable --- oauth2/__init__.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index a4890271..a33875dc 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -452,13 +452,10 @@ def get_normalized_parameters(self): if isinstance(value, basestring): items.append((to_utf8_if_string(key), to_utf8(value))) else: - try: - value = list(value) - except TypeError, e: - assert 'is not iterable' in str(e) - items.append((to_utf8_if_string(key), to_utf8_if_string(value))) + if hasattr(value, '__iter__'): + items.extend((to_utf8_if_string(key), to_utf8_if_string(item)) for item in list(value)) else: - items.extend((to_utf8_if_string(key), to_utf8_if_string(item)) for item in value) + items.append((to_utf8_if_string(key), to_utf8_if_string(value))) # Include any query string parameters from the provided URL query = urlparse.urlparse(self.url)[4] From 57003712cd2955f812d7ab46d3531376c26cc9ed Mon Sep 17 00:00:00 2001 From: Jeff Hammel Date: Wed, 19 Sep 2012 10:46:58 -0700 Subject: [PATCH 3/4] import the sha function from the sha module, not just the module --- oauth2/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index a33875dc..79a65645 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -43,7 +43,7 @@ sha = sha1 except ImportError: # hashlib was added in Python 2.5 - import sha + from sha import sha import _version From dd34c70b0cc8249cb5f7837540ad07001ae858e8 Mon Sep 17 00:00:00 2001 From: Jeff Hammel Date: Wed, 19 Sep 2012 11:51:49 -0700 Subject: [PATCH 4/4] follow up to fix for how hmac works in 2.4 --- oauth2/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index 79a65645..c42aee10 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -40,9 +40,10 @@ try: from hashlib import sha1 - sha = sha1 + sha_new = sha = sha1 except ImportError: # hashlib was added in Python 2.5 + import sha as sha_new from sha import sha import _version @@ -828,7 +829,7 @@ def sign(self, request, consumer, token): """Builds the base signature string.""" key, raw = self.signing_base(request, consumer, token) - hashed = hmac.new(key, raw, sha) + hashed = hmac.new(key, raw, sha_new) # Calculate the digest base 64. return binascii.b2a_base64(hashed.digest())[:-1]