From 2626ea4a74d09642a50c3fb30944513b68dda289 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 31 May 2012 09:59:58 +0200 Subject: [PATCH] Fixed #14681 -- Do not set mode to None on file-like objects. gzip.GzipFile does not support files with mode set to None. --- django/core/files/base.py | 3 ++- tests/modeltests/files/tests.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/django/core/files/base.py b/django/core/files/base.py index ad0922b88b..a2d703c963 100644 --- a/django/core/files/base.py +++ b/django/core/files/base.py @@ -12,7 +12,8 @@ class File(FileProxyMixin): if name is None: name = getattr(file, 'name', None) self.name = name - self.mode = getattr(file, 'mode', None) + if hasattr(file, 'mode'): + self.mode = file.mode def __str__(self): return smart_str(self.name or '') diff --git a/tests/modeltests/files/tests.py b/tests/modeltests/files/tests.py index 3bfe193610..837723a049 100644 --- a/tests/modeltests/files/tests.py +++ b/tests/modeltests/files/tests.py @@ -1,5 +1,6 @@ from __future__ import absolute_import +import gzip import shutil import tempfile @@ -8,11 +9,12 @@ from django.core.files import File from django.core.files.base import ContentFile from django.core.files.uploadedfile import SimpleUploadedFile from django.test import TestCase +from django.utils import unittest from .models import Storage, temp_storage, temp_storage_location -class FileTests(TestCase): +class FileStorageTests(TestCase): def tearDown(self): shutil.rmtree(temp_storage_location) @@ -106,6 +108,8 @@ class FileTests(TestCase): obj3.default.delete() obj4.random.delete() + +class FileTests(unittest.TestCase): def test_context_manager(self): orig_file = tempfile.TemporaryFile() base_file = File(orig_file) @@ -114,3 +118,10 @@ class FileTests(TestCase): self.assertFalse(f.closed) self.assertTrue(f.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)