russ...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed 5afa746

Fixed #7727 -- Improved the checks for import failure when using PIL. 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.

  • Participants
  • Parent commits e9c55b0

Comments (0)

Files changed (3)

     Stefane Fermgier <>
     Afonso Fernández Nogueira <>
     J. Pablo Fernandez <>
+    Maciej Fijalkowski
     Matthew Flanagan <>
     Eric Floehr <>
     Vincent Foley <>

File django/forms/

             #  but it must be called immediately after the constructor
             trial_image =
+        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(

File tests/modeltests/model_forms/

     description = models.CharField(max_length=20)
         # 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())