From dd6b0c7efc5bfd6b3f94857180a25896502366a8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 3 Jan 2011 15:13:05 -0700 Subject: [PATCH] ImageMagick interface: Don't crash when asked to open empty image files --- src/calibre/utils/magick/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/calibre/utils/magick/__init__.py b/src/calibre/utils/magick/__init__.py index bf0f48db7d..834a798de5 100644 --- a/src/calibre/utils/magick/__init__.py +++ b/src/calibre/utils/magick/__init__.py @@ -106,13 +106,16 @@ class Image(_magick.Image): # {{{ return ans def load(self, data): - return _magick.Image.load(self, bytes(data)) + data = bytes(data) + if not data: + raise ValueError('Cannot open image from empty data string') + return _magick.Image.load(self, data) def open(self, path_or_file): if not hasattr(path_or_file, 'read') and \ path_or_file.lower().endswith('.wmf'): # Special handling for WMF files as ImageMagick seems - # to hand while reading them from a blob on linux + # to hang while reading them from a blob on linux if isinstance(path_or_file, unicode): path_or_file = path_or_file.encode(filesystem_encoding) return _magick.Image.read(self, path_or_file) @@ -121,6 +124,8 @@ class Image(_magick.Image): # {{{ data = data.read() else: data = open(data, 'rb').read() + if not data: + raise ValueError('%r is an empty file'%path_or_file) self.load(data) @dynamic_property