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