1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

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

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