Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 1 | # -*- Mode: Python; tab-width: 4 -*- |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 2 | # Id: asynchat.py,v 2.26 2000/09/07 22:29:26 rushing Exp |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 3 | # Author: Sam Rushing <rushing@nightmare.com> |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 4 | |
| 5 | # ====================================================================== |
| 6 | # Copyright 1996 by Sam Rushing |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 7 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 8 | # All Rights Reserved |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 9 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 10 | # Permission to use, copy, modify, and distribute this software and |
| 11 | # its documentation for any purpose and without fee is hereby |
| 12 | # granted, provided that the above copyright notice appear in all |
| 13 | # copies and that both that copyright notice and this permission |
| 14 | # notice appear in supporting documentation, and that the name of Sam |
| 15 | # Rushing not be used in advertising or publicity pertaining to |
| 16 | # distribution of the software without specific, written prior |
| 17 | # permission. |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 18 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 19 | # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 20 | # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN |
| 21 | # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 22 | # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
| 23 | # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 25 | # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 26 | # ====================================================================== |
| 27 | |
Guido van Rossum | e4a1b6d | 2001-04-06 15:30:33 +0000 | [diff] [blame] | 28 | r"""A class supporting chat-style (command/response) protocols. |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 29 | |
| 30 | This class adds support for 'chat' style protocols - where one side |
| 31 | sends a 'command', and the other sends a response (examples would be |
| 32 | the common internet protocols - smtp, nntp, ftp, etc..). |
| 33 | |
| 34 | The handle_read() method looks at the input stream for the current |
| 35 | 'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n' |
| 36 | for multi-line output), calling self.found_terminator() on its |
| 37 | receipt. |
| 38 | |
| 39 | for example: |
| 40 | Say you build an async nntp client using this class. At the start |
| 41 | of the connection, you'll have self.terminator set to '\r\n', in |
| 42 | order to process the single-line greeting. Just before issuing a |
| 43 | 'LIST' command you'll set it to '\r\n.\r\n'. The output of the LIST |
| 44 | command will be accumulated (using your own 'collect_incoming_data' |
| 45 | method) up to the terminator, and then control will be returned to |
| 46 | you - by calling your self.found_terminator() method. |
| 47 | """ |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 48 | import asyncore |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 49 | from collections import deque |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 50 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 51 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 52 | class async_chat(asyncore.dispatcher): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 53 | """This is an abstract class. You must derive from this class, and add |
| 54 | the two methods collect_incoming_data() and found_terminator()""" |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 55 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 56 | # these are overridable defaults |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 57 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 58 | ac_in_buffer_size = 65536 |
| 59 | ac_out_buffer_size = 65536 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 60 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 61 | # we don't want to enable the use of encoding by default, because that is a |
| 62 | # sign of an application bug that we don't want to pass silently |
| 63 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 64 | use_encoding = 0 |
| 65 | encoding = 'latin-1' |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 66 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 67 | def __init__(self, sock=None, map=None): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 68 | # for string terminator matching |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 69 | self.ac_in_buffer = b'' |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 70 | |
Serhiy Storchaka | 50254c5 | 2013-08-29 11:35:43 +0300 | [diff] [blame] | 71 | # we use a list here rather than io.BytesIO for a few reasons... |
| 72 | # del lst[:] is faster than bio.truncate(0) |
| 73 | # lst = [] is faster than bio.truncate(0) |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 74 | self.incoming = [] |
| 75 | |
| 76 | # we toss the use of the "simple producer" and replace it with |
| 77 | # a pure deque, which the original fifo was a wrapping of |
| 78 | self.producer_fifo = deque() |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 79 | asyncore.dispatcher.__init__(self, sock, map) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 80 | |
Andrew M. Kuchling | 7dd5f3c | 2002-03-08 18:27:11 +0000 | [diff] [blame] | 81 | def collect_incoming_data(self, data): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 82 | raise NotImplementedError("must be implemented in subclass") |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 83 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 84 | def _collect_incoming_data(self, data): |
| 85 | self.incoming.append(data) |
| 86 | |
| 87 | def _get_data(self): |
| 88 | d = b''.join(self.incoming) |
| 89 | del self.incoming[:] |
| 90 | return d |
| 91 | |
Andrew M. Kuchling | 7dd5f3c | 2002-03-08 18:27:11 +0000 | [diff] [blame] | 92 | def found_terminator(self): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 93 | raise NotImplementedError("must be implemented in subclass") |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 94 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 95 | def set_terminator(self, term): |
| 96 | """Set the input delimiter. |
| 97 | |
| 98 | Can be a fixed string of any length, an integer, or None. |
| 99 | """ |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 100 | if isinstance(term, str) and self.use_encoding: |
| 101 | term = bytes(term, self.encoding) |
Victor Stinner | 630a4f6 | 2014-07-08 00:26:36 +0200 | [diff] [blame] | 102 | elif isinstance(term, int) and term < 0: |
| 103 | raise ValueError('the number of received bytes must be positive') |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 104 | self.terminator = term |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 105 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 106 | def get_terminator(self): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 107 | return self.terminator |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 108 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 109 | # grab some more data from the socket, |
| 110 | # throw it to the collector method, |
| 111 | # check for the terminator, |
| 112 | # if found, transition to the next state. |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 113 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 114 | def handle_read(self): |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 115 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 116 | try: |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 117 | data = self.recv(self.ac_in_buffer_size) |
Victor Stinner | 45cff66 | 2014-07-24 18:49:36 +0200 | [diff] [blame] | 118 | except BlockingIOError: |
| 119 | return |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 120 | except OSError as why: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 121 | self.handle_error() |
| 122 | return |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 123 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 124 | if isinstance(data, str) and self.use_encoding: |
| 125 | data = bytes(str, self.encoding) |
| 126 | self.ac_in_buffer = self.ac_in_buffer + data |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 127 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 128 | # Continue to search for self.terminator in self.ac_in_buffer, |
| 129 | # while calling self.collect_incoming_data. The while loop |
| 130 | # is necessary because we might read several data+terminator |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 131 | # combos with a single recv(4096). |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 132 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 133 | while self.ac_in_buffer: |
| 134 | lb = len(self.ac_in_buffer) |
| 135 | terminator = self.get_terminator() |
Andrew M. Kuchling | ca69f02 | 2005-06-09 14:59:45 +0000 | [diff] [blame] | 136 | if not terminator: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 137 | # no terminator, collect it all |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 138 | self.collect_incoming_data(self.ac_in_buffer) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 139 | self.ac_in_buffer = b'' |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 140 | elif isinstance(terminator, int): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 141 | # numeric terminator |
| 142 | n = terminator |
| 143 | if lb < n: |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 144 | self.collect_incoming_data(self.ac_in_buffer) |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 145 | self.ac_in_buffer = b'' |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 146 | self.terminator = self.terminator - lb |
| 147 | else: |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 148 | self.collect_incoming_data(self.ac_in_buffer[:n]) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 149 | self.ac_in_buffer = self.ac_in_buffer[n:] |
| 150 | self.terminator = 0 |
| 151 | self.found_terminator() |
| 152 | else: |
| 153 | # 3 cases: |
| 154 | # 1) end of buffer matches terminator exactly: |
| 155 | # collect data, transition |
| 156 | # 2) end of buffer matches some prefix: |
| 157 | # collect data to the prefix |
| 158 | # 3) end of buffer does not match any prefix: |
| 159 | # collect data |
| 160 | terminator_len = len(terminator) |
Tim Peters | b5d1392 | 2001-04-05 22:38:32 +0000 | [diff] [blame] | 161 | index = self.ac_in_buffer.find(terminator) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 162 | if index != -1: |
| 163 | # we found the terminator |
| 164 | if index > 0: |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 165 | # don't bother reporting the empty string |
| 166 | # (source of subtle bugs) |
| 167 | self.collect_incoming_data(self.ac_in_buffer[:index]) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 168 | self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:] |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 169 | # This does the Right Thing if the terminator |
| 170 | # is changed here. |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 171 | self.found_terminator() |
| 172 | else: |
| 173 | # check for a prefix of the terminator |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 174 | index = find_prefix_at_end(self.ac_in_buffer, terminator) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 175 | if index: |
| 176 | if index != lb: |
| 177 | # we found a prefix, collect up to the prefix |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 178 | self.collect_incoming_data(self.ac_in_buffer[:-index]) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 179 | self.ac_in_buffer = self.ac_in_buffer[-index:] |
| 180 | break |
| 181 | else: |
| 182 | # no prefix, collect it all |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 183 | self.collect_incoming_data(self.ac_in_buffer) |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 184 | self.ac_in_buffer = b'' |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 185 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 186 | def handle_write(self): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 187 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 188 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 189 | def handle_close(self): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 190 | self.close() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 191 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 192 | def push(self, data): |
Victor Stinner | d9e810a | 2014-07-08 00:00:30 +0200 | [diff] [blame] | 193 | if not isinstance(data, (bytes, bytearray, memoryview)): |
| 194 | raise TypeError('data argument must be byte-ish (%r)', |
| 195 | type(data)) |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 196 | sabs = self.ac_out_buffer_size |
| 197 | if len(data) > sabs: |
| 198 | for i in range(0, len(data), sabs): |
| 199 | self.producer_fifo.append(data[i:i+sabs]) |
| 200 | else: |
| 201 | self.producer_fifo.append(data) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 202 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 203 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 204 | def push_with_producer(self, producer): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 205 | self.producer_fifo.append(producer) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 206 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 207 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 208 | def readable(self): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 209 | "predicate for inclusion in the readable for select()" |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 210 | # cannot use the old predicate, it violates the claim of the |
| 211 | # set_terminator method. |
| 212 | |
| 213 | # return (len(self.ac_in_buffer) <= self.ac_in_buffer_size) |
| 214 | return 1 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 215 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 216 | def writable(self): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 217 | "predicate for inclusion in the writable for select()" |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 218 | return self.producer_fifo or (not self.connected) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 219 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 220 | def close_when_done(self): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 221 | "automatically close this channel once the outgoing queue is empty" |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 222 | self.producer_fifo.append(None) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 223 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 224 | def initiate_send(self): |
| 225 | while self.producer_fifo and self.connected: |
| 226 | first = self.producer_fifo[0] |
| 227 | # handle empty string/buffer or None entry |
| 228 | if not first: |
| 229 | del self.producer_fifo[0] |
| 230 | if first is None: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 231 | self.handle_close() |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 232 | return |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 233 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 234 | # handle classic producer behavior |
| 235 | obs = self.ac_out_buffer_size |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 236 | try: |
Giampaolo Rodola' | d9f38bc | 2012-08-04 14:38:16 +0200 | [diff] [blame] | 237 | data = first[:obs] |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 238 | except TypeError: |
| 239 | data = first.more() |
| 240 | if data: |
| 241 | self.producer_fifo.appendleft(data) |
| 242 | else: |
| 243 | del self.producer_fifo[0] |
| 244 | continue |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 245 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 246 | if isinstance(data, str) and self.use_encoding: |
| 247 | data = bytes(data, self.encoding) |
| 248 | |
| 249 | # send the data |
| 250 | try: |
| 251 | num_sent = self.send(data) |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 252 | except OSError: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 253 | self.handle_error() |
| 254 | return |
| 255 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 256 | if num_sent: |
| 257 | if num_sent < len(data) or obs < len(first): |
| 258 | self.producer_fifo[0] = first[num_sent:] |
| 259 | else: |
| 260 | del self.producer_fifo[0] |
| 261 | # we tried to send some actual data |
| 262 | return |
| 263 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 264 | def discard_buffers(self): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 265 | # Emergencies only! |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 266 | self.ac_in_buffer = b'' |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 267 | del self.incoming[:] |
| 268 | self.producer_fifo.clear() |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 269 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 270 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 271 | class simple_producer: |
Guido van Rossum | a8d0f4f | 1999-06-08 13:20:05 +0000 | [diff] [blame] | 272 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 273 | def __init__(self, data, buffer_size=512): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 274 | self.data = data |
| 275 | self.buffer_size = buffer_size |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 276 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 277 | def more(self): |
| 278 | if len(self.data) > self.buffer_size: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 279 | result = self.data[:self.buffer_size] |
| 280 | self.data = self.data[self.buffer_size:] |
| 281 | return result |
| 282 | else: |
| 283 | result = self.data |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 284 | self.data = b'' |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 285 | return result |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 286 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 287 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 288 | class fifo: |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 289 | def __init__(self, list=None): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 290 | if not list: |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 291 | self.list = deque() |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 292 | else: |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 293 | self.list = deque(list) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 294 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 295 | def __len__(self): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 296 | return len(self.list) |
Guido van Rossum | a8d0f4f | 1999-06-08 13:20:05 +0000 | [diff] [blame] | 297 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 298 | def is_empty(self): |
Armin Rigo | b562bc6 | 2004-09-27 17:49:00 +0000 | [diff] [blame] | 299 | return not self.list |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 300 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 301 | def first(self): |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 302 | return self.list[0] |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 303 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 304 | def push(self, data): |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 305 | self.list.append(data) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 306 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 307 | def pop(self): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 308 | if self.list: |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 309 | return (1, self.list.popleft()) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 310 | else: |
| 311 | return (0, None) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 312 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 313 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 314 | # Given 'haystack', see if any prefix of 'needle' is at its end. This |
| 315 | # assumes an exact match has already been checked. Return the number of |
| 316 | # characters matched. |
| 317 | # for example: |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 318 | # f_p_a_e("qwerty\r", "\r\n") => 1 |
| 319 | # f_p_a_e("qwertydkjf", "\r\n") => 0 |
| 320 | # f_p_a_e("qwerty\r\n", "\r\n") => <undefined> |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 321 | |
| 322 | # this could maybe be made faster with a computed regex? |
Andrew M. Kuchling | d305f51 | 2001-01-24 21:10:55 +0000 | [diff] [blame] | 323 | # [answer: no; circa Python-2.0, Jan 2001] |
Andrew M. Kuchling | c63a396 | 2002-03-20 02:22:58 +0000 | [diff] [blame] | 324 | # new python: 28961/s |
| 325 | # old python: 18307/s |
Andrew M. Kuchling | d305f51 | 2001-01-24 21:10:55 +0000 | [diff] [blame] | 326 | # re: 12820/s |
| 327 | # regex: 14035/s |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 328 | |
Victor Stinner | fd5d1b5 | 2014-07-08 00:16:54 +0200 | [diff] [blame] | 329 | def find_prefix_at_end(haystack, needle): |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 330 | l = len(needle) - 1 |
| 331 | while l and not haystack.endswith(needle[:l]): |
| 332 | l -= 1 |
| 333 | return l |