How verify function implemented?
<p>I want to find how <code>verify()</code> function from <code>Pillow</code> library implemented. In a source code i found only this:</p>

<pre><code>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
</code></pre>

<p>Where i can find implementation?</p>

<p>(source code i found here: <a href="https://pillow.readthedocs.io/en/3.0.0/_modules/PIL/Image.html#Image.verify" rel="nofollow">Pillow source code</a> )</p>

<p>A <a href="https://github.com/python-pillow/Pillow/issues/1408#issuecomment-139024840" rel="nofollow">comment on GitHub</a> explains:</p>

<blockquote>
  <p>Image.[v]erify only checks the chunk checksums in png files, and is a no-op elsewhere.</p>
</blockquote>

<p>So the short answer is that you already did find the default implementation which does absolutely nothing.</p>

<p><strong>Except for PNG files</strong>, for which you can find the implementation in the <a href="https://pillow.readthedocs.io/en/3.0.0/_modules/PIL/PngImagePlugin.html#PngImageFile.verify" rel="nofollow"><code>PngImageFile.verify</code></a> method:</p>

<pre class="lang-python prettyprint-override"><code>    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
</code></pre>

<p>which in turn through <code>self.png.verify()</code> calls <a href="https://pillow.readthedocs.io/en/3.0.0/_modules/PIL/PngImagePlugin.html#ChunkStream.verify" rel="nofollow"><code>ChunkStream.verify</code></a>:</p>

<pre class="lang-python prettyprint-override"><code>    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
</code></pre>

<h2>More detailed source code breakdown</h2>

<p>Your already quoted code for the <a href="https://pillow.readthedocs.io/en/3.0.0/_modules/PIL/Image.html#Image.verify" rel="nofollow"><code>verify</code></a> method of the <a href="https://pillow.readthedocs.io/en/3.0.0/_modules/PIL/Image.html#Image" rel="nofollow"><code>Image</code></a> class shows that it by default does nothing:</p>

<pre class="lang-python prettyprint-override"><code>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
</code></pre>

<p>But in the case of PNG files the default <code>verify</code> method is overridden, as seen from the source code for the <a href="https://pillow.readthedocs.io/en/3.0.0/_modules/PIL/ImageFile.html" rel="nofollow"><code>ImageFile</code></a> class, which inherits from the <code>Image</code> class:</p>

<pre class="lang-python prettyprint-override"><code>class ImageFile(Image.Image):
    "Base class for image file format handlers."

    ...
</code></pre>

<p>and the source code for the PNG plugin class <a href="https://pillow.readthedocs.io/en/3.0.0/_modules/PIL/PngImagePlugin.html#PngImageFile" rel="nofollow"><code>PngImageFile</code></a> which inherits from <code>ImageFile</code>:</p>

<pre class="lang-python prettyprint-override"><code>##
# Image plugin for PNG images.

class PngImageFile(ImageFile.ImageFile):

    ...
</code></pre>

<p>and has this overridden implementation of <a href="https://pillow.readthedocs.io/en/3.0.0/_modules/PIL/PngImagePlugin.html#PngImageFile.verify" rel="nofollow"><code>verify</code></a>:</p>

<pre class="lang-python prettyprint-override"><code>    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
</code></pre>

<p>which in turn through <code>self.png.verify()</code> calls <a href="https://pillow.readthedocs.io/en/3.0.0/_modules/PIL/PngImagePlugin.html#ChunkStream.verify" rel="nofollow"><code>ChunkStream.verify</code></a>:</p>

<pre class="lang-python prettyprint-override"><code>    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
</code></pre>

<p>through the <a href="https://pillow.readthedocs.io/en/3.0.0/_modules/PIL/PngImagePlugin.html#PngStream" rel="nofollow"><code>PngStream</code></a> class which doesn't override <code>verify</code>:</p>

<pre class="lang-python prettyprint-override"><code>class PngStream(ChunkStream):

    ...
</code></pre>

python pillow