From 1ff11304dcebbabdede8ef3d659ca0e54055e2fd Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Sun, 25 May 2014 22:52:47 +0200 Subject: [1.7.x] Fixed #22680 -- I/O operation on closed file. This patch is two-fold; first it ensure that Django does close everything in request.FILES at the end of the request and secondly the storage system should no longer close any files during save, it's up to the caller to handle that -- or let Django close the files at the end of the request. Backport of e2efc8965edf684aaf48621680ef54b84e116576 from master. --- tests/file_storage/tests.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'tests/file_storage') diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index a0e2e2a6cc..b7e4bcdf6d 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -19,7 +19,8 @@ from django.core.cache import cache from django.core.exceptions import SuspiciousOperation from django.core.files.base import File, ContentFile from django.core.files.storage import FileSystemStorage, get_storage_class -from django.core.files.uploadedfile import SimpleUploadedFile +from django.core.files.uploadedfile import (InMemoryUploadedFile, SimpleUploadedFile, + TemporaryUploadedFile) from django.test import LiveServerTestCase, SimpleTestCase from django.test import override_settings from django.utils import six @@ -206,6 +207,23 @@ class FileStorageTests(unittest.TestCase): self.storage.delete('path/to/test.file') + def test_save_doesnt_close(self): + with TemporaryUploadedFile('test', 'text/plain', 1, 'utf8') as file: + file.write(b'1') + file.seek(0) + self.assertFalse(file.closed) + self.storage.save('path/to/test.file', file) + self.assertFalse(file.closed) + self.assertFalse(file.file.closed) + + file = InMemoryUploadedFile(six.StringIO('1'), '', 'test', + 'text/plain', 1, 'utf8') + with file: + self.assertFalse(file.closed) + self.storage.save('path/to/test.file', file) + self.assertFalse(file.closed) + self.assertFalse(file.file.closed) + def test_file_path(self): """ File storage returns the full path of a file -- cgit v1.3