@@ -1678,25 +1678,25 @@ class TestLinuxAbstractNamespace(unittest.TestCase):
16781678
16791679 def testLinuxAbstractNamespace (self ):
16801680 address = b"\x00 python-test-hello\x00 \xff "
1681- s1 = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
1682- s1 .bind (address )
1683- s1 .listen (1 )
1684- s2 = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
1685- s2 .connect (s1 .getsockname ())
1686- s1 .accept ()
1687- self .assertEqual (s1 .getsockname (), address )
1688- self .assertEqual (s2 .getpeername (), address )
1681+ with socket .socket (socket .AF_UNIX , socket .SOCK_STREAM ) as s1 :
1682+ s1 .bind (address )
1683+ s1 .listen (1 )
1684+ with socket .socket (socket .AF_UNIX , socket .SOCK_STREAM ) as s2 :
1685+ s2 .connect (s1 .getsockname ())
1686+ with s1 .accept ()[ 0 ] as s3 :
1687+ self .assertEqual (s1 .getsockname (), address )
1688+ self .assertEqual (s2 .getpeername (), address )
16891689
16901690 def testMaxName (self ):
16911691 address = b"\x00 " + b"h" * (self .UNIX_PATH_MAX - 1 )
1692- s = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
1693- s .bind (address )
1694- self .assertEqual (s .getsockname (), address )
1692+ with socket .socket (socket .AF_UNIX , socket .SOCK_STREAM ) as s :
1693+ s .bind (address )
1694+ self .assertEqual (s .getsockname (), address )
16951695
16961696 def testNameOverflow (self ):
16971697 address = "\x00 " + "h" * self .UNIX_PATH_MAX
1698- s = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
1699- self .assertRaises (socket .error , s .bind , address )
1698+ with socket .socket (socket .AF_UNIX , socket .SOCK_STREAM ) as s :
1699+ self .assertRaises (socket .error , s .bind , address )
17001700
17011701
17021702@unittest .skipUnless (thread , 'Threading required for this test.' )
@@ -1898,10 +1898,10 @@ def test_SOCK_CLOEXEC(self):
18981898 if v < (2 , 6 , 28 ):
18991899 self .skipTest ("Linux kernel 2.6.28 or higher required, not %s"
19001900 % "." .join (map (str , v )))
1901- s = socket .socket (socket .AF_INET ,
1902- socket .SOCK_STREAM | socket .SOCK_CLOEXEC )
1903- self .assertTrue (s .type & socket .SOCK_CLOEXEC )
1904- self .assertTrue (fcntl .fcntl (s , fcntl .F_GETFD ) & fcntl .FD_CLOEXEC )
1901+ with socket .socket (socket .AF_INET ,
1902+ socket .SOCK_STREAM | socket .SOCK_CLOEXEC ) as s :
1903+ self .assertTrue (s .type & socket .SOCK_CLOEXEC )
1904+ self .assertTrue (fcntl .fcntl (s , fcntl .F_GETFD ) & fcntl .FD_CLOEXEC )
19051905
19061906
19071907@unittest .skipUnless (hasattr (socket , "SOCK_NONBLOCK" ),
@@ -1922,29 +1922,33 @@ def test_SOCK_NONBLOCK(self):
19221922 % "." .join (map (str , v )))
19231923 # a lot of it seems silly and redundant, but I wanted to test that
19241924 # changing back and forth worked ok
1925- s = socket .socket (socket .AF_INET ,
1926- socket .SOCK_STREAM | socket .SOCK_NONBLOCK )
1927- self .checkNonblock (s )
1928- s .setblocking (1 )
1929- self .checkNonblock (s , False )
1930- s .setblocking (0 )
1931- self .checkNonblock (s )
1932- s .settimeout (None )
1933- self .checkNonblock (s , False )
1934- s .settimeout (2.0 )
1935- self .checkNonblock (s , timeout = 2.0 )
1936- s .setblocking (1 )
1937- self .checkNonblock (s , False )
1925+ with socket .socket (socket .AF_INET ,
1926+ socket .SOCK_STREAM | socket .SOCK_NONBLOCK ) as s :
1927+ self .checkNonblock (s )
1928+ s .setblocking (1 )
1929+ self .checkNonblock (s , False )
1930+ s .setblocking (0 )
1931+ self .checkNonblock (s )
1932+ s .settimeout (None )
1933+ self .checkNonblock (s , False )
1934+ s .settimeout (2.0 )
1935+ self .checkNonblock (s , timeout = 2.0 )
1936+ s .setblocking (1 )
1937+ self .checkNonblock (s , False )
19381938 # defaulttimeout
19391939 t = socket .getdefaulttimeout ()
19401940 socket .setdefaulttimeout (0.0 )
1941- self .checkNonblock (socket .socket ())
1941+ with socket .socket () as s :
1942+ self .checkNonblock (s )
19421943 socket .setdefaulttimeout (None )
1943- self .checkNonblock (socket .socket (), False )
1944+ with socket .socket () as s :
1945+ self .checkNonblock (s , False )
19441946 socket .setdefaulttimeout (2.0 )
1945- self .checkNonblock (socket .socket (), timeout = 2.0 )
1947+ with socket .socket () as s :
1948+ self .checkNonblock (s , timeout = 2.0 )
19461949 socket .setdefaulttimeout (None )
1947- self .checkNonblock (socket .socket (), False )
1950+ with socket .socket () as s :
1951+ self .checkNonblock (s , False )
19481952 socket .setdefaulttimeout (t )
19491953
19501954
0 commit comments