diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-18 22:59:00 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-18 22:59:00 (GMT) |
commit | 98b46702d2798d0db258a02f6a1854dfc5f659fd (patch) | |
tree | 13cb8a131699851391f45fd98cbf3220685ba074 /Lib/socket.py | |
parent | 8db3027e6a79836ea8c77e73be77492c68715da5 (diff) | |
download | cpython-98b46702d2798d0db258a02f6a1854dfc5f659fd.zip cpython-98b46702d2798d0db258a02f6a1854dfc5f659fd.tar.gz cpython-98b46702d2798d0db258a02f6a1854dfc5f659fd.tar.bz2 |
Issue #9854: SocketIO objects now observe the RawIOBase interface in
non-blocking mode: they return None when an operation would block (instead
of raising an exception).
Diffstat (limited to 'Lib/socket.py')
-rw-r--r-- | Lib/socket.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/Lib/socket.py b/Lib/socket.py index dbb7cca..0b19e30 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -54,6 +54,8 @@ except ImportError: errno = None EBADF = getattr(errno, 'EBADF', 9) EINTR = getattr(errno, 'EINTR', 4) +EAGAIN = getattr(errno, 'EAGAIN', 11) +EWOULDBLOCK = getattr(errno, 'EWOULDBLOCK', 11) __all__ = ["getfqdn", "create_connection"] __all__.extend(os._get_exports_list(_socket)) @@ -220,6 +222,8 @@ if hasattr(_socket, "socketpair"): return a, b +_blocking_errnos = { EAGAIN, EWOULDBLOCK } + class SocketIO(io.RawIOBase): """Raw I/O implementation for stream sockets. @@ -262,8 +266,11 @@ class SocketIO(io.RawIOBase): try: return self._sock.recv_into(b) except error as e: - if e.args[0] == EINTR: + n = e.args[0] + if n == EINTR: continue + if n in _blocking_errnos: + return None raise def write(self, b): @@ -274,7 +281,13 @@ class SocketIO(io.RawIOBase): """ self._checkClosed() self._checkWritable() - return self._sock.send(b) + try: + return self._sock.send(b) + except error as e: + # XXX what about EINTR? + if e.args[0] in _blocking_errnos: + return None + raise def readable(self): """True if the SocketIO is open for reading. |