From a5406fe989d27598652dfd9f8dc1429b5d2db85c Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Sat, 10 Mar 2018 17:21:39 +0500 Subject: [PATCH] Used cached_property for File.size. --- django/core/files/base.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/django/core/files/base.py b/django/core/files/base.py index fb819ed31f2..bc87a870c27 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)