diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-06-29 10:12:22 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-06-29 10:12:22 (GMT) |
commit | 34eeed42901666fce099947f93dfdfc05411f286 (patch) | |
tree | 26bd3fea92143532a1a7fd725d08733819a779d1 /Lib/socketserver.py | |
parent | 7acc3486d64c7625d9a366c5834028aa81ae162c (diff) | |
download | cpython-34eeed42901666fce099947f93dfdfc05411f286.zip cpython-34eeed42901666fce099947f93dfdfc05411f286.tar.gz cpython-34eeed42901666fce099947f93dfdfc05411f286.tar.bz2 |
Issue #26721: Change StreamRequestHandler.wfile to BufferedIOBase
Diffstat (limited to 'Lib/socketserver.py')
-rw-r--r-- | Lib/socketserver.py | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/Lib/socketserver.py b/Lib/socketserver.py index c6d38c7..41a3766 100644 --- a/Lib/socketserver.py +++ b/Lib/socketserver.py @@ -132,6 +132,7 @@ try: import threading except ImportError: import dummy_threading as threading +from io import BufferedIOBase from time import monotonic as time __all__ = ["BaseServer", "TCPServer", "UDPServer", @@ -743,7 +744,10 @@ class StreamRequestHandler(BaseRequestHandler): self.connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True) self.rfile = self.connection.makefile('rb', self.rbufsize) - self.wfile = self.connection.makefile('wb', self.wbufsize) + if self.wbufsize == 0: + self.wfile = _SocketWriter(self.connection) + else: + self.wfile = self.connection.makefile('wb', self.wbufsize) def finish(self): if not self.wfile.closed: @@ -756,6 +760,24 @@ class StreamRequestHandler(BaseRequestHandler): self.wfile.close() self.rfile.close() +class _SocketWriter(BufferedIOBase): + """Simple writable BufferedIOBase implementation for a socket + + Does not hold data in a buffer, avoiding any need to call flush().""" + + def __init__(self, sock): + self._sock = sock + + def writable(self): + return True + + def write(self, b): + self._sock.sendall(b) + with memoryview(b) as view: + return view.nbytes + + def fileno(self): + return self._sock.fileno() class DatagramRequestHandler(BaseRequestHandler): |