Fixed #14681 -- Do not set mode to None on file-like objects.

gzip.GzipFile does not support files with mode set to None.
This commit is contained in:
Claude Paroz 2012-05-31 09:59:58 +02:00
parent 4b28ea37e5
commit 2626ea4a74
2 changed files with 14 additions and 2 deletions

View File

@ -12,7 +12,8 @@ class File(FileProxyMixin):
if name is None: if name is None:
name = getattr(file, 'name', None) name = getattr(file, 'name', None)
self.name = name self.name = name
self.mode = getattr(file, 'mode', None) if hasattr(file, 'mode'):
self.mode = file.mode
def __str__(self): def __str__(self):
return smart_str(self.name or '') return smart_str(self.name or '')

View File

@ -1,5 +1,6 @@
from __future__ import absolute_import from __future__ import absolute_import
import gzip
import shutil import shutil
import tempfile import tempfile
@ -8,11 +9,12 @@ from django.core.files import File
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.core.files.uploadedfile import SimpleUploadedFile from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase from django.test import TestCase
from django.utils import unittest
from .models import Storage, temp_storage, temp_storage_location from .models import Storage, temp_storage, temp_storage_location
class FileTests(TestCase): class FileStorageTests(TestCase):
def tearDown(self): def tearDown(self):
shutil.rmtree(temp_storage_location) shutil.rmtree(temp_storage_location)
@ -106,6 +108,8 @@ class FileTests(TestCase):
obj3.default.delete() obj3.default.delete()
obj4.random.delete() obj4.random.delete()
class FileTests(unittest.TestCase):
def test_context_manager(self): def test_context_manager(self):
orig_file = tempfile.TemporaryFile() orig_file = tempfile.TemporaryFile()
base_file = File(orig_file) base_file = File(orig_file)
@ -114,3 +118,10 @@ class FileTests(TestCase):
self.assertFalse(f.closed) self.assertFalse(f.closed)
self.assertTrue(f.closed) self.assertTrue(f.closed)
self.assertTrue(orig_file.closed) self.assertTrue(orig_file.closed)
def test_file_mode(self):
# Should not set mode to None if it is not present.
# See #14681, stdlib gzip module crashes if mode is set to None
file = SimpleUploadedFile("mode_test.txt", "content")
self.assertFalse(hasattr(file, 'mode'))
g = gzip.GzipFile(fileobj=file)