@@ -98,6 +98,12 @@ def _have_socket_vsock():
9898 ret = get_cid () is not None
9999 return ret
100100
101+
102+ def _is_fd_in_blocking_mode (sock ):
103+ return not bool (
104+ fcntl .fcntl (sock , fcntl .F_GETFL , os .O_NONBLOCK ) & os .O_NONBLOCK )
105+
106+
101107HAVE_SOCKET_CAN = _have_socket_can ()
102108
103109HAVE_SOCKET_CAN_ISOTP = _have_socket_can_isotp ()
@@ -4079,8 +4085,44 @@ def testSetBlocking(self):
40794085 # Testing whether set blocking works
40804086 self .serv .setblocking (True )
40814087 self .assertIsNone (self .serv .gettimeout ())
4088+ self .assertTrue (self .serv .getblocking ())
4089+ if fcntl :
4090+ self .assertTrue (_is_fd_in_blocking_mode (self .serv ))
4091+
40824092 self .serv .setblocking (False )
40834093 self .assertEqual (self .serv .gettimeout (), 0.0 )
4094+ self .assertFalse (self .serv .getblocking ())
4095+ if fcntl :
4096+ self .assertFalse (_is_fd_in_blocking_mode (self .serv ))
4097+
4098+ self .serv .settimeout (None )
4099+ self .assertTrue (self .serv .getblocking ())
4100+ if fcntl :
4101+ self .assertTrue (_is_fd_in_blocking_mode (self .serv ))
4102+
4103+ self .serv .settimeout (0 )
4104+ self .assertFalse (self .serv .getblocking ())
4105+ self .assertEqual (self .serv .gettimeout (), 0 )
4106+ if fcntl :
4107+ self .assertFalse (_is_fd_in_blocking_mode (self .serv ))
4108+
4109+ self .serv .settimeout (10 )
4110+ self .assertTrue (self .serv .getblocking ())
4111+ self .assertEqual (self .serv .gettimeout (), 10 )
4112+ if fcntl :
4113+ # When a Python socket has a non-zero timeout, it's
4114+ # switched internally to a non-blocking mode.
4115+ # Later, sock.sendall(), sock.recv(), and other socket
4116+ # operations use a `select()` call and handle EWOULDBLOCK/EGAIN
4117+ # on all socket operations. That's how timeouts are
4118+ # enforced.
4119+ self .assertFalse (_is_fd_in_blocking_mode (self .serv ))
4120+
4121+ self .serv .settimeout (0 )
4122+ self .assertFalse (self .serv .getblocking ())
4123+ if fcntl :
4124+ self .assertFalse (_is_fd_in_blocking_mode (self .serv ))
4125+
40844126 start = time .time ()
40854127 try :
40864128 self .serv .accept ()
@@ -4113,6 +4155,8 @@ def testInitNonBlocking(self):
41134155 self .serv .close ()
41144156 self .serv = socket .socket (socket .AF_INET , socket .SOCK_STREAM |
41154157 socket .SOCK_NONBLOCK )
4158+ self .assertFalse (self .serv .getblocking ())
4159+ self .assertEqual (self .serv .gettimeout (), 0 )
41164160 self .port = support .bind_port (self .serv )
41174161 self .serv .listen ()
41184162 # actual testing
@@ -5190,11 +5234,24 @@ def checkNonblock(self, s, nonblock=True, timeout=0.0):
51905234 self .assertEqual (s .gettimeout (), timeout )
51915235 self .assertTrue (
51925236 fcntl .fcntl (s , fcntl .F_GETFL , os .O_NONBLOCK ) & os .O_NONBLOCK )
5237+ if timeout == 0 :
5238+ # timeout == 0: means that getblocking() must be False.
5239+ self .assertFalse (s .getblocking ())
5240+ else :
5241+ # If timeout > 0, the socket will be in a "blocking" mode
5242+ # from the standpoint of the Python API. For Python socket
5243+ # object, "blocking" means that operations like 'sock.recv()'
5244+ # will block. Internally, file descriptors for
5245+ # "blocking" Python sockets *with timeouts* are in a
5246+ # *non-blocking* mode, and 'sock.recv()' uses 'select()'
5247+ # and handles EWOULDBLOCK/EAGAIN to enforce the timeout.
5248+ self .assertTrue (s .getblocking ())
51935249 else :
51945250 self .assertEqual (s .type , socket .SOCK_STREAM )
51955251 self .assertEqual (s .gettimeout (), None )
51965252 self .assertFalse (
51975253 fcntl .fcntl (s , fcntl .F_GETFL , os .O_NONBLOCK ) & os .O_NONBLOCK )
5254+ self .assertTrue (s .getblocking ())
51985255
51995256 @support .requires_linux_version (2 , 6 , 28 )
52005257 def test_SOCK_NONBLOCK (self ):
@@ -5204,15 +5261,15 @@ def test_SOCK_NONBLOCK(self):
52045261 socket .SOCK_STREAM | socket .SOCK_NONBLOCK ) as s :
52055262 self .checkNonblock (s )
52065263 s .setblocking (1 )
5207- self .checkNonblock (s , False )
5264+ self .checkNonblock (s , nonblock = False )
52085265 s .setblocking (0 )
52095266 self .checkNonblock (s )
52105267 s .settimeout (None )
5211- self .checkNonblock (s , False )
5268+ self .checkNonblock (s , nonblock = False )
52125269 s .settimeout (2.0 )
52135270 self .checkNonblock (s , timeout = 2.0 )
52145271 s .setblocking (1 )
5215- self .checkNonblock (s , False )
5272+ self .checkNonblock (s , nonblock = False )
52165273 # defaulttimeout
52175274 t = socket .getdefaulttimeout ()
52185275 socket .setdefaulttimeout (0.0 )
0 commit comments