mirror of
https://github.com/django/django.git
synced 2024-12-23 09:36:06 +00:00
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:
|
||||
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)
|
||||
|
@ -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.core.files.uploadedfile import SimpleUploadedFile
|
||||
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 @@ class FileTests(TestCase):
|
||||
obj2.normal.delete()
|
||||
obj3.default.delete()
|
||||
obj4.random.delete()
|
||||
|
||||
|
17
tests/modeltests/files/tests_25.py
Normal file
17
tests/modeltests/files/tests_25.py
Normal file
@ -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 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.files.uploadedfile import UploadedFile
|
||||
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:
|
||||
except ImportError:
|
||||
Image = None
|
||||
|
||||
|
||||
class GetStorageClassTests(unittest.TestCase):
|
||||
def assertRaisesErrorWithMessage(self, error, message, callable,
|
||||
*args, **kwargs):
|
||||
@ -430,6 +435,7 @@ class InconsistentGetImageDimensionsBug(unittest.TestCase):
|
||||
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)
|
||||
|
@ -4,7 +4,7 @@ from django.test import TestCase, skipUnlessDBFeature, skipIfDBFeature
|
||||
|
||||
|
||||
if sys.version_info >= (2, 5):
|
||||
from python_25 import AssertNumQueriesTests
|
||||
from tests_25 import AssertNumQueriesTests
|
||||
|
||||
|
||||
class SkippingTestCase(TestCase):
|
||||
|
Loading…
Reference in New Issue
Block a user