summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSamuel Walladge <samuel@swalladge.id.au>2017-02-07 14:08:15 +1030
committernfnty <git@nfnty.se>2017-02-07 06:08:41 +0100
commit9e2e196b1c090dcfe179132693f68f9638bc1c6b (patch)
tree51d855f44965dca0d3cf43aae45b2060b62cce73
parent91427884dd691794f3d382bddbf95207d734e4d6 (diff)
downloadranger-9e2e196b1c090dcfe179132693f68f9638bc1c6b.tar.gz
container.file.File: Fix `is_binary`
Open file in binary mode to avoid decoding errors.
Fix string handling in Python 2 vs 3.

Fixes #161
-rw-r--r--ranger/container/file.py26
1 files changed, 13 insertions, 13 deletions
diff --git a/ranger/container/file.py b/ranger/container/file.py
index efb2a7d8..17ca577c 100644
--- a/ranger/container/file.py
+++ b/ranger/container/file.py
@@ -4,12 +4,14 @@
 from __future__ import (absolute_import, division, print_function)
 
 import re
+import sys
+
 from ranger.container.fsobject import FileSystemObject
 
 N_FIRST_BYTES = 256
-# pylint: disable=invalid-name
-control_characters = set(chr(n) for n in set(range(0, 9)) | set(range(14, 32)))
-# pylint: enable=invalid-name
+CONTROL_CHARACTERS = set(list(range(0, 9)) + list(range(14, 32)))
+if sys.version_info[0] < 3:
+    CONTROL_CHARACTERS = set(chr(n) for n in CONTROL_CHARACTERS)
 
 # Don't even try to preview files which match this regular expression:
 PREVIEW_BLACKLIST = re.compile(r"""
@@ -51,19 +53,17 @@ class File(FileSystemObject):
 
     @property
     def firstbytes(self):
-        if self._firstbytes is None:
-            try:
-                with open(self.path, 'r') as fobj:
-                    try:
-                        self._firstbytes = fobj.read(N_FIRST_BYTES)
-                    except UnicodeDecodeError:
-                        return None
-            except OSError:
-                return None
+        if self._firstbytes is not None:
+            return self._firstbytes
+        try:
+            with open(self.path, 'rb') as fobj:
+                self._firstbytes = set(fobj.read(N_FIRST_BYTES))
+        except OSError:
+            return None
         return self._firstbytes
 
     def is_binary(self):
-        if self.firstbytes and control_characters & set(self.firstbytes):
+        if self.firstbytes and CONTROL_CHARACTERS & self.firstbytes:
             return True
         return False