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 socket |
| 49 | import asyncore |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 50 | from collections import deque |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 51 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 52 | def buffer(obj, start=None, stop=None): |
| 53 | # if memoryview objects gain slicing semantics, |
| 54 | # this function will change for the better |
| 55 | # memoryview used for the TypeError |
| 56 | memoryview(obj) |
| 57 | if start == None: |
| 58 | start = 0 |
| 59 | if stop == None: |
| 60 | stop = len(obj) |
| 61 | x = obj[start:stop] |
| 62 | ## print("buffer type is: %s"%(type(x),)) |
| 63 | return x |
| 64 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 65 | class async_chat (asyncore.dispatcher): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 66 | """This is an abstract class. You must derive from this class, and add |
| 67 | the two methods collect_incoming_data() and found_terminator()""" |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 68 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 69 | # these are overridable defaults |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 70 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 71 | ac_in_buffer_size = 4096 |
| 72 | ac_out_buffer_size = 4096 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 73 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 74 | # we don't want to enable the use of encoding by default, because that is a |
| 75 | # sign of an application bug that we don't want to pass silently |
| 76 | |
| 77 | use_encoding = 0 |
| 78 | encoding = 'latin1' |
| 79 | |
Josiah Carlson | 9f2f833 | 2008-07-07 05:04:12 +0000 | [diff] [blame] | 80 | def __init__ (self, sock=None, map=None): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 81 | # for string terminator matching |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 82 | self.ac_in_buffer = b'' |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 83 | |
| 84 | # we use a list here rather than cStringIO for a few reasons... |
| 85 | # del lst[:] is faster than sio.truncate(0) |
| 86 | # lst = [] is faster than sio.truncate(0) |
| 87 | # cStringIO will be gaining unicode support in py3k, which |
| 88 | # will negatively affect the performance of bytes compared to |
| 89 | # a ''.join() equivalent |
| 90 | self.incoming = [] |
| 91 | |
| 92 | # we toss the use of the "simple producer" and replace it with |
| 93 | # a pure deque, which the original fifo was a wrapping of |
| 94 | self.producer_fifo = deque() |
Josiah Carlson | 9f2f833 | 2008-07-07 05:04:12 +0000 | [diff] [blame] | 95 | asyncore.dispatcher.__init__ (self, sock, map) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 96 | |
Andrew M. Kuchling | 7dd5f3c | 2002-03-08 18:27:11 +0000 | [diff] [blame] | 97 | def collect_incoming_data(self, data): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 98 | raise NotImplementedError("must be implemented in subclass") |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 99 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 100 | def _collect_incoming_data(self, data): |
| 101 | self.incoming.append(data) |
| 102 | |
| 103 | def _get_data(self): |
| 104 | d = b''.join(self.incoming) |
| 105 | del self.incoming[:] |
| 106 | return d |
| 107 | |
Andrew M. Kuchling | 7dd5f3c | 2002-03-08 18:27:11 +0000 | [diff] [blame] | 108 | def found_terminator(self): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 109 | raise NotImplementedError("must be implemented in subclass") |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 110 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 111 | def set_terminator (self, term): |
| 112 | "Set the input delimiter. Can be a fixed string of any length, an integer, or None" |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 113 | if isinstance(term, str) and self.use_encoding: |
| 114 | term = bytes(term, self.encoding) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 115 | self.terminator = term |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 116 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 117 | def get_terminator (self): |
| 118 | return self.terminator |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 119 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 120 | # grab some more data from the socket, |
| 121 | # throw it to the collector method, |
| 122 | # check for the terminator, |
| 123 | # if found, transition to the next state. |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 124 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 125 | def handle_read (self): |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 126 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 127 | try: |
| 128 | data = self.recv (self.ac_in_buffer_size) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 129 | except socket.error as why: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 130 | self.handle_error() |
| 131 | return |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 132 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 133 | if isinstance(data, str) and self.use_encoding: |
| 134 | data = bytes(str, self.encoding) |
| 135 | self.ac_in_buffer = self.ac_in_buffer + data |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 136 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 137 | # Continue to search for self.terminator in self.ac_in_buffer, |
| 138 | # while calling self.collect_incoming_data. The while loop |
| 139 | # is necessary because we might read several data+terminator |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 140 | # combos with a single recv(4096). |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 141 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 142 | while self.ac_in_buffer: |
| 143 | lb = len(self.ac_in_buffer) |
| 144 | terminator = self.get_terminator() |
Andrew M. Kuchling | ca69f02 | 2005-06-09 14:59:45 +0000 | [diff] [blame] | 145 | if not terminator: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 146 | # no terminator, collect it all |
| 147 | self.collect_incoming_data (self.ac_in_buffer) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 148 | self.ac_in_buffer = b'' |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 149 | elif isinstance(terminator, int): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 150 | # numeric terminator |
| 151 | n = terminator |
| 152 | if lb < n: |
| 153 | self.collect_incoming_data (self.ac_in_buffer) |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 154 | self.ac_in_buffer = b'' |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 155 | self.terminator = self.terminator - lb |
| 156 | else: |
| 157 | self.collect_incoming_data (self.ac_in_buffer[:n]) |
| 158 | self.ac_in_buffer = self.ac_in_buffer[n:] |
| 159 | self.terminator = 0 |
| 160 | self.found_terminator() |
| 161 | else: |
| 162 | # 3 cases: |
| 163 | # 1) end of buffer matches terminator exactly: |
| 164 | # collect data, transition |
| 165 | # 2) end of buffer matches some prefix: |
| 166 | # collect data to the prefix |
| 167 | # 3) end of buffer does not match any prefix: |
| 168 | # collect data |
| 169 | terminator_len = len(terminator) |
Tim Peters | b5d1392 | 2001-04-05 22:38:32 +0000 | [diff] [blame] | 170 | index = self.ac_in_buffer.find(terminator) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 171 | if index != -1: |
| 172 | # we found the terminator |
| 173 | if index > 0: |
| 174 | # don't bother reporting the empty string (source of subtle bugs) |
| 175 | self.collect_incoming_data (self.ac_in_buffer[:index]) |
| 176 | self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:] |
| 177 | # This does the Right Thing if the terminator is changed here. |
| 178 | self.found_terminator() |
| 179 | else: |
| 180 | # check for a prefix of the terminator |
| 181 | index = find_prefix_at_end (self.ac_in_buffer, terminator) |
| 182 | if index: |
| 183 | if index != lb: |
| 184 | # we found a prefix, collect up to the prefix |
| 185 | self.collect_incoming_data (self.ac_in_buffer[:-index]) |
| 186 | self.ac_in_buffer = self.ac_in_buffer[-index:] |
| 187 | break |
| 188 | else: |
| 189 | # no prefix, collect it all |
| 190 | self.collect_incoming_data (self.ac_in_buffer) |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 191 | self.ac_in_buffer = b'' |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 192 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 193 | def handle_write (self): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 194 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 195 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 196 | def handle_close (self): |
| 197 | self.close() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 198 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 199 | def push (self, data): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 200 | sabs = self.ac_out_buffer_size |
| 201 | if len(data) > sabs: |
| 202 | for i in range(0, len(data), sabs): |
| 203 | self.producer_fifo.append(data[i:i+sabs]) |
| 204 | else: |
| 205 | self.producer_fifo.append(data) |
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 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 208 | def push_with_producer (self, producer): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 209 | self.producer_fifo.append(producer) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 210 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 211 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 212 | def readable (self): |
| 213 | "predicate for inclusion in the readable for select()" |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 214 | # cannot use the old predicate, it violates the claim of the |
| 215 | # set_terminator method. |
| 216 | |
| 217 | # return (len(self.ac_in_buffer) <= self.ac_in_buffer_size) |
| 218 | return 1 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 219 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 220 | def writable (self): |
| 221 | "predicate for inclusion in the writable for select()" |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 222 | return self.producer_fifo or (not self.connected) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 223 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 224 | def close_when_done (self): |
| 225 | "automatically close this channel once the outgoing queue is empty" |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 226 | self.producer_fifo.append(None) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 227 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 228 | def initiate_send(self): |
| 229 | while self.producer_fifo and self.connected: |
| 230 | first = self.producer_fifo[0] |
| 231 | # handle empty string/buffer or None entry |
| 232 | if not first: |
| 233 | del self.producer_fifo[0] |
| 234 | if first is None: |
| 235 | ## print("first is None") |
| 236 | self.handle_close() |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 237 | return |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 238 | ## print("first is not None") |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 239 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 240 | # handle classic producer behavior |
| 241 | obs = self.ac_out_buffer_size |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 242 | try: |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 243 | data = buffer(first, 0, obs) |
| 244 | except TypeError: |
| 245 | data = first.more() |
| 246 | if data: |
| 247 | self.producer_fifo.appendleft(data) |
| 248 | else: |
| 249 | del self.producer_fifo[0] |
| 250 | continue |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 251 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 252 | if isinstance(data, str) and self.use_encoding: |
| 253 | data = bytes(data, self.encoding) |
| 254 | |
| 255 | # send the data |
| 256 | try: |
| 257 | num_sent = self.send(data) |
| 258 | except socket.error: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 259 | self.handle_error() |
| 260 | return |
| 261 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 262 | if num_sent: |
| 263 | if num_sent < len(data) or obs < len(first): |
| 264 | self.producer_fifo[0] = first[num_sent:] |
| 265 | else: |
| 266 | del self.producer_fifo[0] |
| 267 | # we tried to send some actual data |
| 268 | return |
| 269 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 270 | def discard_buffers (self): |
| 271 | # Emergencies only! |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 272 | self.ac_in_buffer = b'' |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 273 | del self.incoming[:] |
| 274 | self.producer_fifo.clear() |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 276 | class simple_producer: |
Guido van Rossum | a8d0f4f | 1999-06-08 13:20:05 +0000 | [diff] [blame] | 277 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 278 | def __init__ (self, data, buffer_size=512): |
| 279 | self.data = data |
| 280 | self.buffer_size = buffer_size |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 281 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 282 | def more (self): |
| 283 | if len (self.data) > self.buffer_size: |
| 284 | result = self.data[:self.buffer_size] |
| 285 | self.data = self.data[self.buffer_size:] |
| 286 | return result |
| 287 | else: |
| 288 | result = self.data |
Guido van Rossum | 076da09 | 2007-07-12 07:58:54 +0000 | [diff] [blame] | 289 | self.data = b'' |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 290 | return result |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 291 | |
| 292 | class fifo: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 293 | def __init__ (self, list=None): |
| 294 | if not list: |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 295 | self.list = deque() |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 296 | else: |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 297 | self.list = deque(list) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 298 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 299 | def __len__ (self): |
| 300 | return len(self.list) |
Guido van Rossum | a8d0f4f | 1999-06-08 13:20:05 +0000 | [diff] [blame] | 301 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 302 | def is_empty (self): |
Armin Rigo | b562bc6 | 2004-09-27 17:49:00 +0000 | [diff] [blame] | 303 | return not self.list |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 304 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 305 | def first (self): |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 306 | return self.list[0] |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 307 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 308 | def push (self, data): |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 309 | self.list.append(data) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 310 | |
| 311 | def pop (self): |
| 312 | if self.list: |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 313 | return (1, self.list.popleft()) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 314 | else: |
| 315 | return (0, None) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 316 | |
| 317 | # Given 'haystack', see if any prefix of 'needle' is at its end. This |
| 318 | # assumes an exact match has already been checked. Return the number of |
| 319 | # characters matched. |
| 320 | # for example: |
| 321 | # f_p_a_e ("qwerty\r", "\r\n") => 1 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 322 | # f_p_a_e ("qwertydkjf", "\r\n") => 0 |
Andrew M. Kuchling | c63a396 | 2002-03-20 02:22:58 +0000 | [diff] [blame] | 323 | # f_p_a_e ("qwerty\r\n", "\r\n") => <undefined> |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 324 | |
| 325 | # this could maybe be made faster with a computed regex? |
Andrew M. Kuchling | d305f51 | 2001-01-24 21:10:55 +0000 | [diff] [blame] | 326 | # [answer: no; circa Python-2.0, Jan 2001] |
Andrew M. Kuchling | c63a396 | 2002-03-20 02:22:58 +0000 | [diff] [blame] | 327 | # new python: 28961/s |
| 328 | # old python: 18307/s |
Andrew M. Kuchling | d305f51 | 2001-01-24 21:10:55 +0000 | [diff] [blame] | 329 | # re: 12820/s |
| 330 | # regex: 14035/s |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 331 | |
| 332 | def find_prefix_at_end (haystack, needle): |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 333 | l = len(needle) - 1 |
| 334 | while l and not haystack.endswith(needle[:l]): |
| 335 | l -= 1 |
| 336 | return l |