Skip to content

Commit

Permalink
Fixed #7727 -- Improved the checks for import failure when using PIL.…
Browse files Browse the repository at this point in the history
… Under PyPy, you can import the PIL module, but when you try to use it, the underlying _imaging module will not be available. Thanks to Maciej Fijalkowski (fijal) for the report and suggested fix.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8016 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Jul 21, 2008
1 parent bfcecbf commit 4016d52
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -150,6 +150,7 @@ answer newbie questions, and generally made Django that much better:
Stefane Fermgier <sf@fermigier.com> Stefane Fermgier <sf@fermigier.com>
Afonso Fernández Nogueira <fonzzo.django@gmail.com> Afonso Fernández Nogueira <fonzzo.django@gmail.com>
J. Pablo Fernandez <pupeno@pupeno.com> J. Pablo Fernandez <pupeno@pupeno.com>
Maciej Fijalkowski
Matthew Flanagan <http://wadofstuff.blogspot.com> Matthew Flanagan <http://wadofstuff.blogspot.com>
Eric Floehr <eric@intellovations.com> Eric Floehr <eric@intellovations.com>
Vincent Foley <vfoleybourgon@yahoo.ca> Vincent Foley <vfoleybourgon@yahoo.ca>
Expand Down
5 changes: 5 additions & 0 deletions django/forms/fields.py
Expand Up @@ -503,6 +503,11 @@ def clean(self, data, initial=None):
# but it must be called immediately after the constructor # but it must be called immediately after the constructor
trial_image = Image.open(file) trial_image = Image.open(file)
trial_image.verify() 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 except Exception: # Python Imaging Library doesn't recognize it as an image
raise ValidationError(self.error_messages['invalid_image']) raise ValidationError(self.error_messages['invalid_image'])
if hasattr(f, 'seek') and callable(f.seek): if hasattr(f, 'seek') and callable(f.seek):
Expand Down
6 changes: 4 additions & 2 deletions tests/modeltests/model_forms/models.py
Expand Up @@ -69,8 +69,10 @@ class ImageFile(models.Model):
description = models.CharField(max_length=20) description = models.CharField(max_length=20)
try: try:
# If PIL is available, try testing PIL. # If PIL is available, try testing PIL.
# Otherwise, it's equivalent to TextFile above. # Checking for the existence of Image is enough for CPython, but
import Image # 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()) image = models.ImageField(upload_to=tempfile.gettempdir())
except ImportError: except ImportError:
image = models.FileField(upload_to=tempfile.gettempdir()) image = models.FileField(upload_to=tempfile.gettempdir())
Expand Down

0 comments on commit 4016d52

Please sign in to comment.