Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #19367 -- Fixed saving ContentFile in filesystem storage

This was not working properly when ContentFile was initialized with
an unicode string.
Thanks Alexey Boriskin for the report and the test.

Backport of 34dcf51 from master.
  • Loading branch information...
commit e9301ae451f84b06687d27af0a43bcb986054f63 1 parent 57a41d9
Claude Paroz authored December 06, 2012
9  django/core/files/base.py
@@ -6,7 +6,7 @@
6 6
 from django.utils.encoding import smart_text
7 7
 from django.core.files.utils import FileProxyMixin
8 8
 from django.utils import six
9  
-from django.utils.encoding import python_2_unicode_compatible
  9
+from django.utils.encoding import force_bytes, python_2_unicode_compatible
10 10
 
11 11
 @python_2_unicode_compatible
12 12
 class File(FileProxyMixin):
@@ -134,8 +134,11 @@ class ContentFile(File):
134 134
     A File-like object that takes just raw content, rather than an actual file.
135 135
     """
136 136
     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)
139 142
         super(ContentFile, self).__init__(stream_class(content), name=name)
140 143
         self.size = len(content)
141 144
 
20  tests/regressiontests/file_storage/tests.py
@@ -560,6 +560,13 @@ def test_multiple_calls(self):
560 560
 
561 561
 class ContentFileTestCase(unittest.TestCase):
562 562
 
  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
+
563 570
     def test_content_file_default_name(self):
564 571
         self.assertEqual(ContentFile(b"content").name, None)
565 572
 
@@ -576,7 +583,18 @@ def test_content_file_input_type(self):
576 583
         retrieved content is of the same type.
577 584
         """
578 585
         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
+
580 598
 
581 599
 class NoNameFileTestCase(unittest.TestCase):
582 600
     """

0 notes on commit e9301ae

Please sign in to comment.
Something went wrong with that request. Please try again.