Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 alex authored
6 django/core/files/base.py
View
@@ -101,6 +101,12 @@ def __iter__(self):
if buffer_ is not None:
yield buffer_
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_value, tb):
+ self.close()
+
def open(self, mode=None):
if not self.closed:
self.seek(0)
4 tests/modeltests/files/tests.py
View
@@ -1,4 +1,5 @@
import shutil
+import sys
from django.core.cache import cache
from django.core.files.base import ContentFile
@@ -6,6 +7,8 @@
from django.test import TestCase
from models import Storage, temp_storage, temp_storage_location
+if sys.version_info >= (2, 5):
+ from tests_25 import FileObjTests
class FileTests(TestCase):
@@ -97,4 +100,3 @@ def test_files(self):
obj2.normal.delete()
obj3.default.delete()
obj4.random.delete()
-
17 tests/modeltests/files/tests_25.py
View
@@ -0,0 +1,17 @@
+from __future__ import with_statement
+
+import tempfile
+
+from django.core.files import File
+from django.utils.unittest import TestCase
+
+
+class FileObjTests(TestCase):
+ def test_context_manager(self):
+ orig_file = tempfile.TemporaryFile()
+ base_file = File(orig_file)
+ with base_file as f:
+ self.assertIs(base_file, f)
+ self.assertFalse(f.closed)
+ self.assertTrue(f.closed)
+ self.assertTrue(orig_file.closed)
18 tests/regressiontests/file_storage/tests.py
View
@@ -4,8 +4,17 @@
import sys
import tempfile
import time
-from cStringIO import StringIO
from datetime import datetime, timedelta
+try:
+ from cStringIO import StringIO
+except ImportError:
+ from StringIO import StringIO
+
+try:
+ import threading
+except ImportError:
+ import dummy_threading as threading
+
from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.core.files.base import ContentFile, File
@@ -15,11 +24,6 @@
from django.core.exceptions import ImproperlyConfigured
from django.utils import unittest
-try:
- import threading
-except ImportError:
- import dummy_threading as threading
-
# Try to import PIL in either of the two ways it can end up installed.
# Checking for the existence of Image is enough for CPython, but
# for PyPy, you need to check for the underlying modules
@@ -31,6 +35,7 @@
except ImportError:
Image = None
+
class GetStorageClassTests(unittest.TestCase):
def assertRaisesErrorWithMessage(self, error, message, callable,
*args, **kwargs):
@@ -430,6 +435,7 @@ 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, 'rb'))
image_pil = Image.open(img_path)
2  tests/regressiontests/test_utils/tests.py
View
@@ -4,7 +4,7 @@
if sys.version_info >= (2, 5):
- from python_25 import AssertNumQueriesTests
+ from tests_25 import AssertNumQueriesTests
class SkippingTestCase(TestCase):
0  tests/regressiontests/test_utils/python_25.py → tests/regressiontests/test_utils/tests_25.py
View
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.