@@ -1577,6 +1577,22 @@ def test_str_for_enums(self):
15771577 self .assertEqual (str (s .family ), 'AddressFamily.AF_INET' )
15781578 self .assertEqual (str (s .type ), 'SocketKind.SOCK_STREAM' )
15791579
1580+ def test_socket_consistent_sock_type (self ):
1581+ SOCK_NONBLOCK = getattr (socket , 'SOCK_NONBLOCK' , 0 )
1582+ SOCK_CLOEXEC = getattr (socket , 'SOCK_CLOEXEC' , 0 )
1583+ sock_type = socket .SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC
1584+
1585+ with socket .socket (socket .AF_INET , sock_type ) as s :
1586+ self .assertEqual (s .type , socket .SOCK_STREAM )
1587+ s .settimeout (1 )
1588+ self .assertEqual (s .type , socket .SOCK_STREAM )
1589+ s .settimeout (0 )
1590+ self .assertEqual (s .type , socket .SOCK_STREAM )
1591+ s .setblocking (True )
1592+ self .assertEqual (s .type , socket .SOCK_STREAM )
1593+ s .setblocking (False )
1594+ self .assertEqual (s .type , socket .SOCK_STREAM )
1595+
15801596 @unittest .skipIf (os .name == 'nt' , 'Will not work on Windows' )
15811597 def test_uknown_socket_family_repr (self ):
15821598 # Test that when created with a family that's not one of the known
@@ -1589,9 +1605,18 @@ def test_uknown_socket_family_repr(self):
15891605 # On Windows this trick won't work, so the test is skipped.
15901606 fd , path = tempfile .mkstemp ()
15911607 self .addCleanup (os .unlink , path )
1592- with socket .socket (family = 42424 , type = 13331 , fileno = fd ) as s :
1593- self .assertEqual (s .family , 42424 )
1594- self .assertEqual (s .type , 13331 )
1608+ unknown_family = max (socket .AddressFamily .__members__ .values ()) + 1
1609+
1610+ unknown_type = max (
1611+ kind
1612+ for name , kind in socket .SocketKind .__members__ .items ()
1613+ if name not in {'SOCK_NONBLOCK' , 'SOCK_CLOEXEC' }
1614+ ) + 1
1615+
1616+ with socket .socket (
1617+ family = unknown_family , type = unknown_type , fileno = fd ) as s :
1618+ self .assertEqual (s .family , unknown_family )
1619+ self .assertEqual (s .type , unknown_type )
15951620
15961621 @unittest .skipUnless (hasattr (os , 'sendfile' ), 'test needs os.sendfile()' )
15971622 def test__sendfile_use_sendfile (self ):
@@ -5084,7 +5109,7 @@ class InheritanceTest(unittest.TestCase):
50845109 def test_SOCK_CLOEXEC (self ):
50855110 with socket .socket (socket .AF_INET ,
50865111 socket .SOCK_STREAM | socket .SOCK_CLOEXEC ) as s :
5087- self .assertTrue (s .type & socket .SOCK_CLOEXEC )
5112+ self .assertEqual (s .type , socket .SOCK_STREAM )
50885113 self .assertFalse (s .get_inheritable ())
50895114
50905115 def test_default_inheritable (self ):
@@ -5149,11 +5174,15 @@ def test_socketpair(self):
51495174class NonblockConstantTest (unittest .TestCase ):
51505175 def checkNonblock (self , s , nonblock = True , timeout = 0.0 ):
51515176 if nonblock :
5152- self .assertTrue (s .type & socket .SOCK_NONBLOCK )
5177+ self .assertEqual (s .type , socket .SOCK_STREAM )
51535178 self .assertEqual (s .gettimeout (), timeout )
5179+ self .assertTrue (
5180+ fcntl .fcntl (s , fcntl .F_GETFL , os .O_NONBLOCK ) & os .O_NONBLOCK )
51545181 else :
5155- self .assertFalse (s .type & socket .SOCK_NONBLOCK )
5182+ self .assertEqual (s .type , socket .SOCK_STREAM )
51565183 self .assertEqual (s .gettimeout (), None )
5184+ self .assertFalse (
5185+ fcntl .fcntl (s , fcntl .F_GETFL , os .O_NONBLOCK ) & os .O_NONBLOCK )
51575186
51585187 @support .requires_linux_version (2 , 6 , 28 )
51595188 def test_SOCK_NONBLOCK (self ):
0 commit comments