diff options
author | Baranovskiy Konstantin <baranovskiykonstantin@gmail.com> | 2018-10-24 18:11:27 +0300 |
---|---|---|
committer | Baranovskiy Konstantin <baranovskiykonstantin@gmail.com> | 2018-10-26 20:22:40 +0300 |
commit | ec17480833b7c3d968e73a72394c826823dbdfe9 (patch) | |
tree | 73f99d70c0589b4f22f2ce79a6a4c977cd4c03e9 /ranger | |
parent | e9ef0d9517bb3b29596f4a8105908e1c428e06fd (diff) | |
download | ranger-ec17480833b7c3d968e73a72394c826823dbdfe9.tar.gz |
File encoding guessing order changed.
Fixes #1350
Diffstat (limited to 'ranger')
-rw-r--r-- | ranger/core/actions.py | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/ranger/core/actions.py b/ranger/core/actions.py index 20a180e9..e4cc1ded 100644 --- a/ranger/core/actions.py +++ b/ranger/core/actions.py @@ -1170,24 +1170,27 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m try: import chardet except ImportError: - # Guess encoding ourselves. These should be the most frequently used ones. - encodings = ('utf-8', 'utf-16') - for encoding in encodings: - try: - with codecs.open(path, 'r', encoding=encoding) as fobj: - text = fobj.read(count) - except UnicodeDecodeError: - pass - else: - LOG.debug("guessed encoding of '%s' as %r", path, encoding) - return text + pass else: with open(path, 'rb') as fobj: data = fobj.read(count) result = chardet.detect(data) - LOG.debug("chardet guess for '%s': %s", path, result) guessed_encoding = result['encoding'] - return codecs.decode(data, guessed_encoding, 'replace') + if guessed_encoding is not None: + LOG.debug("chardet guess for '%s': %s", path, result) + return codecs.decode(data, guessed_encoding, 'replace') + + # Guess encoding ourselves. These should be the most frequently used ones. + encodings = ('utf-8', 'utf-16') + for encoding in encodings: + try: + with codecs.open(path, 'r', encoding=encoding) as fobj: + text = fobj.read(count) + except UnicodeDecodeError: + pass + else: + LOG.debug("guessed encoding of '%s' as %r", path, encoding) + return text # latin-1 as the last resort with codecs.open(path, 'r', encoding='latin-1', errors='replace') as fobj: |