1
0
mirror of https://github.com/django/django.git synced 2025-10-30 09:06:13 +00:00

Made more extensive usage of context managers with open.

This commit is contained in:
Claude Paroz
2012-05-05 14:01:38 +02:00
parent ec5423df05
commit 865cd35c9b
33 changed files with 178 additions and 233 deletions

View File

@@ -328,9 +328,8 @@ class SsiNode(Node):
else:
return '' # Fail silently for invalid includes.
try:
fp = open(filepath, 'r')
output = fp.read()
fp.close()
with open(filepath, 'r') as fp:
output = fp.read()
except IOError:
output = ''
if self.parsed:

View File

@@ -52,11 +52,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:
file = open(filepath)
try:
return (file.read().decode(settings.FILE_CHARSET), filepath)
finally:
file.close()
with open(filepath) as fp:
return (fp.read().decode(settings.FILE_CHARSET), filepath)
except IOError:
pass
raise TemplateDoesNotExist(template_name)

View File

@@ -34,11 +34,8 @@ class Loader(BaseLoader):
tried = []
for filepath in self.get_template_sources(template_name, template_dirs):
try:
file = open(filepath)
try:
return (file.read().decode(settings.FILE_CHARSET), filepath)
finally:
file.close()
with open(filepath) as fp:
return (fp.read().decode(settings.FILE_CHARSET), filepath)
except IOError:
tried.append(filepath)
if tried: