diff --git a/django/core/files/base.py b/django/core/files/base.py
index 04853fa..a1b0948 100644
a
|
b
|
class File(FileProxyMixin):
|
14 | 14 | if name is None: |
15 | 15 | name = getattr(file, 'name', None) |
16 | 16 | self.name = name |
17 | | if hasattr(file, 'mode'): |
18 | | self.mode = file.mode |
19 | 17 | |
20 | 18 | def __str__(self): |
21 | 19 | return smart_str(self.name or '') |
… |
… |
class File(FileProxyMixin):
|
56 | 54 | return not self.file or self.file.closed |
57 | 55 | closed = property(_get_closed) |
58 | 56 | |
| 57 | @property |
| 58 | def mode(self): |
| 59 | if hasattr(self.file, 'mode'): |
| 60 | return self.file.mode |
| 61 | raise AttributeError("'%s' object has no attribute 'mode'" % self.__class__.__name__) |
| 62 | |
59 | 63 | def chunks(self, chunk_size=None): |
60 | 64 | """ |
61 | 65 | Read the file and yield chucks of ``chunk_size`` bytes (defaults to |
… |
… |
class File(FileProxyMixin):
|
113 | 117 | self.close() |
114 | 118 | |
115 | 119 | def open(self, mode=None): |
| 120 | if not self.closed and mode is not None and mode != self.mode: |
| 121 | # To be reopened in another mode |
| 122 | self.close() |
116 | 123 | if not self.closed: |
117 | 124 | self.seek(0) |
118 | 125 | elif self.name and os.path.exists(self.name): |
diff --git a/tests/modeltests/files/tests.py b/tests/modeltests/files/tests.py
index 3e256f7..6177d0a 100644
a
|
b
|
class FileStorageTests(TestCase):
|
49 | 49 | sorted(files), ["assignment.txt", "default.txt", "django_test.txt"] |
50 | 50 | ) |
51 | 51 | |
| 52 | # Test that the file can be opened in writable mode |
| 53 | obj1.normal.open('rb+') |
| 54 | self.assertEqual(obj1.normal.file.mode, 'rb+') |
| 55 | obj1.normal.write(b"content") |
| 56 | obj1.normal.close() |
| 57 | |
52 | 58 | # Files can be read in a little at a time, if necessary. |
53 | 59 | obj1.normal.open() |
54 | 60 | self.assertEqual(obj1.normal.read(3), b"con") |