Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#11840 closed (worksforme)

Form validation should check for IOErrors

Reported by: Stavros Korokithakis Owned by: nobody
Component: Forms Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no


Forms currently break on various types of images when PIL raises an IOError exception. This should be handled as a form error instead of crashing Django. A traceback is below:

File "main/", line 244, in ad_create_category

if form.is_valid() and image_formset.is_valid():

File "lib/python2.6/site-packages/django/forms/", line 237, in is_valid

if bool(self.errors[i]):

File "lib/python2.6/site-packages/django/forms/", line 211, in _get_errors


File "lib/python2.6/site-packages/django/forms/", line 250, in full_clean


File "lib/python2.6/site-packages/django/forms/", line 111, in _get_errors


File "lib/python2.6/site-packages/django/forms/", line 238, in full_clean

value = field.clean(value, initial)

File "lib/python2.6/site-packages/django/forms/", line 511, in clean


File "lib/python2.6/site-packages/PIL/", line 155, in load


File "lib/python2.6/site-packages/PIL/", line 337, in load_prepare

raise IOError("cannot read interlaced PNG files")

Change History (4)

comment:1 Changed 11 years ago by Honza Král

Component: UncategorizedForms
Needs tests: set
Resolution: worksforme
Status: newclosed

can you provide a test for that? or just a file that results in this error? Looking through the code that shouldn't happen:

        except Exception: # Python Imaging Library doesn't recognize it as an image
            raise ValidationError(self.error_messages['invalid_image'])

I am closing it as works for me, feel free to reopen it if you find a file that results in this error. Thanks!

comment:2 Changed 11 years ago by Stavros Korokithakis

Sadly, I've searched high and low for such a file, but none of my users has responded. Looking at your code it is indeed miraculous that this happens, but looking at mine elucidates the situation:

    def clean(self, data, initial=None):
        # load() is the only method that can spot a truncated JPEG,
        #  but it cannot be called sanely after verify()
        trial_image =

There's no exception block to be found there. I assume it was added after 1.1?

comment:3 Changed 11 years ago by Honza Král

No, the try: except: block is present in the 1.1 tarball, just checked just to be sure.

comment:4 Changed 11 years ago by Stavros Korokithakis

That's odd, I checked my home install and it has it, but both production and staging don't, and django.VERSION reports 1.1.0 there. This is positively strange, but I upgraded and everything is fine now, thank you.

Note: See TracTickets for help on using tickets.
Back to Top