Fixed #14749 -- added support for using Django's file object as context managers. Thanks to Florian Apolloner for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14671 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
73cd9b61c9
commit
f5f18a38ab
|
@ -101,6 +101,12 @@ class File(FileProxyMixin):
|
||||||
if buffer_ is not None:
|
if buffer_ is not None:
|
||||||
yield buffer_
|
yield buffer_
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, tb):
|
||||||
|
self.close()
|
||||||
|
|
||||||
def open(self, mode=None):
|
def open(self, mode=None):
|
||||||
if not self.closed:
|
if not self.closed:
|
||||||
self.seek(0)
|
self.seek(0)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import shutil
|
import shutil
|
||||||
|
import sys
|
||||||
|
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
|
@ -6,6 +7,8 @@ from django.core.files.uploadedfile import SimpleUploadedFile
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from models import Storage, temp_storage, temp_storage_location
|
from models import Storage, temp_storage, temp_storage_location
|
||||||
|
if sys.version_info >= (2, 5):
|
||||||
|
from tests_25 import FileObjTests
|
||||||
|
|
||||||
|
|
||||||
class FileTests(TestCase):
|
class FileTests(TestCase):
|
||||||
|
@ -97,4 +100,3 @@ class FileTests(TestCase):
|
||||||
obj2.normal.delete()
|
obj2.normal.delete()
|
||||||
obj3.default.delete()
|
obj3.default.delete()
|
||||||
obj4.random.delete()
|
obj4.random.delete()
|
||||||
|
|
||||||
|
|
|
@ -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)
|
|
@ -4,8 +4,17 @@ import shutil
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import time
|
import time
|
||||||
from cStringIO import StringIO
|
|
||||||
from datetime import datetime, timedelta
|
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.conf import settings
|
||||||
from django.core.exceptions import SuspiciousOperation
|
from django.core.exceptions import SuspiciousOperation
|
||||||
from django.core.files.base import ContentFile, File
|
from django.core.files.base import ContentFile, File
|
||||||
|
@ -15,11 +24,6 @@ from django.core.files.uploadedfile import UploadedFile
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.utils import unittest
|
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.
|
# 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
|
# Checking for the existence of Image is enough for CPython, but
|
||||||
# for PyPy, you need to check for the underlying modules
|
# for PyPy, you need to check for the underlying modules
|
||||||
|
@ -31,6 +35,7 @@ except ImportError:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
Image = None
|
Image = None
|
||||||
|
|
||||||
|
|
||||||
class GetStorageClassTests(unittest.TestCase):
|
class GetStorageClassTests(unittest.TestCase):
|
||||||
def assertRaisesErrorWithMessage(self, error, message, callable,
|
def assertRaisesErrorWithMessage(self, error, message, callable,
|
||||||
*args, **kwargs):
|
*args, **kwargs):
|
||||||
|
@ -430,6 +435,7 @@ class InconsistentGetImageDimensionsBug(unittest.TestCase):
|
||||||
Multiple calls of get_image_dimensions() should return the same size.
|
Multiple calls of get_image_dimensions() should return the same size.
|
||||||
"""
|
"""
|
||||||
from django.core.files.images import ImageFile
|
from django.core.files.images import ImageFile
|
||||||
|
|
||||||
img_path = os.path.join(os.path.dirname(__file__), "test.png")
|
img_path = os.path.join(os.path.dirname(__file__), "test.png")
|
||||||
image = ImageFile(open(img_path, 'rb'))
|
image = ImageFile(open(img_path, 'rb'))
|
||||||
image_pil = Image.open(img_path)
|
image_pil = Image.open(img_path)
|
||||||
|
|
|
@ -4,7 +4,7 @@ from django.test import TestCase, skipUnlessDBFeature, skipIfDBFeature
|
||||||
|
|
||||||
|
|
||||||
if sys.version_info >= (2, 5):
|
if sys.version_info >= (2, 5):
|
||||||
from python_25 import AssertNumQueriesTests
|
from tests_25 import AssertNumQueriesTests
|
||||||
|
|
||||||
|
|
||||||
class SkippingTestCase(TestCase):
|
class SkippingTestCase(TestCase):
|
||||||
|
|
Loading…
Reference in New Issue