Skip to content

Commit 86d7a21

Browse files
committed
Fix errors with get_coding and no-uft-8 files
1 parent 2bb47cf commit 86d7a21

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

Diff for: spyder/utils/encoding.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,18 @@ def get_coding(text):
107107
@return coding string
108108
"""
109109
for line in text.splitlines()[:2]:
110-
result = CODING_RE.search(to_text_string(line))
111-
if result:
112-
codec = result.group(1)
113-
# sometimes we find a false encoding that can result in errors
114-
if codec in CODECS:
115-
return codec
110+
try:
111+
result = CODING_RE.search(to_text_string(line))
112+
except UnicodeDecodeError:
113+
# This could fail because to_text_string assume the text is utf8-like
114+
# and we don't know the encoding to give it to to_text_string
115+
pass
116+
else:
117+
if result:
118+
codec = result.group(1)
119+
# sometimes we find a false encoding that can result in errors
120+
if codec in CODECS:
121+
return codec
116122

117123
# Fallback using chardet
118124
if is_binary_string(text):

0 commit comments

Comments
 (0)