summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2012-12-06 17:14:44 +0100
committerClaude Paroz <claude@2xlibre.net>2012-12-06 17:14:44 +0100
commit34dcf51e06fc57fc5462bd40f6a4c8ee949521da (patch)
tree6f5661c4d329891174cc06f5fdc11199f985dfaf /django
parent553838a285d6ef4005d7a97fc07aebfb0dcdb921 (diff)
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.
Diffstat (limited to 'django')
-rw-r--r--django/core/files/base.py9
1 files changed, 6 insertions, 3 deletions
diff --git a/django/core/files/base.py b/django/core/files/base.py
index 71de5ab741..2d10100019 100644
--- a/django/core/files/base.py
+++ b/django/core/files/base.py
@@ -6,7 +6,7 @@ from io import BytesIO, StringIO, UnsupportedOperation
from django.utils.encoding import smart_text
from django.core.files.utils import FileProxyMixin
from django.utils import six
-from django.utils.encoding import python_2_unicode_compatible
+from django.utils.encoding import force_bytes, python_2_unicode_compatible
@python_2_unicode_compatible
class File(FileProxyMixin):
@@ -134,8 +134,11 @@ class ContentFile(File):
A File-like object that takes just raw content, rather than an actual file.
"""
def __init__(self, content, name=None):
- content = content or b''
- stream_class = StringIO if isinstance(content, six.text_type) else BytesIO
+ if six.PY3:
+ stream_class = StringIO if isinstance(content, six.text_type) else BytesIO
+ else:
+ stream_class = BytesIO
+ content = force_bytes(content)
super(ContentFile, self).__init__(stream_class(content), name=name)
self.size = len(content)