Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 1 | """Stream-related things.""" |
| 2 | |
Guido van Rossum | 49c96fb | 2013-11-25 15:07:18 -0800 | [diff] [blame] | 3 | __all__ = ['StreamReader', 'StreamWriter', 'StreamReaderProtocol', |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 4 | 'open_connection', 'start_server', |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 5 | 'IncompleteReadError', |
Guido van Rossum | 1540b16 | 2013-11-19 11:43:38 -0800 | [diff] [blame] | 6 | ] |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 7 | |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 8 | import socket |
| 9 | |
Guido van Rossum | e3e786c | 2014-02-18 10:24:30 -0800 | [diff] [blame] | 10 | if hasattr(socket, 'AF_UNIX'): |
| 11 | __all__.extend(['open_unix_connection', 'start_unix_server']) |
| 12 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 13 | from . import coroutines |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 14 | from . import events |
| 15 | from . import futures |
| 16 | from . import protocols |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 17 | from .coroutines import coroutine |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 18 | from .log import logger |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 19 | |
| 20 | |
| 21 | _DEFAULT_LIMIT = 2**16 |
| 22 | |
Guido van Rossum | a849be9 | 2014-01-30 16:05:28 -0800 | [diff] [blame] | 23 | |
Victor Stinner | 8dffc45 | 2014-01-25 15:32:06 +0100 | [diff] [blame] | 24 | class IncompleteReadError(EOFError): |
| 25 | """ |
| 26 | Incomplete read error. Attributes: |
| 27 | |
| 28 | - partial: read bytes string before the end of stream was reached |
| 29 | - expected: total number of expected bytes |
| 30 | """ |
| 31 | def __init__(self, partial, expected): |
| 32 | EOFError.__init__(self, "%s bytes read on a total of %s expected bytes" |
| 33 | % (len(partial), expected)) |
| 34 | self.partial = partial |
| 35 | self.expected = expected |
| 36 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 37 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 38 | @coroutine |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 39 | def open_connection(host=None, port=None, *, |
| 40 | loop=None, limit=_DEFAULT_LIMIT, **kwds): |
| 41 | """A wrapper for create_connection() returning a (reader, writer) pair. |
| 42 | |
| 43 | The reader returned is a StreamReader instance; the writer is a |
Victor Stinner | 183e347 | 2014-01-23 17:40:03 +0100 | [diff] [blame] | 44 | StreamWriter instance. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 45 | |
| 46 | The arguments are all the usual arguments to create_connection() |
| 47 | except protocol_factory; most common are positional host and port, |
| 48 | with various optional keyword arguments following. |
| 49 | |
| 50 | Additional optional keyword arguments are loop (to set the event loop |
| 51 | instance to use) and limit (to set the buffer limit passed to the |
| 52 | StreamReader). |
| 53 | |
| 54 | (If you want to customize the StreamReader and/or |
| 55 | StreamReaderProtocol classes, just copy the code -- there's |
| 56 | really nothing special here except some convenience.) |
| 57 | """ |
| 58 | if loop is None: |
| 59 | loop = events.get_event_loop() |
| 60 | reader = StreamReader(limit=limit, loop=loop) |
Guido van Rossum | efef9d3 | 2014-01-10 13:26:38 -0800 | [diff] [blame] | 61 | protocol = StreamReaderProtocol(reader, loop=loop) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 62 | transport, _ = yield from loop.create_connection( |
| 63 | lambda: protocol, host, port, **kwds) |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 64 | writer = StreamWriter(transport, protocol, reader, loop) |
| 65 | return reader, writer |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 66 | |
| 67 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 68 | @coroutine |
Guido van Rossum | 1540b16 | 2013-11-19 11:43:38 -0800 | [diff] [blame] | 69 | def start_server(client_connected_cb, host=None, port=None, *, |
| 70 | loop=None, limit=_DEFAULT_LIMIT, **kwds): |
| 71 | """Start a socket server, call back for each client connected. |
| 72 | |
| 73 | The first parameter, `client_connected_cb`, takes two parameters: |
| 74 | client_reader, client_writer. client_reader is a StreamReader |
| 75 | object, while client_writer is a StreamWriter object. This |
| 76 | parameter can either be a plain callback function or a coroutine; |
| 77 | if it is a coroutine, it will be automatically converted into a |
| 78 | Task. |
| 79 | |
| 80 | The rest of the arguments are all the usual arguments to |
| 81 | loop.create_server() except protocol_factory; most common are |
| 82 | positional host and port, with various optional keyword arguments |
| 83 | following. The return value is the same as loop.create_server(). |
| 84 | |
| 85 | Additional optional keyword arguments are loop (to set the event loop |
| 86 | instance to use) and limit (to set the buffer limit passed to the |
| 87 | StreamReader). |
| 88 | |
| 89 | The return value is the same as loop.create_server(), i.e. a |
| 90 | Server object which can be used to stop the service. |
| 91 | """ |
| 92 | if loop is None: |
| 93 | loop = events.get_event_loop() |
| 94 | |
| 95 | def factory(): |
| 96 | reader = StreamReader(limit=limit, loop=loop) |
| 97 | protocol = StreamReaderProtocol(reader, client_connected_cb, |
| 98 | loop=loop) |
| 99 | return protocol |
| 100 | |
| 101 | return (yield from loop.create_server(factory, host, port, **kwds)) |
| 102 | |
| 103 | |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 104 | if hasattr(socket, 'AF_UNIX'): |
| 105 | # UNIX Domain Sockets are supported on this platform |
| 106 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 107 | @coroutine |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 108 | def open_unix_connection(path=None, *, |
| 109 | loop=None, limit=_DEFAULT_LIMIT, **kwds): |
| 110 | """Similar to `open_connection` but works with UNIX Domain Sockets.""" |
| 111 | if loop is None: |
| 112 | loop = events.get_event_loop() |
| 113 | reader = StreamReader(limit=limit, loop=loop) |
| 114 | protocol = StreamReaderProtocol(reader, loop=loop) |
| 115 | transport, _ = yield from loop.create_unix_connection( |
| 116 | lambda: protocol, path, **kwds) |
| 117 | writer = StreamWriter(transport, protocol, reader, loop) |
| 118 | return reader, writer |
| 119 | |
| 120 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 121 | @coroutine |
Yury Selivanov | b057c52 | 2014-02-18 12:15:06 -0500 | [diff] [blame] | 122 | def start_unix_server(client_connected_cb, path=None, *, |
| 123 | loop=None, limit=_DEFAULT_LIMIT, **kwds): |
| 124 | """Similar to `start_server` but works with UNIX Domain Sockets.""" |
| 125 | if loop is None: |
| 126 | loop = events.get_event_loop() |
| 127 | |
| 128 | def factory(): |
| 129 | reader = StreamReader(limit=limit, loop=loop) |
| 130 | protocol = StreamReaderProtocol(reader, client_connected_cb, |
| 131 | loop=loop) |
| 132 | return protocol |
| 133 | |
| 134 | return (yield from loop.create_unix_server(factory, path, **kwds)) |
| 135 | |
| 136 | |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 137 | class FlowControlMixin(protocols.Protocol): |
| 138 | """Reusable flow control logic for StreamWriter.drain(). |
| 139 | |
| 140 | This implements the protocol methods pause_writing(), |
| 141 | resume_reading() and connection_lost(). If the subclass overrides |
| 142 | these it must call the super methods. |
| 143 | |
Victor Stinner | 31e7bfa | 2014-07-22 12:03:40 +0200 | [diff] [blame] | 144 | StreamWriter.drain() must wait for _drain_helper() coroutine. |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 145 | """ |
| 146 | |
| 147 | def __init__(self, loop=None): |
Victor Stinner | 70db9e4 | 2015-01-09 21:32:05 +0100 | [diff] [blame] | 148 | if loop is None: |
| 149 | self._loop = events.get_event_loop() |
| 150 | else: |
| 151 | self._loop = loop |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 152 | self._paused = False |
| 153 | self._drain_waiter = None |
Victor Stinner | 31e7bfa | 2014-07-22 12:03:40 +0200 | [diff] [blame] | 154 | self._connection_lost = False |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 155 | |
| 156 | def pause_writing(self): |
| 157 | assert not self._paused |
| 158 | self._paused = True |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 159 | if self._loop.get_debug(): |
| 160 | logger.debug("%r pauses writing", self) |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 161 | |
| 162 | def resume_writing(self): |
| 163 | assert self._paused |
| 164 | self._paused = False |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 165 | if self._loop.get_debug(): |
| 166 | logger.debug("%r resumes writing", self) |
| 167 | |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 168 | waiter = self._drain_waiter |
| 169 | if waiter is not None: |
| 170 | self._drain_waiter = None |
| 171 | if not waiter.done(): |
| 172 | waiter.set_result(None) |
| 173 | |
| 174 | def connection_lost(self, exc): |
Victor Stinner | 31e7bfa | 2014-07-22 12:03:40 +0200 | [diff] [blame] | 175 | self._connection_lost = True |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 176 | # Wake up the writer if currently paused. |
| 177 | if not self._paused: |
| 178 | return |
| 179 | waiter = self._drain_waiter |
| 180 | if waiter is None: |
| 181 | return |
| 182 | self._drain_waiter = None |
| 183 | if waiter.done(): |
| 184 | return |
| 185 | if exc is None: |
| 186 | waiter.set_result(None) |
| 187 | else: |
| 188 | waiter.set_exception(exc) |
| 189 | |
Victor Stinner | 31e7bfa | 2014-07-22 12:03:40 +0200 | [diff] [blame] | 190 | @coroutine |
| 191 | def _drain_helper(self): |
| 192 | if self._connection_lost: |
| 193 | raise ConnectionResetError('Connection lost') |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 194 | if not self._paused: |
Victor Stinner | 31e7bfa | 2014-07-22 12:03:40 +0200 | [diff] [blame] | 195 | return |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 196 | waiter = self._drain_waiter |
| 197 | assert waiter is None or waiter.cancelled() |
| 198 | waiter = futures.Future(loop=self._loop) |
| 199 | self._drain_waiter = waiter |
Victor Stinner | 31e7bfa | 2014-07-22 12:03:40 +0200 | [diff] [blame] | 200 | yield from waiter |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 201 | |
| 202 | |
| 203 | class StreamReaderProtocol(FlowControlMixin, protocols.Protocol): |
| 204 | """Helper class to adapt between Protocol and StreamReader. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 205 | |
| 206 | (This is a helper class instead of making StreamReader itself a |
| 207 | Protocol subclass, because the StreamReader has other potential |
| 208 | uses, and to prevent the user of the StreamReader to accidentally |
| 209 | call inappropriate methods of the protocol.) |
| 210 | """ |
| 211 | |
Guido van Rossum | 1540b16 | 2013-11-19 11:43:38 -0800 | [diff] [blame] | 212 | def __init__(self, stream_reader, client_connected_cb=None, loop=None): |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 213 | super().__init__(loop=loop) |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 214 | self._stream_reader = stream_reader |
Guido van Rossum | 1540b16 | 2013-11-19 11:43:38 -0800 | [diff] [blame] | 215 | self._stream_writer = None |
Guido van Rossum | 1540b16 | 2013-11-19 11:43:38 -0800 | [diff] [blame] | 216 | self._client_connected_cb = client_connected_cb |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 217 | |
| 218 | def connection_made(self, transport): |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 219 | self._stream_reader.set_transport(transport) |
Guido van Rossum | 1540b16 | 2013-11-19 11:43:38 -0800 | [diff] [blame] | 220 | if self._client_connected_cb is not None: |
| 221 | self._stream_writer = StreamWriter(transport, self, |
| 222 | self._stream_reader, |
| 223 | self._loop) |
| 224 | res = self._client_connected_cb(self._stream_reader, |
| 225 | self._stream_writer) |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 226 | if coroutines.iscoroutine(res): |
Victor Stinner | 896a25a | 2014-07-08 11:29:25 +0200 | [diff] [blame] | 227 | self._loop.create_task(res) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 228 | |
| 229 | def connection_lost(self, exc): |
| 230 | if exc is None: |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 231 | self._stream_reader.feed_eof() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 232 | else: |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 233 | self._stream_reader.set_exception(exc) |
Guido van Rossum | 4d62d0b | 2014-01-29 14:24:45 -0800 | [diff] [blame] | 234 | super().connection_lost(exc) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 235 | |
| 236 | def data_received(self, data): |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 237 | self._stream_reader.feed_data(data) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 238 | |
| 239 | def eof_received(self): |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 240 | self._stream_reader.feed_eof() |
| 241 | |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 242 | |
| 243 | class StreamWriter: |
| 244 | """Wraps a Transport. |
| 245 | |
| 246 | This exposes write(), writelines(), [can_]write_eof(), |
| 247 | get_extra_info() and close(). It adds drain() which returns an |
| 248 | optional Future on which you can wait for flow control. It also |
Guido van Rossum | efef9d3 | 2014-01-10 13:26:38 -0800 | [diff] [blame] | 249 | adds a transport property which references the Transport |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 250 | directly. |
| 251 | """ |
| 252 | |
| 253 | def __init__(self, transport, protocol, reader, loop): |
| 254 | self._transport = transport |
| 255 | self._protocol = protocol |
Victor Stinner | 31e7bfa | 2014-07-22 12:03:40 +0200 | [diff] [blame] | 256 | # drain() expects that the reader has a exception() method |
| 257 | assert reader is None or isinstance(reader, StreamReader) |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 258 | self._reader = reader |
| 259 | self._loop = loop |
| 260 | |
Victor Stinner | acdb782 | 2014-07-14 18:33:40 +0200 | [diff] [blame] | 261 | def __repr__(self): |
| 262 | info = [self.__class__.__name__, 'transport=%r' % self._transport] |
| 263 | if self._reader is not None: |
| 264 | info.append('reader=%r' % self._reader) |
| 265 | return '<%s>' % ' '.join(info) |
| 266 | |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 267 | @property |
| 268 | def transport(self): |
| 269 | return self._transport |
| 270 | |
| 271 | def write(self, data): |
| 272 | self._transport.write(data) |
| 273 | |
| 274 | def writelines(self, data): |
| 275 | self._transport.writelines(data) |
| 276 | |
| 277 | def write_eof(self): |
| 278 | return self._transport.write_eof() |
| 279 | |
| 280 | def can_write_eof(self): |
| 281 | return self._transport.can_write_eof() |
| 282 | |
| 283 | def close(self): |
| 284 | return self._transport.close() |
| 285 | |
| 286 | def get_extra_info(self, name, default=None): |
| 287 | return self._transport.get_extra_info(name, default) |
| 288 | |
Victor Stinner | 31e7bfa | 2014-07-22 12:03:40 +0200 | [diff] [blame] | 289 | @coroutine |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 290 | def drain(self): |
Victor Stinner | 31e7bfa | 2014-07-22 12:03:40 +0200 | [diff] [blame] | 291 | """Flush the write buffer. |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 292 | |
| 293 | The intended use is to write |
| 294 | |
| 295 | w.write(data) |
| 296 | yield from w.drain() |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 297 | """ |
Victor Stinner | 31e7bfa | 2014-07-22 12:03:40 +0200 | [diff] [blame] | 298 | if self._reader is not None: |
| 299 | exc = self._reader.exception() |
| 300 | if exc is not None: |
| 301 | raise exc |
| 302 | yield from self._protocol._drain_helper() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 303 | |
| 304 | |
| 305 | class StreamReader: |
| 306 | |
| 307 | def __init__(self, limit=_DEFAULT_LIMIT, loop=None): |
| 308 | # The line length limit is a security feature; |
| 309 | # it also doubles as half the buffer limit. |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 310 | self._limit = limit |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 311 | if loop is None: |
Victor Stinner | 70db9e4 | 2015-01-09 21:32:05 +0100 | [diff] [blame] | 312 | self._loop = events.get_event_loop() |
| 313 | else: |
| 314 | self._loop = loop |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 315 | self._buffer = bytearray() |
Victor Stinner | c2c12e4 | 2015-01-14 00:53:37 +0100 | [diff] [blame^] | 316 | self._eof = False # Whether we're done. |
| 317 | self._waiter = None # A future used by _wait_for_data() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 318 | self._exception = None |
| 319 | self._transport = None |
| 320 | self._paused = False |
| 321 | |
| 322 | def exception(self): |
| 323 | return self._exception |
| 324 | |
| 325 | def set_exception(self, exc): |
| 326 | self._exception = exc |
| 327 | |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 328 | waiter = self._waiter |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 329 | if waiter is not None: |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 330 | self._waiter = None |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 331 | if not waiter.cancelled(): |
| 332 | waiter.set_exception(exc) |
| 333 | |
Victor Stinner | c2c12e4 | 2015-01-14 00:53:37 +0100 | [diff] [blame^] | 334 | def _wakeup_waiter(self): |
| 335 | """Wakeup read() or readline() function waiting for data or EOF.""" |
| 336 | waiter = self._waiter |
| 337 | if waiter is not None: |
| 338 | self._waiter = None |
| 339 | if not waiter.cancelled(): |
| 340 | waiter.set_result(None) |
| 341 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 342 | def set_transport(self, transport): |
| 343 | assert self._transport is None, 'Transport already set' |
| 344 | self._transport = transport |
| 345 | |
| 346 | def _maybe_resume_transport(self): |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 347 | if self._paused and len(self._buffer) <= self._limit: |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 348 | self._paused = False |
Guido van Rossum | 57497ad | 2013-10-18 07:58:20 -0700 | [diff] [blame] | 349 | self._transport.resume_reading() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 350 | |
| 351 | def feed_eof(self): |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 352 | self._eof = True |
Victor Stinner | c2c12e4 | 2015-01-14 00:53:37 +0100 | [diff] [blame^] | 353 | self._wakeup_waiter() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 354 | |
Yury Selivanov | f0020f5 | 2014-02-06 00:14:30 -0500 | [diff] [blame] | 355 | def at_eof(self): |
| 356 | """Return True if the buffer is empty and 'feed_eof' was called.""" |
| 357 | return self._eof and not self._buffer |
| 358 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 359 | def feed_data(self, data): |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 360 | assert not self._eof, 'feed_data after feed_eof' |
| 361 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 362 | if not data: |
| 363 | return |
| 364 | |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 365 | self._buffer.extend(data) |
Victor Stinner | c2c12e4 | 2015-01-14 00:53:37 +0100 | [diff] [blame^] | 366 | self._wakeup_waiter() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 367 | |
| 368 | if (self._transport is not None and |
| 369 | not self._paused and |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 370 | len(self._buffer) > 2*self._limit): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 371 | try: |
Guido van Rossum | 57497ad | 2013-10-18 07:58:20 -0700 | [diff] [blame] | 372 | self._transport.pause_reading() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 373 | except NotImplementedError: |
| 374 | # The transport can't be paused. |
| 375 | # We'll just have to buffer all data. |
| 376 | # Forget the transport so we don't keep trying. |
| 377 | self._transport = None |
| 378 | else: |
| 379 | self._paused = True |
| 380 | |
Victor Stinner | c2c12e4 | 2015-01-14 00:53:37 +0100 | [diff] [blame^] | 381 | def _wait_for_data(self, func_name): |
| 382 | """Wait until feed_data() or feed_eof() is called.""" |
Victor Stinner | 183e347 | 2014-01-23 17:40:03 +0100 | [diff] [blame] | 383 | # StreamReader uses a future to link the protocol feed_data() method |
| 384 | # to a read coroutine. Running two read coroutines at the same time |
| 385 | # would have an unexpected behaviour. It would not possible to know |
| 386 | # which coroutine would get the next data. |
| 387 | if self._waiter is not None: |
| 388 | raise RuntimeError('%s() called while another coroutine is ' |
| 389 | 'already waiting for incoming data' % func_name) |
Victor Stinner | c2c12e4 | 2015-01-14 00:53:37 +0100 | [diff] [blame^] | 390 | |
| 391 | self._waiter = futures.Future(loop=self._loop) |
| 392 | try: |
| 393 | yield from self._waiter |
| 394 | finally: |
| 395 | self._waiter = None |
Victor Stinner | 183e347 | 2014-01-23 17:40:03 +0100 | [diff] [blame] | 396 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 397 | @coroutine |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 398 | def readline(self): |
| 399 | if self._exception is not None: |
| 400 | raise self._exception |
| 401 | |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 402 | line = bytearray() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 403 | not_enough = True |
| 404 | |
| 405 | while not_enough: |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 406 | while self._buffer and not_enough: |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 407 | ichar = self._buffer.find(b'\n') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 408 | if ichar < 0: |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 409 | line.extend(self._buffer) |
| 410 | self._buffer.clear() |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 411 | else: |
| 412 | ichar += 1 |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 413 | line.extend(self._buffer[:ichar]) |
| 414 | del self._buffer[:ichar] |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 415 | not_enough = False |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 416 | |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 417 | if len(line) > self._limit: |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 418 | self._maybe_resume_transport() |
| 419 | raise ValueError('Line is too long') |
| 420 | |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 421 | if self._eof: |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 422 | break |
| 423 | |
| 424 | if not_enough: |
Victor Stinner | c2c12e4 | 2015-01-14 00:53:37 +0100 | [diff] [blame^] | 425 | yield from self._wait_for_data('readline') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 426 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 427 | self._maybe_resume_transport() |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 428 | return bytes(line) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 429 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 430 | @coroutine |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 431 | def read(self, n=-1): |
| 432 | if self._exception is not None: |
| 433 | raise self._exception |
| 434 | |
| 435 | if not n: |
| 436 | return b'' |
| 437 | |
| 438 | if n < 0: |
Guido van Rossum | bf88ffb | 2014-05-12 10:04:37 -0700 | [diff] [blame] | 439 | # This used to just loop creating a new waiter hoping to |
| 440 | # collect everything in self._buffer, but that would |
| 441 | # deadlock if the subprocess sends more than self.limit |
| 442 | # bytes. So just call self.read(self._limit) until EOF. |
| 443 | blocks = [] |
| 444 | while True: |
| 445 | block = yield from self.read(self._limit) |
| 446 | if not block: |
| 447 | break |
| 448 | blocks.append(block) |
| 449 | return b''.join(blocks) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 450 | else: |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 451 | if not self._buffer and not self._eof: |
Victor Stinner | c2c12e4 | 2015-01-14 00:53:37 +0100 | [diff] [blame^] | 452 | yield from self._wait_for_data('read') |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 453 | |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 454 | if n < 0 or len(self._buffer) <= n: |
| 455 | data = bytes(self._buffer) |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 456 | self._buffer.clear() |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 457 | else: |
| 458 | # n > 0 and len(self._buffer) > n |
| 459 | data = bytes(self._buffer[:n]) |
| 460 | del self._buffer[:n] |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 461 | |
Yury Selivanov | e694c97 | 2014-02-05 18:11:13 -0500 | [diff] [blame] | 462 | self._maybe_resume_transport() |
| 463 | return data |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 464 | |
Victor Stinner | f951d28 | 2014-06-29 00:46:45 +0200 | [diff] [blame] | 465 | @coroutine |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 466 | def readexactly(self, n): |
| 467 | if self._exception is not None: |
| 468 | raise self._exception |
| 469 | |
Guido van Rossum | 3845521 | 2014-01-06 16:09:18 -0800 | [diff] [blame] | 470 | # There used to be "optimized" code here. It created its own |
| 471 | # Future and waited until self._buffer had at least the n |
| 472 | # bytes, then called read(n). Unfortunately, this could pause |
| 473 | # the transport if the argument was larger than the pause |
| 474 | # limit (which is twice self._limit). So now we just read() |
| 475 | # into a local buffer. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 476 | |
Guido van Rossum | 3845521 | 2014-01-06 16:09:18 -0800 | [diff] [blame] | 477 | blocks = [] |
| 478 | while n > 0: |
| 479 | block = yield from self.read(n) |
| 480 | if not block: |
Victor Stinner | 8dffc45 | 2014-01-25 15:32:06 +0100 | [diff] [blame] | 481 | partial = b''.join(blocks) |
| 482 | raise IncompleteReadError(partial, len(partial) + n) |
Guido van Rossum | 3845521 | 2014-01-06 16:09:18 -0800 | [diff] [blame] | 483 | blocks.append(block) |
| 484 | n -= len(block) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 485 | |
Guido van Rossum | 3845521 | 2014-01-06 16:09:18 -0800 | [diff] [blame] | 486 | return b''.join(blocks) |