Permalink
Browse files

Fixed #22307 -- 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.
  • Loading branch information...
hlawrenz authored and bmispelon committed Mar 21, 2014
1 parent 7e0f909 commit 918a16bc4c099ab0cae72a231de3e99e2a9d02cb
Showing with 15 additions and 1 deletion.
  1. +1 −1 django/core/files/base.py
  2. +14 −0 tests/files/tests.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()
View
@@ -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(b"foo bar baz quux\n")
+ django_file = File(temp, name="something.txt")
+ self.assertEqual(django_file.size, 17)
+
+ def test_written_spooled_temp(self):
+ with tempfile.SpooledTemporaryFile(max_size=4) as temp:
+ temp.write(b"foo bar baz quux\n")
+ django_file = File(temp, name="something.txt")
+ self.assertEqual(django_file.size, 17)

0 comments on commit 918a16b

Please sign in to comment.