Kaydet (Commit) 1d5eb342 authored tarafından Victor Stinner's avatar Victor Stinner

test_socket: use context managers to close directly the socket

Fix ResourceWarning(unclosed socket) warnings. Patch written by Nadeem Vawda.
üst a935e8ff
...@@ -1678,25 +1678,25 @@ class TestLinuxAbstractNamespace(unittest.TestCase): ...@@ -1678,25 +1678,25 @@ class TestLinuxAbstractNamespace(unittest.TestCase):
def testLinuxAbstractNamespace(self): def testLinuxAbstractNamespace(self):
address = b"\x00python-test-hello\x00\xff" address = b"\x00python-test-hello\x00\xff"
s1 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s1:
s1.bind(address) s1.bind(address)
s1.listen(1) s1.listen(1)
s2 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s2:
s2.connect(s1.getsockname()) s2.connect(s1.getsockname())
s1.accept() with s1.accept()[0] as s3:
self.assertEqual(s1.getsockname(), address) self.assertEqual(s1.getsockname(), address)
self.assertEqual(s2.getpeername(), address) self.assertEqual(s2.getpeername(), address)
def testMaxName(self): def testMaxName(self):
address = b"\x00" + b"h" * (self.UNIX_PATH_MAX - 1) address = b"\x00" + b"h" * (self.UNIX_PATH_MAX - 1)
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
s.bind(address) s.bind(address)
self.assertEqual(s.getsockname(), address) self.assertEqual(s.getsockname(), address)
def testNameOverflow(self): def testNameOverflow(self):
address = "\x00" + "h" * self.UNIX_PATH_MAX address = "\x00" + "h" * self.UNIX_PATH_MAX
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
self.assertRaises(socket.error, s.bind, address) self.assertRaises(socket.error, s.bind, address)
@unittest.skipUnless(thread, 'Threading required for this test.') @unittest.skipUnless(thread, 'Threading required for this test.')
...@@ -1898,10 +1898,10 @@ class CloexecConstantTest(unittest.TestCase): ...@@ -1898,10 +1898,10 @@ class CloexecConstantTest(unittest.TestCase):
if v < (2, 6, 28): if v < (2, 6, 28):
self.skipTest("Linux kernel 2.6.28 or higher required, not %s" self.skipTest("Linux kernel 2.6.28 or higher required, not %s"
% ".".join(map(str, v))) % ".".join(map(str, v)))
s = socket.socket(socket.AF_INET, with socket.socket(socket.AF_INET,
socket.SOCK_STREAM | socket.SOCK_CLOEXEC) socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s:
self.assertTrue(s.type & socket.SOCK_CLOEXEC) self.assertTrue(s.type & socket.SOCK_CLOEXEC)
self.assertTrue(fcntl.fcntl(s, fcntl.F_GETFD) & fcntl.FD_CLOEXEC) self.assertTrue(fcntl.fcntl(s, fcntl.F_GETFD) & fcntl.FD_CLOEXEC)
@unittest.skipUnless(hasattr(socket, "SOCK_NONBLOCK"), @unittest.skipUnless(hasattr(socket, "SOCK_NONBLOCK"),
...@@ -1922,29 +1922,33 @@ class NonblockConstantTest(unittest.TestCase): ...@@ -1922,29 +1922,33 @@ class NonblockConstantTest(unittest.TestCase):
% ".".join(map(str, v))) % ".".join(map(str, v)))
# a lot of it seems silly and redundant, but I wanted to test that # a lot of it seems silly and redundant, but I wanted to test that
# changing back and forth worked ok # changing back and forth worked ok
s = socket.socket(socket.AF_INET, with socket.socket(socket.AF_INET,
socket.SOCK_STREAM | socket.SOCK_NONBLOCK) socket.SOCK_STREAM | socket.SOCK_NONBLOCK) as s:
self.checkNonblock(s) self.checkNonblock(s)
s.setblocking(1) s.setblocking(1)
self.checkNonblock(s, False) self.checkNonblock(s, False)
s.setblocking(0) s.setblocking(0)
self.checkNonblock(s) self.checkNonblock(s)
s.settimeout(None) s.settimeout(None)
self.checkNonblock(s, False) self.checkNonblock(s, False)
s.settimeout(2.0) s.settimeout(2.0)
self.checkNonblock(s, timeout=2.0) self.checkNonblock(s, timeout=2.0)
s.setblocking(1) s.setblocking(1)
self.checkNonblock(s, False) self.checkNonblock(s, False)
# defaulttimeout # defaulttimeout
t = socket.getdefaulttimeout() t = socket.getdefaulttimeout()
socket.setdefaulttimeout(0.0) socket.setdefaulttimeout(0.0)
self.checkNonblock(socket.socket()) with socket.socket() as s:
self.checkNonblock(s)
socket.setdefaulttimeout(None) socket.setdefaulttimeout(None)
self.checkNonblock(socket.socket(), False) with socket.socket() as s:
self.checkNonblock(s, False)
socket.setdefaulttimeout(2.0) socket.setdefaulttimeout(2.0)
self.checkNonblock(socket.socket(), timeout=2.0) with socket.socket() as s:
self.checkNonblock(s, timeout=2.0)
socket.setdefaulttimeout(None) socket.setdefaulttimeout(None)
self.checkNonblock(socket.socket(), False) with socket.socket() as s:
self.checkNonblock(s, False)
socket.setdefaulttimeout(t) socket.setdefaulttimeout(t)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment