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