Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the '__iter__' attribute to determine if something is iterable vs a volatile error message #120

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions oauth2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@

try:
from hashlib import sha1
sha = sha1
sha_new = sha = sha1
except ImportError:
# hashlib was added in Python 2.5
import sha
import sha as sha_new
from sha import sha

import _version

Expand Down Expand Up @@ -129,13 +130,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):
"""
Expand All @@ -145,13 +143,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 /."""
Expand Down Expand Up @@ -458,13 +453,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]
Expand Down Expand Up @@ -837,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]
Expand Down