Normalized opening a file and decoding its content.

`io.open` is required on Python 2.7. Just `open` would work on Python 3.
This commit is contained in:
Aymeric Augustin 2014-11-11 22:36:41 +01:00
parent 95b8323ac2
commit 3bc7a14ea5
4 changed files with 10 additions and 7 deletions

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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: