From baae4b818778180fedfcfcfc7aa77acfb9b237fb Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Mon, 3 Dec 2012 10:37:26 +0100 Subject: [PATCH] Fixed #19397 -- Crash on binary files in project templates. Thanks gw 2012 at tnode com for the report. --- django/core/management/templates.py | 8 ++++---- .../project_template/ticket-19397-binary-file.ico | Bin 0 -> 894 bytes 2 files changed, 4 insertions(+), 4 deletions(-) create mode 100644 tests/regressiontests/admin_scripts/custom_templates/project_template/ticket-19397-binary-file.ico diff --git a/django/core/management/templates.py b/django/core/management/templates.py index d34a0deb7e..f522097b8c 100644 --- a/django/core/management/templates.py +++ b/django/core/management/templates.py @@ -8,8 +8,6 @@ import shutil import stat import sys import tempfile -import codecs - try: from urllib.request import urlretrieve except ImportError: # Python 2 @@ -156,12 +154,14 @@ class TemplateCommand(BaseCommand): # Only render the Python files, as we don't want to # accidentally render Django templates files - with codecs.open(old_path, 'r', 'utf-8') as template_file: + with open(old_path, 'rb') as template_file: content = template_file.read() if filename.endswith(extensions) or filename in extra_files: + content = content.decode('utf-8') template = Template(content) content = template.render(context) - with codecs.open(new_path, 'w', 'utf-8') as new_file: + content = content.encode('utf-8') + with open(new_path, 'wb') as new_file: new_file.write(content) if self.verbosity >= 2: diff --git a/tests/regressiontests/admin_scripts/custom_templates/project_template/ticket-19397-binary-file.ico b/tests/regressiontests/admin_scripts/custom_templates/project_template/ticket-19397-binary-file.ico new file mode 100644 index 0000000000000000000000000000000000000000..1db49645b557569b7cd290a84eaf8048cc052ad0 GIT binary patch literal 894 zcmZQzU<5(|0R|wDV8~!*U=RbcG=LZ+qyWT>Kyh54Yks{;nlX_GzhciU%f3Y|Mct{y znC6~gw*TbL|NsC0{r9)DF9VMj*K`w)Omm zP|f!r-|jxTncW%-WPknk1yg901jS{_^R|x6hdB z;ob=@atM z!xLFOknNUk3PfmvKra3yil!c@n1SK{e@uKPo`FHq3Mg*Rz)