python - How verify function implemented? -
i want find how verify() function pillow library implemented. in source code found this:
def verify(self): """ verifies contents of file. data read file, method attempts determine if file broken, without decoding image data. if method finds problems, raises suitable exceptions. if need load image after using method, must reopen image file. """ pass where can find implementation?
(source code found here: pillow source code )
a comment on github explains:
image.[v]erify checks chunk checksums in png files, , no-op elsewhere.
so short answer did find default implementation absolutely nothing.
except png files, can find implementation in pngimagefile.verify method:
def verify(self): "verify png file" if self.fp none: raise runtimeerror("verify must called directly after open") # beginning of idat block self.fp.seek(self.tile[0][2] - 8) self.png.verify() self.png.close() self.fp = none which in turn through self.png.verify() calls chunkstream.verify:
def verify(self, endchunk=b"iend"): # simple approach; calculate checksum remaining # blocks. must 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 more detailed source code breakdown
your quoted code verify method of image class shows default nothing:
class image: ... def verify(self): """ verifies contents of file. data read file, method attempts determine if file broken, without decoding image data. if method finds problems, raises suitable exceptions. if need load image after using method, must reopen image file. """ pass but in case of png files default verify method overridden, seen source code imagefile class, inherits image class:
class imagefile(image.image): "base class image file format handlers." ... and source code png plugin class pngimagefile inherits imagefile:
## # image plugin png images. class pngimagefile(imagefile.imagefile): ... and has overridden implementation of verify:
def verify(self): "verify png file" if self.fp none: raise runtimeerror("verify must called directly after open") # beginning of idat block self.fp.seek(self.tile[0][2] - 8) self.png.verify() self.png.close() self.fp = none which in turn through self.png.verify() calls chunkstream.verify:
def verify(self, endchunk=b"iend"): # simple approach; calculate checksum remaining # blocks. must 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 through pngstream class doesn't override verify:
class pngstream(chunkstream): ...
Comments
Post a Comment