From 159d1be6567a3696468b77c64169f4f5a5429c14 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 29 Mar 2012 16:49:59 +0000 Subject: [PATCH] Fixed #11150 -- Removed dependency on cStringIO in ImageField validation. Thanks vvd for the report. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17825 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/fields.py | 4 ++-- tests/modeltests/model_forms/tests.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/django/forms/fields.py b/django/forms/fields.py index 96ecabfdc1..1fa8d57dff 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -577,8 +577,8 @@ class ImageField(FileField): # Since we're about to use the file again we have to reset the # file object if possible. - if hasattr(file, 'reset'): - file.reset() + if hasattr(file, 'seek') and callable(file.seek): + file.seek(0) # verify() is the only method that can spot a corrupt PNG, # but it must be called immediately after the constructor diff --git a/tests/modeltests/model_forms/tests.py b/tests/modeltests/model_forms/tests.py index a2ad024827..ff5048eced 100644 --- a/tests/modeltests/model_forms/tests.py +++ b/tests/modeltests/model_forms/tests.py @@ -1370,6 +1370,19 @@ class OldFormForXTests(TestCase): self.assertEqual(instance.image.name, 'foo/test4.png') instance.delete() + # Test image field when cStringIO is not available + from django.forms import fields + from StringIO import StringIO + old_StringIO = fields.StringIO + fields.StringIO = StringIO + try: + f = ImageFileForm( + data={'description': u'An image'}, + files={'image': SimpleUploadedFile('test.png', image_data)}) + self.assertEqual(f.is_valid(), True) + finally: + fields.StringIO = old_StringIO + def test_media_on_modelform(self): # Similar to a regular Form class you can define custom media to be used on # the ModelForm.