1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Fixed #27777 -- Made File.open() work with the with statement (#8310)

Fixed #27777 -- Made File.open() work with the with statement
This commit is contained in:
Ingo Klöcker
2017-04-07 14:21:06 +02:00
committed by Florian Apolloner
parent 695d4dd790
commit c4536c4a54
6 changed files with 43 additions and 1 deletions

View File

@@ -10,7 +10,9 @@ from django.core.files import File
from django.core.files.base import ContentFile
from django.core.files.move import file_move_safe
from django.core.files.temp import NamedTemporaryFile
from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile
from django.core.files.uploadedfile import (
InMemoryUploadedFile, SimpleUploadedFile, UploadedFile,
)
try:
from PIL import Image
@@ -38,6 +40,23 @@ class FileTests(unittest.TestCase):
self.assertTrue(f.closed)
self.assertTrue(orig_file.closed)
def test_open_resets_opened_file_to_start_and_returns_context_manager(self):
file = File(BytesIO(b'content'))
file.read()
with file.open() as f:
self.assertEqual(f.read(), b'content')
def test_open_reopens_closed_file_and_returns_context_manager(self):
temporary_file = tempfile.NamedTemporaryFile(delete=False)
file = File(temporary_file)
try:
file.close()
with file.open() as f:
self.assertFalse(f.closed)
finally:
# remove temporary file
os.unlink(file.name)
def test_namedtemporaryfile_closes(self):
"""
The symbol django.core.files.NamedTemporaryFile is assigned as
@@ -178,6 +197,21 @@ class ContentFileTestCase(unittest.TestCase):
self.assertIsInstance(ContentFile(b"content").read(), bytes)
self.assertIsInstance(ContentFile("español").read(), str)
def test_open_resets_file_to_start_and_returns_context_manager(self):
file = ContentFile(b'content')
with file.open() as f:
self.assertEqual(f.read(), b'content')
with file.open() as f:
self.assertEqual(f.read(), b'content')
class InMemoryUploadedFileTests(unittest.TestCase):
def test_open_resets_file_to_start_and_returns_context_manager(self):
uf = InMemoryUploadedFile(StringIO('1'), '', 'test', 'text/plain', 1, 'utf8')
uf.read()
with uf.open() as f:
self.assertEqual(f.read(), '1')
class DimensionClosingBug(unittest.TestCase):
"""