Author: lukeplant
Date: 2010-09-10 13:49:45 -0500 (Fri, 10 Sep 2010)
New Revision: 13716
Modified:
django/branches/releases/1.2.X/
django/branches/releases/1.2.X/django/core/files/images.py
django/branches/releases/1.2.X/tests/regressiontests/file_storage/tests.py
Log:
[1.2.X] Fixed #11158 - get_image_dimensions very slow/incorrect after 1 call
Thanks to kua for the report, and to kua, SmileyChris and SAn for the patch
Backport of [13715] from trunk
Property changes on: django/branches/releases/1.2.X
___________________________________________________________________
Name: svnmerge-integrated
-
/django/trunk:1-13360,13434,13480,13574,13600,13638,13652,13664,13666,13668,13680,13683,13685,13687-13688,13690,13694,13696,13701-13702,13705,13709,13712
+
/django/trunk:1-13360,13434,13480,13574,13600,13638,13652,13664,13666,13668,13680,13683,13685,13687-13688,13690,13694,13696,13701-13702,13705,13709,13712,13715
Modified: django/branches/releases/1.2.X/django/core/files/images.py
===================================================================
--- django/branches/releases/1.2.X/django/core/files/images.py 2010-09-10
18:45:25 UTC (rev 13715)
+++ django/branches/releases/1.2.X/django/core/files/images.py 2010-09-10
18:49:45 UTC (rev 13716)
@@ -23,23 +23,26 @@
if not hasattr(self, '_dimensions_cache'):
close = self.closed
self.open()
- self._dimensions_cache = get_image_dimensions(self)
- if close:
- self.close()
+ self._dimensions_cache = get_image_dimensions(self, close=close)
return self._dimensions_cache
-def get_image_dimensions(file_or_path):
- """Returns the (width, height) of an image, given an open file or a
path."""
+def get_image_dimensions(file_or_path, close=False):
+ """
+ Returns the (width, height) of an image, given an open file or a path. Set
+ 'close' to True to close the file at the end if it is initially in an open
+ state.
+ """
# Try to import PIL in either of the two ways it can end up installed.
try:
from PIL import ImageFile as PILImageFile
except ImportError:
import ImageFile as PILImageFile
-
+
p = PILImageFile.Parser()
- close = False
if hasattr(file_or_path, 'read'):
file = file_or_path
+ file_pos = file.tell()
+ file.seek(0)
else:
file = open(file_or_path, 'rb')
close = True
@@ -55,3 +58,5 @@
finally:
if close:
file.close()
+ else:
+ file.seek(file_pos)
Modified:
django/branches/releases/1.2.X/tests/regressiontests/file_storage/tests.py
===================================================================
--- django/branches/releases/1.2.X/tests/regressiontests/file_storage/tests.py
2010-09-10 18:45:25 UTC (rev 13715)
+++ django/branches/releases/1.2.X/tests/regressiontests/file_storage/tests.py
2010-09-10 18:49:45 UTC (rev 13716)
@@ -230,3 +230,19 @@
finally:
del images.open
self.assert_(FileWrapper._closed)
+
+ class InconsistentGetImageDimensionsBug(TestCase):
+ """
+ Test that get_image_dimensions() works properly after various calls
using a file handler (#11158)
+ """
+ def test_multiple_calls(self):
+ """
+ Multiple calls of get_image_dimensions() should return the same
size.
+ """
+ from django.core.files.images import ImageFile
+ img_path = os.path.join(os.path.dirname(__file__), "test.png")
+ image = ImageFile(open(img_path))
+ image_pil = Image.open(img_path)
+ size_1, size_2 = get_image_dimensions(image),
get_image_dimensions(image)
+ self.assertEqual(image_pil.size, size_1)
+ self.assertEqual(size_1, size_2)
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.