From 667c21bbed197f39fa263bf95f91f4e9417d68c8 Mon Sep 17 00:00:00 2001 From: beville Date: Wed, 23 Jan 2013 03:04:55 +0000 Subject: [PATCH] More exception handling for corrupt archives git-svn-id: http://comictagger.googlecode.com/svn/trunk@326 6c5673fe-1810-88d6-992b-cd32ca31540c --- comicarchive.py | 27 ++++++++++++++++++--------- imagehasher.py | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/comicarchive.py b/comicarchive.py index 9ddce20..17e8f2b 100644 --- a/comicarchive.py +++ b/comicarchive.py @@ -69,6 +69,8 @@ class ZipArchiver: data = zf.read( archive_file ) except zipfile.BadZipfile: print "bad zipfile: {0} :: {1}".format(self.path, archive_file) + except Exception: + print "bad zipfile: {0} :: {1}".format(self.path, archive_file) finally: zf.close() return data @@ -288,6 +290,9 @@ class RarArchiver: except (OSError, IOError) as e: print e, "in readArchiveFile! try %s" % tries time.sleep(1) + except Exception as e: + print "Unexpected exception in readArchiveFile! {0}".format( e ) + break else: #Success" @@ -295,9 +300,9 @@ class RarArchiver: if (len(entries) == 1): return entries[0][1] else: - return "" + return None - return "" + return None @@ -884,13 +889,17 @@ class ComicArchive: if pil_available: if 'ImageSize' not in p or 'ImageHeight' not in p or 'ImageWidth' not in p: data = self.getPage( idx ) - - im = Image.open(StringIO.StringIO(data)) - w,h = im.size - - p['ImageSize'] = str(len(data)) - p['ImageHeight'] = str(h) - p['ImageWidth'] = str(w) + if data is not None: + try: + im = Image.open(StringIO.StringIO(data)) + w,h = im.size + + p['ImageSize'] = str(len(data)) + p['ImageHeight'] = str(h) + p['ImageWidth'] = str(w) + except IOError: + p['ImageSize'] = str(len(data)) + else: if 'ImageSize' not in p: data = self.getPage( idx ) diff --git a/imagehasher.py b/imagehasher.py index edffba8..79c522b 100755 --- a/imagehasher.py +++ b/imagehasher.py @@ -34,7 +34,7 @@ class ImageHasher(object): image = self.image.resize((self.width, self.height), Image.ANTIALIAS).convert("L") except Exception as e: sys.exc_clear() - print "average_hash:", e.strerror + print "average_hash error:", e return long(0) pixels = list(image.getdata())