Ticket #11739: 11739-4.diff

File 11739-4.diff, 2.2 KB (added by Claude Paroz, 13 years ago)

Third alternative

  • django/core/files/base.py

    diff --git a/django/core/files/base.py b/django/core/files/base.py
    index 48d0be4..1a5e0d4 100644
    a b  
    11import os
    22try:
    3     from cStringIO import StringIO
     3    from cStringIO import StringIO as fastStringIO
    44except ImportError:
    5     from StringIO import StringIO
     5    from StringIO import StringIO as fastStringIO
     6from StringIO import StringIO
    67
    78from django.utils.encoding import smart_str, smart_unicode
    89from django.core.files.utils import FileProxyMixin
    class File(FileProxyMixin):  
    8485        # Iterate over this file-like object by newlines
    8586        buffer_ = None
    8687        for chunk in self.chunks():
    87             chunk_buffer = StringIO(chunk)
     88            chunk_buffer = fastStringIO(chunk)
    8889
    8990            for line in chunk_buffer:
    9091                if buffer_:
    class ContentFile(File):  
    124125    """
    125126    def __init__(self, content, name=None):
    126127        content = content or ''
    127         super(ContentFile, self).__init__(StringIO(content), name=name)
     128        if isinstance(content, unicode):
     129            # fastStringIO might be linked to cStringIO, which doesn't support
     130            # unicode content (#11739)
     131            file_wrapper = StringIO(content)
     132        else:
     133            file_wrapper = fastStringIO(content)
     134        super(ContentFile, self).__init__(file_wrapper, name=name)
    128135        self.size = len(content)
    129136
    130137    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 d1ecbe6..576c1c3 100644
    a b class ContentFileTestCase(unittest.TestCase):  
    553553    def test_content_file_custome_name(self):
    554554        name = "I can have a name too!"
    555555        self.assertEqual(ContentFile("content", name=name).name, name)
     556
     557class ContentFileUnicodeBug(unittest.TestCase):
     558    """
     559    Tests that ContentFile instances can contain unicode (#11739)
     560    """
     561    def test_unicode(self):
     562        """
     563        ContentFile instances should be able to handle unicode.
     564        """
     565        snowman = u'☃'
     566        file = ContentFile(snowman)
     567        self.assertEqual(file.read(), snowman)
Back to Top