Skip to content

Commit 15ec5d2

Browse files
committed
Update __init__.py
Unicode fix for hmac
1 parent c05dff2 commit 15ec5d2

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

oauth2/__init__.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ def to_unicode(s):
112112
except UnicodeDecodeError as le:
113113
raise TypeError('You are required to pass either a unicode object or a utf-8 string here. You passed a Python string object which contained non-utf-8: %r. The UnicodeDecodeError that resulted from attempting to interpret it as utf-8 was: %s' % (s, le,))
114114
except NameError:
115-
pass
115+
if not isinstance(s, str):
116+
raise TypeError('You are required to pass either unicode or string here, not: %r (%s)' % (type(s), s))
117+
try:
118+
s = s.decode('utf-8')
119+
except UnicodeDecodeError as le:
120+
raise TypeError('You are required to pass either a unicode object or a utf-8 string here. You passed a Python string object which contained non-utf-8: %r. The UnicodeDecodeError that resulted from attempting to interpret it as utf-8 was: %s' % (s, le,))
116121
return s
117122

118123
def to_utf8(s):
@@ -125,7 +130,10 @@ def to_unicode_if_string(s):
125130
else:
126131
return s
127132
except NameError:
128-
return s
133+
if isinstance(s, str):
134+
return to_unicode(s)
135+
else:
136+
return s
129137

130138
def to_utf8_if_string(s):
131139
try:
@@ -134,7 +142,11 @@ def to_utf8_if_string(s):
134142
else:
135143
return s
136144
except NameError:
137-
return s
145+
if isinstance(s, str):
146+
return to_utf8(s)
147+
else:
148+
return s
149+
except NameError:
138150

139151
def to_unicode_optional_iterator(x):
140152
"""
@@ -146,7 +158,7 @@ def to_unicode_optional_iterator(x):
146158
return to_unicode(x)
147159
except NameError:
148160
if isinstance(x, str):
149-
return x
161+
return to_unicode(x)
150162

151163
try:
152164
l = list(x)
@@ -166,7 +178,7 @@ def to_utf8_optional_iterator(x):
166178
return to_utf8(x)
167179
except NameError:
168180
if isinstance(x, str):
169-
return x
181+
return to_utf8(x)
170182

171183
try:
172184
l = list(x)

0 commit comments

Comments
 (0)