Ticket #19367: 19367-2b.diff

File 19367-2b.diff, 2.7 KB (added by Claude Paroz, 12 years ago)

Approach 2: Always force content to bytes

  • django/core/files/base.py

    diff --git a/django/core/files/base.py b/django/core/files/base.py
    index 71de5ab..6e92a20 100644
    a b  
    11from __future__ import unicode_literals
    22
    33import os
    4 from io import BytesIO, StringIO, UnsupportedOperation
     4from io import BytesIO, UnsupportedOperation
    55
    66from django.utils.encoding import smart_text
    77from django.core.files.utils import FileProxyMixin
    88from django.utils import six
    9 from django.utils.encoding import python_2_unicode_compatible
     9from django.utils.encoding import force_bytes, python_2_unicode_compatible
    1010
    1111@python_2_unicode_compatible
    1212class File(FileProxyMixin):
    class ContentFile(File):  
    134134    A File-like object that takes just raw content, rather than an actual file.
    135135    """
    136136    def __init__(self, content, name=None):
    137         content = content or b''
    138         stream_class = StringIO if isinstance(content, six.text_type) else BytesIO
    139         super(ContentFile, self).__init__(stream_class(content), name=name)
     137        super(ContentFile, self).__init__(BytesIO(force_bytes(content)), name=name)
    140138        self.size = len(content)
    141139
    142140    def __str__(self):
  • tests/regressiontests/file_storage/tests.py

    diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py
    index 595b65d..738e4f8 100644
    a b class InconsistentGetImageDimensionsBug(unittest.TestCase):  
    560560
    561561class ContentFileTestCase(unittest.TestCase):
    562562
     563    def setUp(self):
     564        self.storage_dir = tempfile.mkdtemp()
     565        self.storage = FileSystemStorage(self.storage_dir)
     566
     567    def tearDown(self):
     568        shutil.rmtree(self.storage_dir)
     569
    563570    def test_content_file_default_name(self):
    564571        self.assertEqual(ContentFile(b"content").name, None)
    565572
    class ContentFileTestCase(unittest.TestCase):  
    573580    def test_content_file_input_type(self):
    574581        """
    575582        Test that ContentFile can accept both bytes and unicode and that the
    576         retrieved content is of the same type.
     583        retrieved content is always bytes.
    577584        """
    578585        self.assertTrue(isinstance(ContentFile(b"content").read(), bytes))
    579         self.assertTrue(isinstance(ContentFile("español").read(), six.text_type))
     586        self.assertTrue(isinstance(ContentFile("español").read(), bytes))
     587
     588    def test_content_saving(self):
     589        """
     590        Test that ContentFile can be saved correctly with the filesystem storage,
     591        both if it was initialized with string or unicode content"""
     592        self.storage.save('bytes.txt', ContentFile(b"content"))
     593        self.storage.save('unicode.txt', ContentFile("español"))
     594
    580595
    581596class NoNameFileTestCase(unittest.TestCase):
    582597    """
Back to Top