Ticket #15644: 15644-3.diff

File 15644-3.diff, 3.7 KB (added by Michael Palumbo <michael.palumbo87@…>, 4 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): 
    3636        if not hasattr(self, '_size'):
    3737            if hasattr(self.file, 'size'):
    3838                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):
    4040                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)
    4146            else:
    4247                raise AttributeError("Unable to determine the file's size.")
    4348        return self._size
    class File(FileProxyMixin): 
    6166
    6267        if hasattr(self, 'seek'):
    6368            self.seek(0)
    64         # Assume the pointer is at zero...
    65         counter = self.size
    66 
    67         while counter > 0:
    68             yield self.read(chunk_size)
    69             counter -= chunk_size
     69       
     70        while True:
     71            data = self.read(chunk_size)
     72            if not data:
     73                break
     74            yield data
    7075
    7176    def multiple_chunks(self, chunk_size=None):
    7277        """
  • 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: 
    1818
    1919from django.conf import settings
    2020from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
    21 from django.core.files.base import ContentFile
     21from django.core.files.base import File, ContentFile
    2222from django.core.files.images import get_image_dimensions
    2323from django.core.files.storage import FileSystemStorage, get_storage_class
    2424from django.core.files.uploadedfile import UploadedFile
    class ContentFileTestCase(unittest.TestCase): 
    550550    def test_content_file_default_name(self):
    551551        self.assertEqual(ContentFile("content").name, None)
    552552
    553     def test_content_file_custome_name(self):
     553    def test_content_file_custom_name(self):
    554554        name = "I can have a name too!"
    555555        self.assertEqual(ContentFile("content", name=name).name, name)
     556
     557class 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       
     568class 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())
Back to Top