From: Benjamin Peterson Date: Fri, 4 Apr 2014 17:59:33 +0000 (-0400) Subject: use with statement X-Git-Tag: v2.7.7rc1~85 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3c0027b7214ad45224399493a4899afa116b9056;p=python use with statement --- diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py index 88e2e854a5..2390c70a1d 100644 --- a/Lib/test/test_httpservers.py +++ b/Lib/test/test_httpservers.py @@ -324,18 +324,16 @@ class SimpleHTTPServerTestCase(BaseTestCase): self.check_status_and_reason(response, 404) response = self.request('/' + 'ThisDoesNotExist' + '/') self.check_status_and_reason(response, 404) - f = open(os.path.join(self.tempdir_name, 'index.html'), 'w') - response = self.request('/' + self.tempdir_name + '/') - self.check_status_and_reason(response, 200) - - # chmod() doesn't work as expected on Windows, and filesystem - # permissions are ignored by root on Unix. - if os.name == 'posix' and os.geteuid() != 0: - os.chmod(self.tempdir, 0) - response = self.request(self.tempdir_name + '/') - self.check_status_and_reason(response, 404) - os.chmod(self.tempdir, 0755) - f.close() + with open(os.path.join(self.tempdir_name, 'index.html'), 'w') as fp: + response = self.request('/' + self.tempdir_name + '/') + self.check_status_and_reason(response, 200) + # chmod() doesn't work as expected on Windows, and filesystem + # permissions are ignored by root on Unix. + if os.name == 'posix' and os.geteuid() != 0: + os.chmod(self.tempdir, 0) + response = self.request(self.tempdir_name + '/') + self.check_status_and_reason(response, 404) + os.chmod(self.tempdir, 0755) def test_head(self): response = self.request(