Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed a failing test introduced in 918a16b.

Refs #22307.
  • Loading branch information...
commit 96fc3908ad25f40b4c6e2f76be2639f665223227 1 parent c94bff2
Baptiste Mispelon bmispelon authored
Showing with 19 additions and 12 deletions.
  1. +19 −12 django/core/files/base.py
31 django/core/files/base.py
View
@@ -36,19 +36,26 @@ def __nonzero__(self): # Python 2 compatibility
def __len__(self):
return self.size
+ def _get_size_from_underlying_file(self):
+ if hasattr(self.file, 'size'):
+ return self.file.size
+ if hasattr(self.file, 'name'):
+ try:
+ return os.path.getsize(self.file.name)
+ except (OSError, TypeError):
+ pass
+ if hasattr(self.file, 'tell') and hasattr(self.file, 'seek'):
+ pos = self.file.tell()
+ self.file.seek(0, os.SEEK_END)
+ size = self.file.tell()
+ self.file.seek(pos)
+ return size
+ raise AttributeError("Unable to determine the file's size.")
+
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 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()
- self.file.seek(0, os.SEEK_END)
- self._size = self.file.tell()
- self.file.seek(pos)
- else:
- raise AttributeError("Unable to determine the file's size.")
+ if hasattr(self, '_size'):
+ return self._size
+ self._size = self._get_size_from_underlying_file()
return self._size
def _set_size(self, size):
Please sign in to comment.
Something went wrong with that request. Please try again.