Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

gzip.GzipFile does not support files with mode set to None.
  • Loading branch information...
commit 2626ea4a74d09642a50c3fb30944513b68dda289 1 parent 4b28ea3
@claudep claudep authored
Showing with 14 additions and 2 deletions.
  1. +2 −1  django/core/files/base.py
  2. +12 −1 tests/modeltests/files/tests.py
View
3  django/core/files/base.py
@@ -12,7 +12,8 @@ def __init__(self, file, name=None):
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 '')
View
13 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.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 @@ def test_files(self):
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 @@ def test_context_manager(self):
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)
Please sign in to comment.
Something went wrong with that request. Please try again.