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
|
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): |
… |
… |
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 | |
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):
|
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 | |
… |
… |
class ContentFileTestCase(unittest.TestCase):
|
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 | """ |