diff --git a/tests/file_uploads/views.py b/tests/file_uploads/views.py index 5269e98da7..c5ecaa2286 100644 --- a/tests/file_uploads/views.py +++ b/tests/file_uploads/views.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals +import contextlib import hashlib import json import os @@ -100,7 +101,10 @@ def file_upload_echo_content(request): """ Simple view to echo back the content of uploaded files for tests. """ - r = {k: f.read().decode('utf-8') for k, f in request.FILES.items()} + def read_and_close(f): + with contextlib.closing(f): + return f.read().decode('utf-8') + r = {k: read_and_close(f) for k, f in request.FILES.items()} return HttpResponse(json.dumps(r)) diff --git a/tests/gis_tests/geoapp/test_sitemaps.py b/tests/gis_tests/geoapp/test_sitemaps.py index 288d003f21..5162b11c77 100644 --- a/tests/gis_tests/geoapp/test_sitemaps.py +++ b/tests/gis_tests/geoapp/test_sitemaps.py @@ -60,10 +60,10 @@ class GeoSitemapTest(TestCase): elif kml_type == 'kmz': # Have to decompress KMZ before parsing. buf = BytesIO(self.client.get(kml_url).content) - zf = zipfile.ZipFile(buf) - self.assertEqual(1, len(zf.filelist)) - self.assertEqual('doc.kml', zf.filelist[0].filename) - kml_doc = minidom.parseString(zf.read('doc.kml')) + with zipfile.ZipFile(buf) as zf: + self.assertEqual(1, len(zf.filelist)) + self.assertEqual('doc.kml', zf.filelist[0].filename) + kml_doc = minidom.parseString(zf.read('doc.kml')) # Ensuring the correct number of placemarks are in the KML doc. if 'city' in kml_url: diff --git a/tests/middleware/tests.py b/tests/middleware/tests.py index 3029f2ea0e..aa84a69ca1 100644 --- a/tests/middleware/tests.py +++ b/tests/middleware/tests.py @@ -633,7 +633,8 @@ class GZipMiddlewareTest(SimpleTestCase): @staticmethod def decompress(gzipped_string): - return gzip.GzipFile(mode='rb', fileobj=BytesIO(gzipped_string)).read() + with gzip.GzipFile(mode='rb', fileobj=BytesIO(gzipped_string)) as f: + return f.read() def test_compress_response(self): """ diff --git a/tests/servers/tests.py b/tests/servers/tests.py index 3ba80cc23d..dbb298e003 100644 --- a/tests/servers/tests.py +++ b/tests/servers/tests.py @@ -4,6 +4,7 @@ Tests for django.core.servers. """ from __future__ import unicode_literals +import contextlib import os import socket @@ -114,16 +115,16 @@ class LiveServerViews(LiveServerBase): Ensure that the LiveServerTestCase serves views. Refs #2879. """ - f = self.urlopen('/example_view/') - self.assertEqual(f.read(), b'example view') + with contextlib.closing(self.urlopen('/example_view/')) as f: + self.assertEqual(f.read(), b'example view') def test_static_files(self): """ Ensure that the LiveServerTestCase serves static files. Refs #2879. """ - f = self.urlopen('/static/example_static_file.txt') - self.assertEqual(f.read().rstrip(b'\r\n'), b'example static file') + with contextlib.closing(self.urlopen('/static/example_static_file.txt')) as f: + self.assertEqual(f.read().rstrip(b'\r\n'), b'example static file') def test_no_collectstatic_emulation(self): """ @@ -143,12 +144,12 @@ class LiveServerViews(LiveServerBase): Ensure that the LiveServerTestCase serves media files. Refs #2879. """ - f = self.urlopen('/media/example_media_file.txt') - self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file') + with contextlib.closing(self.urlopen('/media/example_media_file.txt')) as f: + self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file') def test_environ(self): - f = self.urlopen('/environ_view/?%s' % urlencode({'q': 'тест'})) - self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read()) + with contextlib.closing(self.urlopen('/environ_view/?%s' % urlencode({'q': 'тест'}))) as f: + self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read()) class LiveServerDatabase(LiveServerBase): @@ -159,8 +160,8 @@ class LiveServerDatabase(LiveServerBase): live server thread. Refs #2879. """ - f = self.urlopen('/model_view/') - self.assertEqual(f.read().splitlines(), [b'jane', b'robert']) + with contextlib.closing(self.urlopen('/model_view/')) as f: + self.assertEqual(f.read().splitlines(), [b'jane', b'robert']) def test_database_writes(self): """ diff --git a/tests/staticfiles_tests/test_liveserver.py b/tests/staticfiles_tests/test_liveserver.py index b6dfc767e7..72dee8999c 100644 --- a/tests/staticfiles_tests/test_liveserver.py +++ b/tests/staticfiles_tests/test_liveserver.py @@ -4,6 +4,7 @@ django.contrib.staticfiles.testing.StaticLiveServerTestCase instead of django.test.LiveServerTestCase. """ +import contextlib import os from django.contrib.staticfiles.testing import StaticLiveServerTestCase @@ -94,5 +95,5 @@ class StaticLiveServerView(LiveServerBase): Test that StaticLiveServerTestCase use of staticfiles' serve() allows it to discover app's static assets without having to collectstatic first. """ - f = self.urlopen('/static/test/file.txt') - self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.') + with contextlib.closing(self.urlopen('/static/test/file.txt')) as f: + self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.')