summaryrefslogtreecommitdiff
path: root/django/core/files/base.py
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2012-04-05 15:44:04 +0000
committerClaude Paroz <claude@2xlibre.net>2012-04-05 15:44:04 +0000
commit5c954136eaef3d98d532368deec4c19cf892f664 (patch)
tree2fb423b086e301350e7053340e05c8ccb7959b8f /django/core/files/base.py
parent5e047ed859251d8019a185262b8f5abf5966af09 (diff)
downloaddjango-5c954136eaef3d98d532368deec4c19cf892f664.tar.gz
Fixed #15644 -- Improved Django File wrapper to support more file-like objects. Thanks nickname123 and Michael Palumbo for working on the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17871 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core/files/base.py')
-rw-r--r--django/core/files/base.py17
1 files changed, 11 insertions, 6 deletions
diff --git a/django/core/files/base.py b/django/core/files/base.py
index 48d0be43f0..29dbb6fcc1 100644
--- a/django/core/files/base.py
+++ b/django/core/files/base.py
@@ -36,8 +36,13 @@ class File(FileProxyMixin):
if not hasattr(self, '_size'):
if hasattr(self.file, 'size'):
self._size = self.file.size
- elif os.path.exists(self.file.name):
+ elif hasattr(self.file, 'name') 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.")
return self._size
@@ -61,12 +66,12 @@ class File(FileProxyMixin):
if hasattr(self, 'seek'):
self.seek(0)
- # Assume the pointer is at zero...
- counter = self.size
- while counter > 0:
- yield self.read(chunk_size)
- counter -= chunk_size
+ while True:
+ data = self.read(chunk_size)
+ if not data:
+ break
+ yield data
def multiple_chunks(self, chunk_size=None):
"""