Ticket #15644: 15644-3.diff
File 15644-3.diff, 3.7 KB (added by , 13 years ago) |
---|
-
django/core/files/base.py
diff --git a/django/core/files/base.py b/django/core/files/base.py index 48d0be4..b6482c5 100644
a b class File(FileProxyMixin): 36 36 if not hasattr(self, '_size'): 37 37 if hasattr(self.file, 'size'): 38 38 self._size = self.file.size 39 elif os.path.exists(self.file.name):39 elif hasattr(self.file, 'name') and os.path.exists(self.file.name): 40 40 self._size = os.path.getsize(self.file.name) 41 elif hasattr(self.file, 'tell') and hasattr(self.file, 'seek'): 42 pos = self.file.tell() 43 self.file.seek(0, os.SEEK_END) 44 self._size = self.file.tell() 45 self.file.seek(pos) 41 46 else: 42 47 raise AttributeError("Unable to determine the file's size.") 43 48 return self._size … … class File(FileProxyMixin): 61 66 62 67 if hasattr(self, 'seek'): 63 68 self.seek(0) 64 # Assume the pointer is at zero...65 counter = self.size66 67 while counter > 0:68 yield self.read(chunk_size)69 counter -= chunk_size69 70 while True: 71 data = self.read(chunk_size) 72 if not data: 73 break 74 yield data 70 75 71 76 def multiple_chunks(self, chunk_size=None): 72 77 """ -
tests/regressiontests/file_storage/tests.py
diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py index 769e2c7..17e2b44 100644
a b except ImportError: 18 18 19 19 from django.conf import settings 20 20 from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured 21 from django.core.files.base import ContentFile21 from django.core.files.base import File, ContentFile 22 22 from django.core.files.images import get_image_dimensions 23 23 from django.core.files.storage import FileSystemStorage, get_storage_class 24 24 from django.core.files.uploadedfile import UploadedFile … … class ContentFileTestCase(unittest.TestCase): 550 550 def test_content_file_default_name(self): 551 551 self.assertEqual(ContentFile("content").name, None) 552 552 553 def test_content_file_custom e_name(self):553 def test_content_file_custom_name(self): 554 554 name = "I can have a name too!" 555 555 self.assertEqual(ContentFile("content", name=name).name, name) 556 557 class NoNameFileTestCase(unittest.TestCase): 558 """ 559 Other examples of unnamed files may be tempfile.SpooledTemporaryFile or 560 urllib.urlopen() 561 """ 562 def test_noname_file_default_name(self): 563 self.assertEqual(File(StringIO('A file with no name')).name, None) 564 565 def test_noname_file_get_size(self): 566 self.assertEqual(File(StringIO('A file with no name')).size, 19) 567 568 class FileLikeObjectTestCase(unittest.TestCase): 569 """ 570 Test file-like objects (#15644). 571 """ 572 def setUp(self): 573 self.temp_dir = tempfile.mkdtemp() 574 self.storage = FileSystemStorage(location=self.temp_dir) 575 576 def tearDown(self): 577 shutil.rmtree(self.temp_dir) 578 579 def test_urllib2_urlopen(self): 580 """ 581 Test the File storage API with a file like object coming from urllib2.urlopen() 582 """ 583 import urllib2 584 585 url = 'http://www.djangoproject.com' 586 file_like_object = urllib2.urlopen(urllib2.Request(url)) 587 f = File(file_like_object) 588 stored_filename = self.storage.save("remote_file.html", f) 589 590 stored_file = self.storage.open(stored_filename) 591 remote_file = urllib2.urlopen(urllib2.Request(url)) 592 593 self.assertEqual(stored_file.read(), remote_file.read())