Fixed #29188 -- Fixed ContentFile.size after a write().

This commit is contained in:
Alex Stovbur 2018-03-07 23:20:25 +02:00 committed by Tim Graham
parent 277ed07209
commit 2d9ec4d735
2 changed files with 14 additions and 0 deletions

View File

@ -149,6 +149,10 @@ class ContentFile(File):
def close(self): def close(self):
pass pass
def write(self, data):
self.__dict__.pop('_size', None) # Clear the computed size.
return self.file.write(data)
def endswith_cr(line): def endswith_cr(line):
"""Return True if line (a text or byte string) ends with '\r'.""" """Return True if line (a text or byte string) ends with '\r'."""

View File

@ -206,6 +206,16 @@ class ContentFileTestCase(unittest.TestCase):
with file.open() as f: with file.open() as f:
self.assertEqual(f.read(), b'content') self.assertEqual(f.read(), b'content')
def test_size_changing_after_writing(self):
"""ContentFile.size changes after a write()."""
f = ContentFile('')
self.assertEqual(f.size, 0)
f.write('Test ')
f.write('string')
self.assertEqual(f.size, 11)
with f.open() as fh:
self.assertEqual(fh.read(), 'Test string')
class InMemoryUploadedFileTests(unittest.TestCase): class InMemoryUploadedFileTests(unittest.TestCase):
def test_open_resets_file_to_start_and_returns_context_manager(self): def test_open_resets_file_to_start_and_returns_context_manager(self):