diff --git a/django/core/files/base.py b/django/core/files/base.py
index 6204d71..fadbd36 100644
a
|
b
|
class File(FileProxyMixin):
|
15 | 15 | if name is None: |
16 | 16 | name = getattr(file, 'name', None) |
17 | 17 | self.name = name |
18 | | self.mode = getattr(file, 'mode', None) |
| 18 | if hasattr(file, 'mode'): |
| 19 | self.mode = getattr(file, 'mode') |
19 | 20 | |
20 | 21 | def __str__(self): |
21 | 22 | return smart_str(self.name or '') |
diff --git a/tests/modeltests/files/tests.py b/tests/modeltests/files/tests.py
index 885f787..72a3577 100644
a
|
b
|
from __future__ import with_statement
|
3 | 3 | import shutil |
4 | 4 | import sys |
5 | 5 | import tempfile |
| 6 | import gzip |
6 | 7 | |
7 | 8 | from django.core.cache import cache |
8 | 9 | from django.core.files import File |
… |
… |
class FileTests(TestCase):
|
101 | 102 | obj4.random.save("random_file", ContentFile("random content")) |
102 | 103 | self.assertTrue(obj4.random.name.endswith("/random_file")) |
103 | 104 | |
| 105 | # should not set mode to None if it is not present |
| 106 | file = SimpleUploadedFile("mode_test.txt", "content") |
| 107 | self.assertFalse(hasattr(file, 'mode')) |
| 108 | |
| 109 | # Verify fix for #14681, File like objects should not explicitly set |
| 110 | # mode=None for compatibility with stdlib gzip |
| 111 | file = SimpleUploadedFile("gzip_test.txt", "content") |
| 112 | self.assertFalse(hasattr(file, 'mode')) |
| 113 | g = gzip.GzipFile(fileobj=file) |
| 114 | |
104 | 115 | # Clean up the temporary files and dir. |
105 | 116 | obj1.normal.delete() |
106 | 117 | obj2.normal.delete() |