Ticket #15644: 15644-2.diff

File 15644-2.diff, 2.3 KB (added by claudep, 3 years ago)

Patch with tests

  • django/core/files/base.py

    diff --git a/django/core/files/base.py b/django/core/files/base.py
    index 48d0be4..47c06ab 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
  • tests/regressiontests/file_storage/tests.py

    diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py
    index 769e2c7..74d13e5 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)
Back to Top