From 43c71f7a938556337bad3ed2ad4b277e730fb969 Mon Sep 17 00:00:00 2001 From: Jon Anderson Date: Sun, 21 Jan 2018 01:19:41 -0800 Subject: Added except catching for Python2. When reading a file for which Ranger doesn't have permissions, Python3 throws an OSError. Python2 on the other hand throws a IOError, which wasn't being caught and caused Ranger to crash. IOError was added to catch statements for this reason. --- ranger/container/file.py | 3 ++- ranger/core/actions.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ranger/container/file.py b/ranger/container/file.py index 17ca577c..83942671 100644 --- a/ranger/container/file.py +++ b/ranger/container/file.py @@ -58,7 +58,8 @@ class File(FileSystemObject): try: with open(self.path, 'rb') as fobj: self._firstbytes = set(fobj.read(N_FIRST_BYTES)) - except OSError: + # IOError for Python2, OSError for Python3 + except (IOError, OSError): return None return self._firstbytes diff --git a/ranger/core/actions.py b/ranger/core/actions.py index 0298af19..aef0d205 100644 --- a/ranger/core/actions.py +++ b/ranger/core/actions.py @@ -978,7 +978,8 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m if not self.settings.preview_script or not self.settings.use_preview_script: try: return codecs.open(path, 'r', errors='ignore') - except OSError: + # IOError for Python2, OSError for Python3 + except (IOError, OSError): return None # self.previews is a 2 dimensional dict: -- cgit 1.4.1-2-gfad0 From 623e603481b2516e48c9949218e115968f42c37c Mon Sep 17 00:00:00 2001 From: Jon Anderson Date: Sat, 27 Jan 2018 15:40:33 -0800 Subject: Added more IOError checks for Python2. --- ranger/config/commands.py | 17 +++++++++++++++++ ranger/ext/img_display.py | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/ranger/config/commands.py b/ranger/config/commands.py index 299f17c2..f11e0e83 100755 --- a/ranger/config/commands.py +++ b/ranger/config/commands.py @@ -776,14 +776,23 @@ class load_copy_buffer(Command): copy_buffer_filename = 'copy_buffer' def execute(self): + import sys from ranger.container.file import File from os.path import exists fname = self.fm.datapath(self.copy_buffer_filename) + fobj = None try: fobj = open(fname, 'r') except OSError: + if sys.version_info[0] < 3: + raise + except IOError: + if sys.version_info[0] >= 3: + raise + if fobj is None: return self.fm.notify( "Cannot open %s" % (fname or self.copy_buffer_filename), bad=True) + self.fm.copy_buffer = set(File(g) for g in fobj.read().split("\n") if exists(g)) fobj.close() @@ -799,11 +808,19 @@ class save_copy_buffer(Command): copy_buffer_filename = 'copy_buffer' def execute(self): + import sys fname = None fname = self.fm.datapath(self.copy_buffer_filename) + fobj = None try: fobj = open(fname, 'w') except OSError: + if sys.version_info[0] < 3: + raise + except IOError: + if sys.version_info[0] >= 3: + raise + if fobj is None: return self.fm.notify("Cannot open %s" % (fname or self.copy_buffer_filename), bad=True) fobj.write("\n".join(fobj.path for fobj in self.fm.copy_buffer)) diff --git a/ranger/ext/img_display.py b/ranger/ext/img_display.py index c55d835e..e4740407 100644 --- a/ranger/ext/img_display.py +++ b/ranger/ext/img_display.py @@ -306,6 +306,13 @@ class ITerm2ImageDisplayer(ImageDisplayer, FileManagerAware): file_handle.seek(1, 1) height, width = struct.unpack('>HH', file_handle.read(4)) except OSError: + if sys.version_info[0] < 3: + raise + file_handle.close() + return 0, 0 + except IOError: + if sys.version_info[0] >= 3: + raise file_handle.close() return 0, 0 else: -- cgit 1.4.1-2-gfad0 From 8e24016afb0c12cfb181dbec11ccbaa5986cd9e9 Mon Sep 17 00:00:00 2001 From: Jon Anderson Date: Sun, 21 Jan 2018 01:19:41 -0800 Subject: Added except catching for Python2. When reading a file for which Ranger doesn't have permissions, Python3 throws an OSError. Python2 on the other hand throws a IOError, which wasn't being caught and caused Ranger to crash. IOError was added to catch statements for this reason. --- ranger/container/file.py | 3 ++- ranger/core/actions.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ranger/container/file.py b/ranger/container/file.py index d2daa169..6450cfe6 100644 --- a/ranger/container/file.py +++ b/ranger/container/file.py @@ -57,7 +57,8 @@ class File(FileSystemObject): try: with open(self.path, 'rb') as fobj: self._firstbytes = set(fobj.read(N_FIRST_BYTES)) - except OSError: + # IOError for Python2, OSError for Python3 + except (IOError, OSError): return None return self._firstbytes diff --git a/ranger/core/actions.py b/ranger/core/actions.py index 0298af19..aef0d205 100644 --- a/ranger/core/actions.py +++ b/ranger/core/actions.py @@ -978,7 +978,8 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m if not self.settings.preview_script or not self.settings.use_preview_script: try: return codecs.open(path, 'r', errors='ignore') - except OSError: + # IOError for Python2, OSError for Python3 + except (IOError, OSError): return None # self.previews is a 2 dimensional dict: -- cgit 1.4.1-2-gfad0 From 65b8a3817fe988bc00f530d6859dbb39f1ce99eb Mon Sep 17 00:00:00 2001 From: Jon Anderson Date: Sat, 27 Jan 2018 15:40:33 -0800 Subject: Added more IOError checks for Python2. --- ranger/config/commands.py | 17 +++++++++++++++++ ranger/ext/img_display.py | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/ranger/config/commands.py b/ranger/config/commands.py index d426b72e..3d5340dd 100755 --- a/ranger/config/commands.py +++ b/ranger/config/commands.py @@ -785,14 +785,23 @@ class load_copy_buffer(Command): copy_buffer_filename = 'copy_buffer' def execute(self): + import sys from ranger.container.file import File from os.path import exists fname = self.fm.datapath(self.copy_buffer_filename) + fobj = None try: fobj = open(fname, 'r') except OSError: + if sys.version_info[0] < 3: + raise + except IOError: + if sys.version_info[0] >= 3: + raise + if fobj is None: return self.fm.notify( "Cannot open %s" % (fname or self.copy_buffer_filename), bad=True) + self.fm.copy_buffer = set(File(g) for g in fobj.read().split("\n") if exists(g)) fobj.close() @@ -808,11 +817,19 @@ class save_copy_buffer(Command): copy_buffer_filename = 'copy_buffer' def execute(self): + import sys fname = None fname = self.fm.datapath(self.copy_buffer_filename) + fobj = None try: fobj = open(fname, 'w') except OSError: + if sys.version_info[0] < 3: + raise + except IOError: + if sys.version_info[0] >= 3: + raise + if fobj is None: return self.fm.notify("Cannot open %s" % (fname or self.copy_buffer_filename), bad=True) fobj.write("\n".join(fobj.path for fobj in self.fm.copy_buffer)) diff --git a/ranger/ext/img_display.py b/ranger/ext/img_display.py index 01c739d3..0bc9e96e 100644 --- a/ranger/ext/img_display.py +++ b/ranger/ext/img_display.py @@ -305,6 +305,13 @@ class ITerm2ImageDisplayer(ImageDisplayer, FileManagerAware): file_handle.seek(1, 1) height, width = struct.unpack('>HH', file_handle.read(4)) except OSError: + if sys.version_info[0] < 3: + raise + file_handle.close() + return 0, 0 + except IOError: + if sys.version_info[0] >= 3: + raise file_handle.close() return 0, 0 else: -- cgit 1.4.1-2-gfad0 From 8083eb60c4193ffd34a2e5ade9b364e6669e5d7b Mon Sep 17 00:00:00 2001 From: hut Date: Sun, 28 Jan 2018 19:12:36 +0100 Subject: ext.img_display: cleaner catching of OSError/IOError --- ranger/config/commands.py | 20 ++++---------------- ranger/ext/img_display.py | 13 +++---------- 2 files changed, 7 insertions(+), 26 deletions(-) diff --git a/ranger/config/commands.py b/ranger/config/commands.py index 3d5340dd..a3837d8e 100755 --- a/ranger/config/commands.py +++ b/ranger/config/commands.py @@ -789,16 +789,10 @@ class load_copy_buffer(Command): from ranger.container.file import File from os.path import exists fname = self.fm.datapath(self.copy_buffer_filename) - fobj = None + unreadable = IOError if sys.version_info[0] < 3 else OSError try: fobj = open(fname, 'r') - except OSError: - if sys.version_info[0] < 3: - raise - except IOError: - if sys.version_info[0] >= 3: - raise - if fobj is None: + except unreadable: return self.fm.notify( "Cannot open %s" % (fname or self.copy_buffer_filename), bad=True) @@ -820,16 +814,10 @@ class save_copy_buffer(Command): import sys fname = None fname = self.fm.datapath(self.copy_buffer_filename) - fobj = None + unwritable = IOError if sys.version_info[0] < 3 else OSError try: fobj = open(fname, 'w') - except OSError: - if sys.version_info[0] < 3: - raise - except IOError: - if sys.version_info[0] >= 3: - raise - if fobj is None: + except unwritable: return self.fm.notify("Cannot open %s" % (fname or self.copy_buffer_filename), bad=True) fobj.write("\n".join(fobj.path for fobj in self.fm.copy_buffer)) diff --git a/ranger/ext/img_display.py b/ranger/ext/img_display.py index 0bc9e96e..67941e27 100644 --- a/ranger/ext/img_display.py +++ b/ranger/ext/img_display.py @@ -291,6 +291,7 @@ class ITerm2ImageDisplayer(ImageDisplayer, FileManagerAware): elif image_type == 'gif': width, height = struct.unpack('H', file_handle.read(2))[0] - 2 file_handle.seek(1, 1) height, width = struct.unpack('>HH', file_handle.read(4)) - except OSError: - if sys.version_info[0] < 3: - raise - file_handle.close() - return 0, 0 - except IOError: - if sys.version_info[0] >= 3: - raise - file_handle.close() - return 0, 0 + except unreadable: + height, width = 0, 0 else: file_handle.close() return 0, 0 -- cgit 1.4.1-2-gfad0