From 3bc7a14ea52d916576b3d61dc6ba48b340a02671 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Tue, 11 Nov 2014 22:36:41 +0100 Subject: [PATCH] Normalized opening a file and decoding its content. `io.open` is required on Python 2.7. Just `open` would work on Python 3. --- django/core/management/commands/makemessages.py | 2 +- django/core/management/sql.py | 4 ++-- django/template/loaders/app_directories.py | 5 +++-- django/template/loaders/filesystem.py | 6 ++++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py index 6c22dfe1ea..77e7e62493 100644 --- a/django/core/management/commands/makemessages.py +++ b/django/core/management/commands/makemessages.py @@ -90,7 +90,7 @@ class TranslatableFile(object): work_file = orig_file is_templatized = file_ext in command.extensions if is_templatized: - with io.open(orig_file, 'r', encoding=settings.FILE_CHARSET) as fp: + with io.open(orig_file, encoding=settings.FILE_CHARSET) as fp: src_data = fp.read() content = templatize(src_data, orig_file[2:]) work_file = os.path.join(self.dirpath, '%s.py' % self.file) diff --git a/django/core/management/sql.py b/django/core/management/sql.py index df5d7f3a54..275711a385 100644 --- a/django/core/management/sql.py +++ b/django/core/management/sql.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals -import codecs +import io import os import re import warnings @@ -234,7 +234,7 @@ def custom_sql_for_model(model, style, connection): sql_files.append(os.path.join(app_dir, "%s.sql" % opts.model_name)) for sql_file in sql_files: if os.path.exists(sql_file): - with codecs.open(sql_file, 'r', encoding=settings.FILE_CHARSET) as fp: + with io.open(sql_file, encoding=settings.FILE_CHARSET) as fp: output.extend(connection.ops.prepare_sql_script(fp.read(), _allow_fallback=True)) return output diff --git a/django/template/loaders/app_directories.py b/django/template/loaders/app_directories.py index cab1edf8cc..db23f7226f 100644 --- a/django/template/loaders/app_directories.py +++ b/django/template/loaders/app_directories.py @@ -3,6 +3,7 @@ Wrapper for loading templates from "templates" directories in INSTALLED_APPS packages. """ +import io import os import sys @@ -56,8 +57,8 @@ class Loader(BaseLoader): def load_template_source(self, template_name, template_dirs=None): for filepath in self.get_template_sources(template_name, template_dirs): try: - with open(filepath, 'rb') as fp: - return (fp.read().decode(settings.FILE_CHARSET), filepath) + with io.open(filepath, encoding=settings.FILE_CHARSET) as fp: + return fp.read(), filepath except IOError: pass raise TemplateDoesNotExist(template_name) diff --git a/django/template/loaders/filesystem.py b/django/template/loaders/filesystem.py index dc7de84abf..935154e40c 100644 --- a/django/template/loaders/filesystem.py +++ b/django/template/loaders/filesystem.py @@ -2,6 +2,8 @@ Wrapper for loading templates from the filesystem. """ +import io + from django.conf import settings from django.core.exceptions import SuspiciousFileOperation from django.template.base import TemplateDoesNotExist @@ -32,8 +34,8 @@ class Loader(BaseLoader): tried = [] for filepath in self.get_template_sources(template_name, template_dirs): try: - with open(filepath, 'rb') as fp: - return (fp.read().decode(settings.FILE_CHARSET), filepath) + with io.open(filepath, encoding=settings.FILE_CHARSET) as fp: + return fp.read(), filepath except IOError: tried.append(filepath) if tried: