summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVille Skyttä <ville.skytta@iki.fi>2016-08-05 02:45:14 +0300
committerTim Graham <timograham@gmail.com>2016-08-04 19:45:14 -0400
commita2fb2b3a1feee512131c76e9040232faf2362526 (patch)
treeaef371904eb7c1710fb717c3a6bf77b3da44ee94
parent50e299dbfbbfd796e63e7e13b4566cf69d2c4acb (diff)
Fixed #27020 -- Used a context manager to close files.
-rw-r--r--django/contrib/admindocs/views.py7
-rw-r--r--django/contrib/auth/password_validation.py3
-rw-r--r--django/contrib/gis/shortcuts.py5
-rw-r--r--django/utils/text.py22
-rw-r--r--tests/test_client/test_conditional_content_removal.py5
5 files changed, 21 insertions, 21 deletions
diff --git a/django/contrib/admindocs/views.py b/django/contrib/admindocs/views.py
index b122c315fc..15f0c7fb3b 100644
--- a/django/contrib/admindocs/views.py
+++ b/django/contrib/admindocs/views.py
@@ -324,10 +324,15 @@ class TemplateDetailView(BaseAdminDocsView):
# This doesn't account for template loaders (#24128).
for index, directory in enumerate(default_engine.dirs):
template_file = os.path.join(directory, template)
+ if os.path.exists(template_file):
+ with open(template_file) as f:
+ template_contents = f.read()
+ else:
+ template_contents = ''
templates.append({
'file': template_file,
'exists': os.path.exists(template_file),
- 'contents': lambda: open(template_file).read() if os.path.exists(template_file) else '',
+ 'contents': template_contents,
'order': index,
})
kwargs.update({
diff --git a/django/contrib/auth/password_validation.py b/django/contrib/auth/password_validation.py
index aba2d6f1cf..e5ab22b614 100644
--- a/django/contrib/auth/password_validation.py
+++ b/django/contrib/auth/password_validation.py
@@ -169,7 +169,8 @@ class CommonPasswordValidator(object):
def __init__(self, password_list_path=DEFAULT_PASSWORD_LIST_PATH):
try:
- common_passwords_lines = gzip.open(password_list_path).read().decode('utf-8').splitlines()
+ with gzip.open(password_list_path) as f:
+ common_passwords_lines = f.read().decode('utf-8').splitlines()
except IOError:
with open(password_list_path) as f:
common_passwords_lines = f.readlines()
diff --git a/django/contrib/gis/shortcuts.py b/django/contrib/gis/shortcuts.py
index 6e0caaee60..67e6d06b2f 100644
--- a/django/contrib/gis/shortcuts.py
+++ b/django/contrib/gis/shortcuts.py
@@ -15,9 +15,8 @@ except ImportError:
def compress_kml(kml):
"Returns compressed KMZ from the given KML string."
kmz = BytesIO()
- zf = zipfile.ZipFile(kmz, 'a', zipfile.ZIP_DEFLATED)
- zf.writestr('doc.kml', kml.encode(settings.DEFAULT_CHARSET))
- zf.close()
+ with zipfile.ZipFile(kmz, 'a', zipfile.ZIP_DEFLATED) as zf:
+ zf.writestr('doc.kml', kml.encode(settings.DEFAULT_CHARSET))
kmz.seek(0)
return kmz.read()
diff --git a/django/utils/text.py b/django/utils/text.py
index e6849a018d..f51113b483 100644
--- a/django/utils/text.py
+++ b/django/utils/text.py
@@ -291,9 +291,8 @@ def phone2numeric(phone):
# Used with permission.
def compress_string(s):
zbuf = BytesIO()
- zfile = GzipFile(mode='wb', compresslevel=6, fileobj=zbuf)
- zfile.write(s)
- zfile.close()
+ with GzipFile(mode='wb', compresslevel=6, fileobj=zbuf) as zfile:
+ zfile.write(s)
return zbuf.getvalue()
@@ -321,15 +320,14 @@ class StreamingBuffer(object):
# Like compress_string, but for iterators of strings.
def compress_sequence(sequence):
buf = StreamingBuffer()
- zfile = GzipFile(mode='wb', compresslevel=6, fileobj=buf)
- # Output headers...
- yield buf.read()
- for item in sequence:
- zfile.write(item)
- data = buf.read()
- if data:
- yield data
- zfile.close()
+ with GzipFile(mode='wb', compresslevel=6, fileobj=buf) as zfile:
+ # Output headers...
+ yield buf.read()
+ for item in sequence:
+ zfile.write(item)
+ data = buf.read()
+ if data:
+ yield data
yield buf.read()
diff --git a/tests/test_client/test_conditional_content_removal.py b/tests/test_client/test_conditional_content_removal.py
index cc5bdb69ed..644eb23a06 100644
--- a/tests/test_client/test_conditional_content_removal.py
+++ b/tests/test_client/test_conditional_content_removal.py
@@ -11,11 +11,8 @@ from django.test.client import conditional_content_removal
# based on Python 3.3's gzip.compress
def gzip_compress(data):
buf = io.BytesIO()
- f = gzip.GzipFile(fileobj=buf, mode='wb', compresslevel=0)
- try:
+ with gzip.GzipFile(fileobj=buf, mode='wb', compresslevel=0) as f:
f.write(data)
- finally:
- f.close()
return buf.getvalue()