Skip to content

Commit a5406fe

Browse files
committed
Used cached_property for File.size.
1 parent 5b083a8 commit a5406fe

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

django/core/files/base.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from io import BytesIO, StringIO, UnsupportedOperation
33

44
from django.core.files.utils import FileProxyMixin
5+
from django.utils.functional import cached_property
56

67

78
class File(FileProxyMixin):
@@ -27,7 +28,8 @@ def __bool__(self):
2728
def __len__(self):
2829
return self.size
2930

30-
def _get_size_from_underlying_file(self):
31+
@cached_property
32+
def size(self):
3133
if hasattr(self.file, 'size'):
3234
return self.file.size
3335
if hasattr(self.file, 'name'):
@@ -43,17 +45,6 @@ def _get_size_from_underlying_file(self):
4345
return size
4446
raise AttributeError("Unable to determine the file's size.")
4547

46-
def _get_size(self):
47-
if hasattr(self, '_size'):
48-
return self._size
49-
self._size = self._get_size_from_underlying_file()
50-
return self._size
51-
52-
def _set_size(self, size):
53-
self._size = size
54-
55-
size = property(_get_size, _set_size)
56-
5748
def chunks(self, chunk_size=None):
5849
"""
5950
Read the file and yield chunks of ``chunk_size`` bytes (defaults to
@@ -150,7 +141,7 @@ def close(self):
150141
pass
151142

152143
def write(self, data):
153-
self.__dict__.pop('_size', None) # Clear the computed size.
144+
self.__dict__.pop('size', None) # Clear the computed size.
154145
return self.file.write(data)
155146

156147

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy