Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed SpooledTemporaryFile bug in File class. #2455

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion django/core/files/base.py
Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions tests/files/tests.py
Expand Up @@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Python3 compatibility, you should use b"foo bar baz quux\n"

django_file = File(temp, name="something.txt")
self.assertTrue(django_file.size, 17)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be assertEqual?


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)