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

PR: More accurate test for text-like files #2939

Merged
merged 1 commit into from
Jan 27, 2016
Merged
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
11 changes: 7 additions & 4 deletions spyderlib/utils/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import os
import locale
import sys
from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32
from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32, getincrementaldecoder

# Local imports
from spyderlib.py3compat import (is_string, to_text_string, is_binary_string,
Expand Down Expand Up @@ -243,13 +243,16 @@ def is_text_file(filename):
for bom in [BOM_UTF8, BOM_UTF16, BOM_UTF32]:
if chunk.startswith(bom):
return True
chunk = chunk.decode('utf-8')

decoder = getincrementaldecoder('utf-8')()
while 1:
is_final = len(chunk) < CHUNKSIZE
chunk = decoder.decode(chunk, final=is_final)
if '\0' in chunk: # found null byte
return False
if len(chunk) < CHUNKSIZE:
if is_final:
break # done
chunk = fid.read(CHUNKSIZE).decode('utf-8')
chunk = fid.read(CHUNKSIZE)
except UnicodeDecodeError:
return False
except Exception:
Expand Down