jsoncomm: add blocking property to Socket

Add a new `blocking` property to get and set the blocking state
of the underlying socket. In Python this is tied to the timeout
setting of the `socket.Socket`, i.e. non-blocking means having
any timeout specified, including "0" for not waiting at all.
Blocking means having a timeout value of `None`.
The getter is emulating the logic of `Socket.getblocking`, which
was added in 3.7, and we need to stay compatible with 3.6.
The logic is implemented in `Modules/socketmodule.c` in Python.
This commit is contained in:
Christian Kellner 2020-07-25 14:46:41 +02:00 committed by Tom Gundersen
parent 06db7834f9
commit 2da98a57d7

View file

@ -114,6 +114,21 @@ class Socket(contextlib.AbstractContextManager):
self.close()
return False
@property
def blocking(self):
"""Get the current blocking mode of the socket.
This is related to the socket's timeout, i.e. if no time out is set
the socket is in blocking mode; otherwise it is non-blocking.
"""
timeout = self._socket.gettimeout()
return timeout is not None
@blocking.setter
def blocking(self, value: bool):
"""Set the blocking mode of the socket."""
self._socket.setblocking(value)
def close(self):
"""Close Socket