From: Mukund Sivaraman Date: Thu, 14 Jun 2012 07:15:17 +0000 (+0530) Subject: [1828] Make code safer by using with X-Git-Tag: trac2351_base~226^2~42^2~1^2~17 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=57449046356fda9a21f3cf2dc1e5faadaaf42d7c;p=thirdparty%2Fkea.git [1828] Make code safer by using with --- diff --git a/src/bin/xfrout/tests/xfrout_test.py.in b/src/bin/xfrout/tests/xfrout_test.py.in index ab463d2c66..3c5066d9a8 100644 --- a/src/bin/xfrout/tests/xfrout_test.py.in +++ b/src/bin/xfrout/tests/xfrout_test.py.in @@ -1196,31 +1196,30 @@ class TestUnixSockServer(unittest.TestCase): # We test with UDP, as it can be "connected" without other # endpoint. Note that in the current implementation _guess_remote() # unconditionally returns SOCK_STREAM. - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - sock.connect(('127.0.0.1', 12345)) - self.assertEqual((socket.AF_INET, socket.SOCK_STREAM, - ('127.0.0.1', 12345)), - self.unix._guess_remote(sock.fileno())) - sock.close() + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: + sock.connect(('127.0.0.1', 12345)) + self.assertEqual((socket.AF_INET, socket.SOCK_STREAM, + ('127.0.0.1', 12345)), + self.unix._guess_remote(sock.fileno())) + if socket.has_ipv6: # Don't check IPv6 address on hosts not supporting them - sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) - sock.connect(('::1', 12345)) - self.assertEqual((socket.AF_INET6, socket.SOCK_STREAM, - ('::1', 12345, 0, 0)), - self.unix._guess_remote(sock.fileno())) - sock.close() + with socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) as sock: + sock.connect(('::1', 12345)) + self.assertEqual((socket.AF_INET6, socket.SOCK_STREAM, + ('::1', 12345, 0, 0)), + self.unix._guess_remote(sock.fileno())) + # Try when pretending there's no IPv6 support # (No need to pretend when there's really no IPv6) xfrout.socket.has_ipv6 = False - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - sock.connect(('127.0.0.1', 12345)) - self.assertEqual((socket.AF_INET, socket.SOCK_STREAM, - ('127.0.0.1', 12345)), - self.unix._guess_remote(sock.fileno())) + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock: + sock.connect(('127.0.0.1', 12345)) + self.assertEqual((socket.AF_INET, socket.SOCK_STREAM, + ('127.0.0.1', 12345)), + self.unix._guess_remote(sock.fileno())) # Return it back xfrout.socket.has_ipv6 = True - sock.close() def test_receive_query_message(self): send_msg = b"\xd6=\x00\x00\x00\x01\x00" @@ -1380,9 +1379,9 @@ class TestUnixSockServer(unittest.TestCase): self._start_unix_sock_server(sock_file) old_stdout = sys.stdout - sys.stdout = open(os.devnull, 'w') - self.assertTrue(self.unix._sock_file_in_use(sock_file)) - sys.stdout.close() + with open(os.devnull, 'w') as f: + sys.stdout = f + self.assertTrue(self.unix._sock_file_in_use(sock_file)) sys.stdout = old_stdout def test_remove_unused_sock_file_in_use(self):