summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorSergey Fedoseev <fedoseev.sergey@gmail.com>2018-03-10 17:21:39 +0500
committerSergey Fedoseev <fedoseev.sergey@gmail.com>2018-03-10 17:21:39 +0500
commita5406fe989d27598652dfd9f8dc1429b5d2db85c (patch)
tree3cfff48927b9ecdfec84a9cee5e836f07d98bb2e /django
parent5b083a824ea90fb1705bd0c208245ff92a5237d8 (diff)
Used cached_property for File.size.
Diffstat (limited to 'django')
-rw-r--r--django/core/files/base.py17
1 files changed, 4 insertions, 13 deletions
diff --git a/django/core/files/base.py b/django/core/files/base.py
index fb819ed31f..bc87a870c2 100644
--- a/django/core/files/base.py
+++ b/django/core/files/base.py
@@ -2,6 +2,7 @@ import os
from io import BytesIO, StringIO, UnsupportedOperation
from django.core.files.utils import FileProxyMixin
+from django.utils.functional import cached_property
class File(FileProxyMixin):
@@ -27,7 +28,8 @@ class File(FileProxyMixin):
def __len__(self):
return self.size
- def _get_size_from_underlying_file(self):
+ @cached_property
+ def size(self):
if hasattr(self.file, 'size'):
return self.file.size
if hasattr(self.file, 'name'):
@@ -43,17 +45,6 @@ class File(FileProxyMixin):
return size
raise AttributeError("Unable to determine the file's size.")
- def _get_size(self):
- if hasattr(self, '_size'):
- return self._size
- self._size = self._get_size_from_underlying_file()
- return self._size
-
- def _set_size(self, size):
- self._size = size
-
- size = property(_get_size, _set_size)
-
def chunks(self, chunk_size=None):
"""
Read the file and yield chunks of ``chunk_size`` bytes (defaults to
@@ -150,7 +141,7 @@ class ContentFile(File):
pass
def write(self, data):
- self.__dict__.pop('_size', None) # Clear the computed size.
+ self.__dict__.pop('size', None) # Clear the computed size.
return self.file.write(data)