diff --git a/AUTHORS b/AUTHORS index 24e3e5bcac..d7945d4a1a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -150,6 +150,7 @@ answer newbie questions, and generally made Django that much better: Stefane Fermgier Afonso Fernández Nogueira J. Pablo Fernandez + Maciej Fijalkowski Matthew Flanagan Eric Floehr Vincent Foley diff --git a/django/forms/fields.py b/django/forms/fields.py index 9df8955392..134b63a625 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -503,6 +503,11 @@ class ImageField(FileField): # but it must be called immediately after the constructor trial_image = Image.open(file) trial_image.verify() + except ImportError: + # Under PyPy, it is possible to import PIL. However, the underlying + # _imaging C module isn't available, so an ImportError will be + # raised. Catch and re-raise. + raise except Exception: # Python Imaging Library doesn't recognize it as an image raise ValidationError(self.error_messages['invalid_image']) if hasattr(f, 'seek') and callable(f.seek): diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index cc9efd0f94..be2a8ba835 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -69,8 +69,10 @@ class ImageFile(models.Model): description = models.CharField(max_length=20) try: # If PIL is available, try testing PIL. - # Otherwise, it's equivalent to TextFile above. - import Image + # Checking for the existence of Image is enough for CPython, but + # for PyPy, you need to check for the underlying modules + # If PIL is not available, this test is equivalent to TextFile above. + import Image, _imaging image = models.ImageField(upload_to=tempfile.gettempdir()) except ImportError: image = models.FileField(upload_to=tempfile.gettempdir())