Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6054: work around PIL's installation brokeness by detecting ei…

…ther of the two ways it can end up being installed.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12429 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 757898162670b1a3178af3a0dc9fa2921c48702c 1 parent e6740cb
Jacob Kaplan-Moss authored February 14, 2010
7  django/core/files/images.py
@@ -30,7 +30,12 @@ def _get_image_dimensions(self):
30 30
 
31 31
 def get_image_dimensions(file_or_path):
32 32
     """Returns the (width, height) of an image, given an open file or a path."""
33  
-    from PIL import ImageFile as PILImageFile
  33
+    # Try to import PIL in either of the two ways it can end up installed.
  34
+    try:
  35
+        from PIL import ImageFile as PILImageFile
  36
+    except ImportError:
  37
+        import ImageFile as PILImageFile
  38
+        
34 39
     p = PILImageFile.Parser()
35 40
     close = False
36 41
     if hasattr(file_or_path, 'read'):
6  django/core/management/validation.py
@@ -47,10 +47,14 @@ def get_validation_errors(outfile, app=None):
47 47
             if isinstance(f, models.FileField) and not f.upload_to:
48 48
                 e.add(opts, '"%s": FileFields require an "upload_to" attribute.' % f.name)
49 49
             if isinstance(f, models.ImageField):
  50
+                # Try to import PIL in either of the two ways it can end up installed.
50 51
                 try:
51 52
                     from PIL import Image
52 53
                 except ImportError:
53  
-                    e.add(opts, '"%s": To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .' % f.name)
  54
+                    try:
  55
+                        import Image
  56
+                    except ImportError:
  57
+                        e.add(opts, '"%s": To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .' % f.name)
54 58
             if isinstance(f, models.BooleanField) and getattr(f, 'null', False):
55 59
                 e.add(opts, '"%s": BooleanFields do not accept null values. Use a NullBooleanField instead.' % f.name)
56 60
             if f.choices:
7  django/forms/fields.py
@@ -467,7 +467,12 @@ def to_python(self, data):
467 467
         f = super(ImageField, self).to_python(data)
468 468
         if f is None:
469 469
             return None
470  
-        from PIL import Image
  470
+            
  471
+        # Try to import PIL in either of the two ways it can end up installed.
  472
+        try:
  473
+            from PIL import Image
  474
+        except ImportError:
  475
+            import Image
471 476
 
472 477
         # We need to get a file object for PIL. We might have a path or we might
473 478
         # have to read the data into memory.
14  tests/modeltests/model_forms/models.py
@@ -93,11 +93,15 @@ def __unicode__(self):
93 93
         return self.description
94 94
 
95 95
 try:
96  
-    # If PIL is available, try testing ImageFields.
97  
-    # Checking for the existence of Image is enough for CPython, but
98  
-    # for PyPy, you need to check for the underlying modules
99  
-    # If PIL is not available, ImageField tests are omitted.
100  
-    from PIL import Image, _imaging
  96
+    # If PIL is available, try testing ImageFields. Checking for the existence
  97
+    # of Image is enough for CPython, but for PyPy, you need to check for the
  98
+    # underlying modules If PIL is not available, ImageField tests are omitted.
  99
+    # Try to import PIL in either of the two ways it can end up installed.
  100
+    try:
  101
+        from PIL import Image, _imaging
  102
+    except ImportError:
  103
+        import Image, _imaging
  104
+    
101 105
     test_images = True
102 106
 
103 107
     class ImageFile(models.Model):
10  tests/regressiontests/file_storage/tests.py
@@ -18,12 +18,16 @@
18 18
 except ImportError:
19 19
     import dummy_threading as threading
20 20
 
  21
+# Try to import PIL in either of the two ways it can end up installed.
  22
+# Checking for the existence of Image is enough for CPython, but
  23
+# for PyPy, you need to check for the underlying modules
21 24
 try:
22  
-    # Checking for the existence of Image is enough for CPython, but
23  
-    # for PyPy, you need to check for the underlying modules
24 25
     from PIL import Image, _imaging
25 26
 except ImportError:
26  
-    Image = None
  27
+    try:
  28
+        import Image, _imaging
  29
+    except ImportError:
  30
+        Image = None
27 31
 
28 32
 class FileStorageTests(unittest.TestCase):
29 33
     storage_class = FileSystemStorage
11  tests/regressiontests/model_fields/models.py
@@ -6,12 +6,17 @@
6 6
 except ImportError:
7 7
     from django.utils import _decimal as decimal    # Python 2.3 fallback
8 8
 
  9
+# Try to import PIL in either of the two ways it can end up installed.
  10
+# Checking for the existence of Image is enough for CPython, but for PyPy,
  11
+# you need to check for the underlying modules.
  12
+
9 13
 try:
10  
-    # Checking for the existence of Image is enough for CPython, but for PyPy,
11  
-    # you need to check for the underlying modules.
12 14
     from PIL import Image, _imaging
13 15
 except ImportError:
14  
-    Image = None
  16
+    try:
  17
+        import Image, _imaging
  18
+    except ImportError:
  19
+        Image = None
15 20
 
16 21
 from django.core.files.storage import FileSystemStorage
17 22
 from django.db import models

0 notes on commit 7578981

Please sign in to comment.
Something went wrong with that request. Please try again.