ImageMagick interface: Don't crash when asked to open empty image files

This commit is contained in:
Kovid Goyal 2011-01-03 15:13:05 -07:00
parent 8b03a506a3
commit dd6b0c7efc

View File

@ -106,13 +106,16 @@ class Image(_magick.Image): # {{{
return ans return ans
def load(self, data): 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): def open(self, path_or_file):
if not hasattr(path_or_file, 'read') and \ if not hasattr(path_or_file, 'read') and \
path_or_file.lower().endswith('.wmf'): path_or_file.lower().endswith('.wmf'):
# Special handling for WMF files as ImageMagick seems # 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): if isinstance(path_or_file, unicode):
path_or_file = path_or_file.encode(filesystem_encoding) path_or_file = path_or_file.encode(filesystem_encoding)
return _magick.Image.read(self, path_or_file) return _magick.Image.read(self, path_or_file)
@ -121,6 +124,8 @@ class Image(_magick.Image): # {{{
data = data.read() data = data.read()
else: else:
data = open(data, 'rb').read() data = open(data, 'rb').read()
if not data:
raise ValueError('%r is an empty file'%path_or_file)
self.load(data) self.load(data)
@dynamic_property @dynamic_property