Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14749 -- added support for using Django's file object as conte…

…xt managers. Thanks to Florian Apolloner for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14671 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f5f18a38ab990910ca43448212c70938992c9787 1 parent 73cd9b6
Alex Gaynor authored November 21, 2010
6  django/core/files/base.py
@@ -101,6 +101,12 @@ def __iter__(self):
101 101
         if buffer_ is not None:
102 102
             yield buffer_
103 103
 
  104
+    def __enter__(self):
  105
+        return self
  106
+
  107
+    def __exit__(self, exc_type, exc_value, tb):
  108
+        self.close()
  109
+
104 110
     def open(self, mode=None):
105 111
         if not self.closed:
106 112
             self.seek(0)
4  tests/modeltests/files/tests.py
... ...
@@ -1,4 +1,5 @@
1 1
 import shutil
  2
+import sys
2 3
 
3 4
 from django.core.cache import cache
4 5
 from django.core.files.base import ContentFile
@@ -6,6 +7,8 @@
6 7
 from django.test import TestCase
7 8
 
8 9
 from models import Storage, temp_storage, temp_storage_location
  10
+if sys.version_info >= (2, 5):
  11
+    from tests_25 import FileObjTests
9 12
 
10 13
 
11 14
 class FileTests(TestCase):
@@ -97,4 +100,3 @@ def test_files(self):
97 100
         obj2.normal.delete()
98 101
         obj3.default.delete()
99 102
         obj4.random.delete()
100  
-
17  tests/modeltests/files/tests_25.py
... ...
@@ -0,0 +1,17 @@
  1
+from __future__ import with_statement
  2
+
  3
+import tempfile
  4
+
  5
+from django.core.files import File
  6
+from django.utils.unittest import TestCase
  7
+
  8
+
  9
+class FileObjTests(TestCase):
  10
+    def test_context_manager(self):
  11
+        orig_file = tempfile.TemporaryFile()
  12
+        base_file = File(orig_file)
  13
+        with base_file as f:
  14
+            self.assertIs(base_file, f)
  15
+            self.assertFalse(f.closed)
  16
+        self.assertTrue(f.closed)
  17
+        self.assertTrue(orig_file.closed)
18  tests/regressiontests/file_storage/tests.py
@@ -4,8 +4,17 @@
4 4
 import sys
5 5
 import tempfile
6 6
 import time
7  
-from cStringIO import StringIO
8 7
 from datetime import datetime, timedelta
  8
+try:
  9
+    from cStringIO import StringIO
  10
+except ImportError:
  11
+    from StringIO import StringIO
  12
+
  13
+try:
  14
+    import threading
  15
+except ImportError:
  16
+    import dummy_threading as threading
  17
+
9 18
 from django.conf import settings
10 19
 from django.core.exceptions import SuspiciousOperation
11 20
 from django.core.files.base import ContentFile, File
@@ -15,11 +24,6 @@
15 24
 from django.core.exceptions import ImproperlyConfigured
16 25
 from django.utils import unittest
17 26
 
18  
-try:
19  
-    import threading
20  
-except ImportError:
21  
-    import dummy_threading as threading
22  
-
23 27
 # Try to import PIL in either of the two ways it can end up installed.
24 28
 # Checking for the existence of Image is enough for CPython, but
25 29
 # for PyPy, you need to check for the underlying modules
@@ -31,6 +35,7 @@
31 35
     except ImportError:
32 36
         Image = None
33 37
 
  38
+
34 39
 class GetStorageClassTests(unittest.TestCase):
35 40
     def assertRaisesErrorWithMessage(self, error, message, callable,
36 41
         *args, **kwargs):
@@ -430,6 +435,7 @@ def test_multiple_calls(self):
430 435
         Multiple calls of get_image_dimensions() should return the same size.
431 436
         """
432 437
         from django.core.files.images import ImageFile
  438
+
433 439
         img_path = os.path.join(os.path.dirname(__file__), "test.png")
434 440
         image = ImageFile(open(img_path, 'rb'))
435 441
         image_pil = Image.open(img_path)
2  tests/regressiontests/test_utils/tests.py
@@ -4,7 +4,7 @@
4 4
 
5 5
 
6 6
 if sys.version_info >= (2, 5):
7  
-    from python_25 import AssertNumQueriesTests
  7
+    from tests_25 import AssertNumQueriesTests
8 8
 
9 9
 
10 10
 class SkippingTestCase(TestCase):
0  tests/regressiontests/test_utils/python_25.py → tests/regressiontests/test_utils/tests_25.py
File renamed without changes

0 notes on commit f5f18a3

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