Ticket #19367: 19367-2a.diff

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

Approach 1: Different behaviour depending on python 2 or 3

  • django/core/files/base.py

    diff --git a/django/core/files/base.py b/django/core/files/base.py
    index 71de5ab..2d10100 100644
    a b from io import BytesIO, StringIO, UnsupportedOperation  
    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
     137        if six.PY3:
     138            stream_class = StringIO if isinstance(content, six.text_type) else BytesIO
     139        else:
     140            stream_class = BytesIO
     141            content = force_bytes(content)
    139142        super(ContentFile, self).__init__(stream_class(content), name=name)
    140143        self.size = len(content)
    141144
  • tests/regressiontests/file_storage/tests.py

    diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py
    index 595b65d..45c18ba 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):  
    576583        retrieved content is of the same type.
    577584        """
    578585        self.assertTrue(isinstance(ContentFile(b"content").read(), bytes))
    579         self.assertTrue(isinstance(ContentFile("español").read(), six.text_type))
     586        if six.PY3:
     587            self.assertTrue(isinstance(ContentFile("español").read(), six.text_type))
     588        else:
     589            self.assertTrue(isinstance(ContentFile("español").read(), bytes))
     590
     591    def test_content_saving(self):
     592        """
     593        Test that ContentFile can be saved correctly with the filesystem storage,
     594        both if it was initialized with string or unicode content"""
     595        self.storage.save('bytes.txt', ContentFile(b"content"))
     596        self.storage.save('unicode.txt', ContentFile("español"))
     597
    580598
    581599class NoNameFileTestCase(unittest.TestCase):
    582600    """
Back to Top