Skip to content

Commit 6ffe6f3

Browse files
author
Jeff Hammel
committed
a better way of detecting if things are iterable
1 parent a83f4a2 commit 6ffe6f3

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

oauth2/__init__.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,10 @@ def to_unicode_optional_iterator(x):
129129
if isinstance(x, basestring):
130130
return to_unicode(x)
131131

132-
try:
133-
l = list(x)
134-
except TypeError, e:
135-
assert 'is not iterable' in str(e)
132+
if not hasattr(x, '__iter__'):
136133
return x
137-
else:
138-
return [ to_unicode(e) for e in l ]
134+
135+
return [ to_unicode(e) for e in list(x) ]
139136

140137
def to_utf8_optional_iterator(x):
141138
"""
@@ -145,13 +142,10 @@ def to_utf8_optional_iterator(x):
145142
if isinstance(x, basestring):
146143
return to_utf8(x)
147144

148-
try:
149-
l = list(x)
150-
except TypeError, e:
151-
assert 'is not iterable' in str(e)
145+
if not hasattr(x, '__iter__'):
152146
return x
153-
else:
154-
return [ to_utf8_if_string(e) for e in l ]
147+
148+
return [ to_utf8_if_string(e) for e in list(x) ]
155149

156150
def escape(s):
157151
"""Escape a URL including any /."""

0 commit comments

Comments
 (0)