Opened 3 years ago

Closed 3 years ago

#21762 closed Bug (needsinfo)

django.core.files.Storage#save does not respect settings.FILE_CHARSET

Reported by: anonymous Owned by: nobody
Component: File uploads/storage Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Assuming that chunk is not an instance of bytes, this line:

https://github.com/django/django/blob/7e2d61a9724644d6d1c7ce9361d9fd5be3e2ab86/django/core/files/storage.py#L226

will create a text-mode file without setting the encoding to settings.FILE_CHARSET. As a result, my CI builds were failing with UnicodeEncodeErrors on invocation of manage.py collectstatic (which invoked django-pipeline, which uses this code).

Change History (3)

comment:1 Changed 3 years ago by anonymous

I suggest changing the code under if _file is None: to the following:

if isinstance(chunk, bytes):
    _file = os.fdopen(fd, 'wb')
else:
    _file = os.fdopen(fd, 'wt', encoding=settings.FILE_CHARSET)

comment:2 Changed 3 years ago by anonymous

Apparently the meaning of os.fdopen has changed in Python 3, which is what I'm using. The encoding kwarg is probably not accepted in Python 2, so maybe change it to use plain open if it needs to open in text mode.

comment:3 Changed 3 years ago by Claude Paroz

Resolution: needsinfo
Status: newclosed

settings.FILE_CHARSET is documented and used in Django for reading files, not for writing files. If you get a UnicodeEncodeError, I guess you have an issue with your default encoding. On Python 3 (which is the target here), I think that the default encoding is generally always UTF-8. Can you check that (locale.getpreferredencoding)?

Note: See TracTickets for help on using tickets.
Back to Top