summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_socketserver.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-03-05 08:06:26 (GMT)
committerGitHub <noreply@github.com>2019-03-05 08:06:26 (GMT)
commit5b10b9824780b2181158902067912ee9e7b04657 (patch)
tree1c89bea944e6638eb008c8f106b2ee48cc9448d1 /Lib/test/test_socketserver.py
parent9e4861f52349011cd5916eef8e8344575e8ac426 (diff)
downloadcpython-5b10b9824780b2181158902067912ee9e7b04657.zip
cpython-5b10b9824780b2181158902067912ee9e7b04657.tar.gz
cpython-5b10b9824780b2181158902067912ee9e7b04657.tar.bz2
bpo-22831: Use "with" to avoid possible fd leaks in tests (part 2). (GH-10929)
Diffstat (limited to 'Lib/test/test_socketserver.py')
-rw-r--r--Lib/test/test_socketserver.py36
1 files changed, 17 insertions, 19 deletions
diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py
index 6584ba5..8aed4b6 100644
--- a/Lib/test/test_socketserver.py
+++ b/Lib/test/test_socketserver.py
@@ -157,27 +157,25 @@ class SocketServerTest(unittest.TestCase):
if verbose: print("done")
def stream_examine(self, proto, addr):
- s = socket.socket(proto, socket.SOCK_STREAM)
- s.connect(addr)
- s.sendall(TEST_STR)
- buf = data = receive(s, 100)
- while data and b'\n' not in buf:
- data = receive(s, 100)
- buf += data
- self.assertEqual(buf, TEST_STR)
- s.close()
+ with socket.socket(proto, socket.SOCK_STREAM) as s:
+ s.connect(addr)
+ s.sendall(TEST_STR)
+ buf = data = receive(s, 100)
+ while data and b'\n' not in buf:
+ data = receive(s, 100)
+ buf += data
+ self.assertEqual(buf, TEST_STR)
def dgram_examine(self, proto, addr):
- s = socket.socket(proto, socket.SOCK_DGRAM)
- if HAVE_UNIX_SOCKETS and proto == socket.AF_UNIX:
- s.bind(self.pickaddr(proto))
- s.sendto(TEST_STR, addr)
- buf = data = receive(s, 100)
- while data and b'\n' not in buf:
- data = receive(s, 100)
- buf += data
- self.assertEqual(buf, TEST_STR)
- s.close()
+ with socket.socket(proto, socket.SOCK_DGRAM) as s:
+ if HAVE_UNIX_SOCKETS and proto == socket.AF_UNIX:
+ s.bind(self.pickaddr(proto))
+ s.sendto(TEST_STR, addr)
+ buf = data = receive(s, 100)
+ while data and b'\n' not in buf:
+ data = receive(s, 100)
+ buf += data
+ self.assertEqual(buf, TEST_STR)
def test_TCPServer(self):
self.run_server(socketserver.TCPServer,