Skip to content

Commit 66b36af

Browse files
committed
Fix encoding logic/order
- If user provides an encoding value, use it instead of trying to _guess_ first. - If no value is provided, then decode with default of utf-8, otherwise try with unicode-escape
1 parent a6d372d commit 66b36af

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

sqlparse/lexer.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ def get_tokens(text, encoding=None):
4343
if isinstance(text, text_type):
4444
pass
4545
elif isinstance(text, bytes_type):
46-
try:
47-
text = text.decode('utf-8')
48-
except UnicodeDecodeError:
49-
if not encoding:
50-
encoding = 'unicode-escape'
46+
if encoding:
5147
text = text.decode(encoding)
48+
else:
49+
try:
50+
text = text.decode('utf-8')
51+
except UnicodeDecodeError:
52+
text = text.decode('unicode-escape')
5253
else:
5354
raise TypeError(u"Expected text or file-like object, got {!r}".
5455
format(type(text)))

0 commit comments

Comments
 (0)