diff options
-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: |