From 58485621a2295bff0545b9257de171ddea85509b Mon Sep 17 00:00:00 2001 From: Hans Lawrenz Date: Fri, 21 Mar 2014 10:57:18 -0400 Subject: [PATCH 1/2] Fixed SpooledTemporaryFile bug in File class. Added condition to prevent checking the existence of a file name of a file like object when the name attribute is None. This is necessary because a SpooledTemporaryFile won't exist on the file system or have a name until it has reached its max_size. Also added tests. --- django/core/files/base.py | 2 +- tests/files/tests.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/django/core/files/base.py b/django/core/files/base.py index 641ff924c6b0c..85b3c21cbae21 100644 --- a/django/core/files/base.py +++ b/django/core/files/base.py @@ -40,7 +40,7 @@ def _get_size(self): if not hasattr(self, '_size'): if hasattr(self.file, 'size'): self._size = self.file.size - elif hasattr(self.file, 'name') and os.path.exists(self.file.name): + elif hasattr(self.file, 'name') and self.file.name is not None and os.path.exists(self.file.name): self._size = os.path.getsize(self.file.name) elif hasattr(self.file, 'tell') and hasattr(self.file, 'seek'): pos = self.file.tell() diff --git a/tests/files/tests.py b/tests/files/tests.py index b9f8b5228ad00..2865a7385d5cb 100644 --- a/tests/files/tests.py +++ b/tests/files/tests.py @@ -205,3 +205,17 @@ def test_file_move_overwrite(self): os.close(handle_a) os.close(handle_b) + + +class SpooledTempTests(unittest.TestCase): + def test_in_memory_spooled_temp(self): + with tempfile.SpooledTemporaryFile() as temp: + temp.write("foo bar baz quux\n") + django_file = File(temp, name="something.txt") + self.assertTrue(django_file.size, 17) + + def test_written_spooled_temp(self): + with tempfile.SpooledTemporaryFile(max_size=4) as temp: + temp.write("foo bar baz quux\n") + django_file = File(temp, name="something.txt") + self.assertTrue(django_file.size, 17) From 0ff75972df5c518c8ef5c801fc79b4c07259d531 Mon Sep 17 00:00:00 2001 From: Hans Lawrenz Date: Fri, 21 Mar 2014 16:15:05 -0400 Subject: [PATCH 2/2] Corrected errors in tests. --- tests/files/tests.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/files/tests.py b/tests/files/tests.py index 2865a7385d5cb..f22ebbb67daac 100644 --- a/tests/files/tests.py +++ b/tests/files/tests.py @@ -210,12 +210,12 @@ def test_file_move_overwrite(self): class SpooledTempTests(unittest.TestCase): def test_in_memory_spooled_temp(self): with tempfile.SpooledTemporaryFile() as temp: - temp.write("foo bar baz quux\n") + temp.write(b"foo bar baz quux\n") django_file = File(temp, name="something.txt") - self.assertTrue(django_file.size, 17) + self.assertEqual(django_file.size, 17) def test_written_spooled_temp(self): with tempfile.SpooledTemporaryFile(max_size=4) as temp: - temp.write("foo bar baz quux\n") + temp.write(b"foo bar baz quux\n") django_file = File(temp, name="something.txt") - self.assertTrue(django_file.size, 17) + self.assertEqual(django_file.size, 17)