mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
ImageMagick interface: Don't crash when asked to open empty image files
This commit is contained in:
parent
8b03a506a3
commit
dd6b0c7efc
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user