验证功能如何实现?
How verify function implemented?
我想了解 Pillow
库中的 verify()
函数是如何实现的。在源代码中我只找到了这个:
def verify(self):
"""
Verifies the contents of a file. For data read from a file, this
method attempts to determine if the file is broken, without
actually decoding the image data. If this method finds any
problems, it raises suitable exceptions. If you need to load
the image after using this method, you must reopen the image
file.
"""
pass
在哪里可以找到实现?
(我在这里找到的源代码:Pillow source code)
一个comment on GitHub说明:
Image.[v]erify only checks the chunk checksums in png files, and is a no-op elsewhere.
所以简短的回答是您已经找到了绝对不执行任何操作的默认实现。
除了 PNG 文件,您可以在 PngImageFile.verify
方法中找到实现:
def verify(self):
"Verify PNG file"
if self.fp is None:
raise RuntimeError("verify must be called directly after open")
# back up to beginning of IDAT block
self.fp.seek(self.tile[0][2] - 8)
self.png.verify()
self.png.close()
self.fp = None
反过来通过 self.png.verify()
调用 ChunkStream.verify
:
def verify(self, endchunk=b"IEND"):
# Simple approach; just calculate checksum for all remaining
# blocks. Must be called directly after open.
cids = []
while True:
cid, pos, length = self.read()
if cid == endchunk:
break
self.crc(cid, ImageFile._safe_read(self.fp, length))
cids.append(cid)
return cids
更详细的源代码分解
您已经为 verify
method of the Image
class 引用的代码表明它在默认情况下不执行任何操作:
class Image:
...
def verify(self):
"""
Verifies the contents of a file. For data read from a file, this
method attempts to determine if the file is broken, without
actually decoding the image data. If this method finds any
problems, it raises suitable exceptions. If you need to load
the image after using this method, you must reopen the image
file.
"""
pass
但是对于 PNG 文件,默认的 verify
方法被覆盖,从 ImageFile
class 的源代码可以看出,它继承自 Image
class:
class ImageFile(Image.Image):
"Base class for image file format handlers."
...
以及 PNG 插件的源代码 class PngImageFile
继承自 ImageFile
:
##
# Image plugin for PNG images.
class PngImageFile(ImageFile.ImageFile):
...
并具有 verify
的重写实现:
def verify(self):
"Verify PNG file"
if self.fp is None:
raise RuntimeError("verify must be called directly after open")
# back up to beginning of IDAT block
self.fp.seek(self.tile[0][2] - 8)
self.png.verify()
self.png.close()
self.fp = None
反过来通过 self.png.verify()
调用 ChunkStream.verify
:
def verify(self, endchunk=b"IEND"):
# Simple approach; just calculate checksum for all remaining
# blocks. Must be called directly after open.
cids = []
while True:
cid, pos, length = self.read()
if cid == endchunk:
break
self.crc(cid, ImageFile._safe_read(self.fp, length))
cids.append(cid)
return cids
通过 PngStream
class 不会覆盖 verify
:
class PngStream(ChunkStream):
...
我想了解 Pillow
库中的 verify()
函数是如何实现的。在源代码中我只找到了这个:
def verify(self):
"""
Verifies the contents of a file. For data read from a file, this
method attempts to determine if the file is broken, without
actually decoding the image data. If this method finds any
problems, it raises suitable exceptions. If you need to load
the image after using this method, you must reopen the image
file.
"""
pass
在哪里可以找到实现?
(我在这里找到的源代码:Pillow source code)
一个comment on GitHub说明:
Image.[v]erify only checks the chunk checksums in png files, and is a no-op elsewhere.
所以简短的回答是您已经找到了绝对不执行任何操作的默认实现。
除了 PNG 文件,您可以在 PngImageFile.verify
方法中找到实现:
def verify(self):
"Verify PNG file"
if self.fp is None:
raise RuntimeError("verify must be called directly after open")
# back up to beginning of IDAT block
self.fp.seek(self.tile[0][2] - 8)
self.png.verify()
self.png.close()
self.fp = None
反过来通过 self.png.verify()
调用 ChunkStream.verify
:
def verify(self, endchunk=b"IEND"):
# Simple approach; just calculate checksum for all remaining
# blocks. Must be called directly after open.
cids = []
while True:
cid, pos, length = self.read()
if cid == endchunk:
break
self.crc(cid, ImageFile._safe_read(self.fp, length))
cids.append(cid)
return cids
更详细的源代码分解
您已经为 verify
method of the Image
class 引用的代码表明它在默认情况下不执行任何操作:
class Image:
...
def verify(self):
"""
Verifies the contents of a file. For data read from a file, this
method attempts to determine if the file is broken, without
actually decoding the image data. If this method finds any
problems, it raises suitable exceptions. If you need to load
the image after using this method, you must reopen the image
file.
"""
pass
但是对于 PNG 文件,默认的 verify
方法被覆盖,从 ImageFile
class 的源代码可以看出,它继承自 Image
class:
class ImageFile(Image.Image):
"Base class for image file format handlers."
...
以及 PNG 插件的源代码 class PngImageFile
继承自 ImageFile
:
##
# Image plugin for PNG images.
class PngImageFile(ImageFile.ImageFile):
...
并具有 verify
的重写实现:
def verify(self):
"Verify PNG file"
if self.fp is None:
raise RuntimeError("verify must be called directly after open")
# back up to beginning of IDAT block
self.fp.seek(self.tile[0][2] - 8)
self.png.verify()
self.png.close()
self.fp = None
反过来通过 self.png.verify()
调用 ChunkStream.verify
:
def verify(self, endchunk=b"IEND"):
# Simple approach; just calculate checksum for all remaining
# blocks. Must be called directly after open.
cids = []
while True:
cid, pos, length = self.read()
if cid == endchunk:
break
self.crc(cid, ImageFile._safe_read(self.fp, length))
cids.append(cid)
return cids
通过 PngStream
class 不会覆盖 verify
:
class PngStream(ChunkStream):
...