From 9b1af8452642267ae982853ba331bb8fd8f22320 Mon Sep 17 00:00:00 2001 From: toonn Date: Sun, 28 Oct 2018 13:11:28 +0100 Subject: Put our guesses back in the except clause --- ranger/core/actions.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/ranger/core/actions.py b/ranger/core/actions.py index e4cc1ded..7e5765b0 100644 --- a/ranger/core/actions.py +++ b/ranger/core/actions.py @@ -1170,7 +1170,18 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m try: import chardet except ImportError: - pass + # 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 else: with open(path, 'rb') as fobj: data = fobj.read(count) @@ -1180,18 +1191,6 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m 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: return fobj.read(count) -- cgit 1.4.1-2-gfad0