PIL fails when trying to parse a streaming png file

gordallott avatargordallott created an issue

PIL 1.1.6-3ubuntu1 fails when operating on an png file from its parser() method with

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 961, in histogram
    self.load()
  File "/usr/lib/python2.6/dist-packages/PIL/ImageFile.py", line 189, in load
    s = read(self.decodermaxblock)
  File "/usr/lib/python2.6/dist-packages/PIL/PngImagePlugin.py", line 365, in load_read
    return self.fp.read(bytes)
  File "/usr/lib/python2.6/dist-packages/PIL/ImageFile.py", line 300, in read
    data = self.data[pos:pos+bytes]
TypeError: 'NoneType' object is unsubscriptable

this python script will expose the bug:

import PIL.ImageFile
imgfile = open('foo.png')
p = PIL.ImageFile.Parser()

while 1:
    data = imgfile.read(1024)
    if not data:
        break
    p.feed(data)

img = p.close()
img.histogram() # simple operation on PIL.Image object, should work but fails on png files

Attached is a patch that fixes this, a lack of any bug tracker/development contact for python-imaging upstream necessitates it being here.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.