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:
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user