Refs #23919 -- Removed obsolete contextlib.closing() calls (for Python 2).

This commit is contained in:
Tim Graham 2017-01-19 08:50:28 -05:00 committed by GitHub
parent 7d20094996
commit 5320fa77c3
3 changed files with 7 additions and 10 deletions

View File

@ -1,4 +1,3 @@
import contextlib
import hashlib import hashlib
import json import json
import os import os
@ -98,7 +97,7 @@ def file_upload_echo_content(request):
Simple view to echo back the content of uploaded files for tests. Simple view to echo back the content of uploaded files for tests.
""" """
def read_and_close(f): def read_and_close(f):
with contextlib.closing(f): with f:
return f.read().decode('utf-8') return f.read().decode('utf-8')
r = {k: read_and_close(f) for k, f in request.FILES.items()} r = {k: read_and_close(f) for k, f in request.FILES.items()}
return HttpResponse(json.dumps(r)) return HttpResponse(json.dumps(r))

View File

@ -1,7 +1,6 @@
""" """
Tests for django.core.servers. Tests for django.core.servers.
""" """
import contextlib
import errno import errno
import os import os
import socket import socket
@ -58,11 +57,11 @@ class LiveServerViews(LiveServerBase):
self.assertEqual(err.exception.code, 404, 'Expected 404 response') self.assertEqual(err.exception.code, 404, 'Expected 404 response')
def test_view(self): def test_view(self):
with contextlib.closing(self.urlopen('/example_view/')) as f: with self.urlopen('/example_view/') as f:
self.assertEqual(f.read(), b'example view') self.assertEqual(f.read(), b'example view')
def test_static_files(self): def test_static_files(self):
with contextlib.closing(self.urlopen('/static/example_static_file.txt')) as f: with self.urlopen('/static/example_static_file.txt') as f:
self.assertEqual(f.read().rstrip(b'\r\n'), b'example static file') self.assertEqual(f.read().rstrip(b'\r\n'), b'example static file')
def test_no_collectstatic_emulation(self): def test_no_collectstatic_emulation(self):
@ -76,11 +75,11 @@ class LiveServerViews(LiveServerBase):
self.assertEqual(err.exception.code, 404, 'Expected 404 response') self.assertEqual(err.exception.code, 404, 'Expected 404 response')
def test_media_files(self): def test_media_files(self):
with contextlib.closing(self.urlopen('/media/example_media_file.txt')) as f: with self.urlopen('/media/example_media_file.txt') as f:
self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file') self.assertEqual(f.read().rstrip(b'\r\n'), b'example media file')
def test_environ(self): def test_environ(self):
with contextlib.closing(self.urlopen('/environ_view/?%s' % urlencode({'q': 'тест'}))) as f: with 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()) self.assertIn(b"QUERY_STRING: 'q=%D1%82%D0%B5%D1%81%D1%82'", f.read())
@ -90,7 +89,7 @@ class LiveServerDatabase(LiveServerBase):
""" """
Fixtures are properly loaded and visible to the live server thread. Fixtures are properly loaded and visible to the live server thread.
""" """
with contextlib.closing(self.urlopen('/model_view/')) as f: with self.urlopen('/model_view/') as f:
self.assertEqual(f.read().splitlines(), [b'jane', b'robert']) self.assertEqual(f.read().splitlines(), [b'jane', b'robert'])
def test_database_writes(self): def test_database_writes(self):

View File

@ -4,7 +4,6 @@ django.contrib.staticfiles.testing.StaticLiveServerTestCase instead of
django.test.LiveServerTestCase. django.test.LiveServerTestCase.
""" """
import contextlib
import os import os
from urllib.request import urlopen from urllib.request import urlopen
@ -86,5 +85,5 @@ class StaticLiveServerView(LiveServerBase):
StaticLiveServerTestCase use of staticfiles' serve() allows it StaticLiveServerTestCase use of staticfiles' serve() allows it
to discover app's static assets without having to collectstatic first. to discover app's static assets without having to collectstatic first.
""" """
with contextlib.closing(self.urlopen('/static/test/file.txt')) as f: with self.urlopen('/static/test/file.txt') as f:
self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.') self.assertEqual(f.read().rstrip(b'\r\n'), b'In static directory.')