annotate Lib/asynchat.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents ff96a313cd1c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # -*- Mode: Python; tab-width: 4 -*-
16535
084b3dc9b6ff Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16479
diff changeset
2 # Id: asynchat.py,v 2.26 2000/09/07 22:29:26 rushing Exp
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
3 # Author: Sam Rushing <rushing@nightmare.com>
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # ======================================================================
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # Copyright 1996 by Sam Rushing
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
7 #
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # All Rights Reserved
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
9 #
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # Permission to use, copy, modify, and distribute this software and
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # its documentation for any purpose and without fee is hereby
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # granted, provided that the above copyright notice appear in all
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # copies and that both that copyright notice and this permission
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # notice appear in supporting documentation, and that the name of Sam
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # Rushing not be used in advertising or publicity pertaining to
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # distribution of the software without specific, written prior
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # permission.
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
18 #
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # ======================================================================
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
17326
bb9e214035e1 Make the docstring a raw string, for pydoc.
Guido van Rossum <guido@python.org>
parents: 17323
diff changeset
28 r"""A class supporting chat-style (command/response) protocols.
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
29
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
30 This class adds support for 'chat' style protocols - where one side
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
31 sends a 'command', and the other sends a response (examples would be
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
32 the common internet protocols - smtp, nntp, ftp, etc..).
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
33
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
34 The handle_read() method looks at the input stream for the current
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
35 'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n'
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
36 for multi-line output), calling self.found_terminator() on its
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
37 receipt.
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
38
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
39 for example:
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
40 Say you build an async nntp client using this class. At the start
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
41 of the connection, you'll have self.terminator set to '\r\n', in
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
42 order to process the single-line greeting. Just before issuing a
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
43 'LIST' command you'll set it to '\r\n.\r\n'. The output of the LIST
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
44 command will be accumulated (using your own 'collect_incoming_data'
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
45 method) up to the terminator, and then control will be returned to
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
46 you - by calling your self.found_terminator() method.
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
47 """
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11286
diff changeset
48
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 import socket
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 import asyncore
31467
44d4438ed57a Use collection.deque() instead of a list for a FIFO queue.
Raymond Hettinger <python@rcn.com>
parents: 28255
diff changeset
51 from collections import deque
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 48203
diff changeset
52 from sys import py3kwarning
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 48203
diff changeset
53 from warnings import filterwarnings, catch_warnings
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 class async_chat (asyncore.dispatcher):
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
56 """This is an abstract class. You must derive from this class, and add
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
57 the two methods collect_incoming_data() and found_terminator()"""
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
59 # these are overridable defaults
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
61 ac_in_buffer_size = 4096
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
62 ac_out_buffer_size = 4096
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
47907
d7fd32ce07aa Fixed bugs 760475, 953599, and 1519.
Josiah Carlson <josiah.carlson@gmail.com>
parents: 47539
diff changeset
64 def __init__ (self, sock=None, map=None):
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
65 # for string terminator matching
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
66 self.ac_in_buffer = ''
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
67
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
68 # we use a list here rather than cStringIO for a few reasons...
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
69 # del lst[:] is faster than sio.truncate(0)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
70 # lst = [] is faster than sio.truncate(0)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
71 # cStringIO will be gaining unicode support in py3k, which
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
72 # will negatively affect the performance of bytes compared to
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
73 # a ''.join() equivalent
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
74 self.incoming = []
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
75
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
76 # we toss the use of the "simple producer" and replace it with
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
77 # a pure deque, which the original fifo was a wrapping of
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
78 self.producer_fifo = deque()
47907
d7fd32ce07aa Fixed bugs 760475, 953599, and 1519.
Josiah Carlson <josiah.carlson@gmail.com>
parents: 47539
diff changeset
79 asyncore.dispatcher.__init__ (self, sock, map)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
22191
cb4edd10877d [Bug #491820] Define two abstract methods to shut up Pychecker, and for
Andrew M. Kuchling <amk@amk.ca>
parents: 17340
diff changeset
81 def collect_incoming_data(self, data):
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
82 raise NotImplementedError("must be implemented in subclass")
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
83
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
84 def _collect_incoming_data(self, data):
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
85 self.incoming.append(data)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
86
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
87 def _get_data(self):
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
88 d = ''.join(self.incoming)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
89 del self.incoming[:]
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
90 return d
23075
b7e87732f6ab Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 22415
diff changeset
91
22191
cb4edd10877d [Bug #491820] Define two abstract methods to shut up Pychecker, and for
Andrew M. Kuchling <amk@amk.ca>
parents: 17340
diff changeset
92 def found_terminator(self):
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
93 raise NotImplementedError("must be implemented in subclass")
23075
b7e87732f6ab Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 22415
diff changeset
94
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
95 def set_terminator (self, term):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
96 "Set the input delimiter. Can be a fixed string of any length, an integer, or None"
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
97 self.terminator = term
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
99 def get_terminator (self):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
100 return self.terminator
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
102 # grab some more data from the socket,
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
103 # throw it to the collector method,
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
104 # check for the terminator,
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
105 # if found, transition to the next state.
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
107 def handle_read (self):
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
109 try:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
110 data = self.recv (self.ac_in_buffer_size)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
111 except socket.error, why:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
112 self.handle_error()
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
113 return
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
115 self.ac_in_buffer = self.ac_in_buffer + data
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
117 # Continue to search for self.terminator in self.ac_in_buffer,
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
118 # while calling self.collect_incoming_data. The while loop
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
119 # is necessary because we might read several data+terminator
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
120 # combos with a single recv(4096).
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
122 while self.ac_in_buffer:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
123 lb = len(self.ac_in_buffer)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
124 terminator = self.get_terminator()
35053
5e45dcbf7cff [Patch #1002763] Allow long ints as terminator values; also, treat a terminator of 0 like the empty string or None
Andrew M. Kuchling <amk@amk.ca>
parents: 33598
diff changeset
125 if not terminator:
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
126 # no terminator, collect it all
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
127 self.collect_incoming_data (self.ac_in_buffer)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
128 self.ac_in_buffer = ''
35053
5e45dcbf7cff [Patch #1002763] Allow long ints as terminator values; also, treat a terminator of 0 like the empty string or None
Andrew M. Kuchling <amk@amk.ca>
parents: 33598
diff changeset
129 elif isinstance(terminator, int) or isinstance(terminator, long):
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
130 # numeric terminator
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
131 n = terminator
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
132 if lb < n:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
133 self.collect_incoming_data (self.ac_in_buffer)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
134 self.ac_in_buffer = ''
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
135 self.terminator = self.terminator - lb
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
136 else:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
137 self.collect_incoming_data (self.ac_in_buffer[:n])
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
138 self.ac_in_buffer = self.ac_in_buffer[n:]
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
139 self.terminator = 0
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
140 self.found_terminator()
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
141 else:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
142 # 3 cases:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
143 # 1) end of buffer matches terminator exactly:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
144 # collect data, transition
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
145 # 2) end of buffer matches some prefix:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
146 # collect data to the prefix
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
147 # 3) end of buffer does not match any prefix:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
148 # collect data
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
149 terminator_len = len(terminator)
17323
e7f5b70b0606 Fix the fix (my error -- hasty pasty).
Tim Peters <tim.peters@gmail.com>
parents: 17322
diff changeset
150 index = self.ac_in_buffer.find(terminator)
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
151 if index != -1:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
152 # we found the terminator
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
153 if index > 0:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
154 # don't bother reporting the empty string (source of subtle bugs)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
155 self.collect_incoming_data (self.ac_in_buffer[:index])
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
156 self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:]
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
157 # This does the Right Thing if the terminator is changed here.
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
158 self.found_terminator()
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
159 else:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
160 # check for a prefix of the terminator
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
161 index = find_prefix_at_end (self.ac_in_buffer, terminator)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
162 if index:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
163 if index != lb:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
164 # we found a prefix, collect up to the prefix
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
165 self.collect_incoming_data (self.ac_in_buffer[:-index])
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
166 self.ac_in_buffer = self.ac_in_buffer[-index:]
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
167 break
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
168 else:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
169 # no prefix, collect it all
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
170 self.collect_incoming_data (self.ac_in_buffer)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
171 self.ac_in_buffer = ''
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
173 def handle_write (self):
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
174 self.initiate_send()
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
175
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
176 def handle_close (self):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
177 self.close()
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
179 def push (self, data):
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
180 sabs = self.ac_out_buffer_size
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
181 if len(data) > sabs:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
182 for i in xrange(0, len(data), sabs):
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
183 self.producer_fifo.append(data[i:i+sabs])
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
184 else:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
185 self.producer_fifo.append(data)
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
186 self.initiate_send()
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
188 def push_with_producer (self, producer):
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
189 self.producer_fifo.append(producer)
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
190 self.initiate_send()
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
192 def readable (self):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
193 "predicate for inclusion in the readable for select()"
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
194 # cannot use the old predicate, it violates the claim of the
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
195 # set_terminator method.
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
196
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
197 # return (len(self.ac_in_buffer) <= self.ac_in_buffer_size)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
198 return 1
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
200 def writable (self):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
201 "predicate for inclusion in the writable for select()"
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
202 return self.producer_fifo or (not self.connected)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
203
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
204 def close_when_done (self):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
205 "automatically close this channel once the outgoing queue is empty"
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
206 self.producer_fifo.append(None)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
207
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
208 def initiate_send(self):
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
209 while self.producer_fifo and self.connected:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
210 first = self.producer_fifo[0]
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
211 # handle empty string/buffer or None entry
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
212 if not first:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
213 del self.producer_fifo[0]
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
214 if first is None:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
215 self.handle_close()
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
216 return
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
218 # handle classic producer behavior
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
219 obs = self.ac_out_buffer_size
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
220 try:
48948
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 48203
diff changeset
221 with catch_warnings():
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 48203
diff changeset
222 if py3kwarning:
d563621d76db Move test.test_support.catch_warning() to the warnings module, rename it
Brett Cannon <bcannon@gmail.com>
parents: 48203
diff changeset
223 filterwarnings("ignore", ".*buffer", DeprecationWarning)
48203
388a4be7d679 Suppress the warning in asynchat from using buffer() when running udner -3.
Brett Cannon <bcannon@gmail.com>
parents: 47907
diff changeset
224 data = buffer(first, 0, obs)
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
225 except TypeError:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
226 data = first.more()
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
227 if data:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
228 self.producer_fifo.appendleft(data)
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
229 else:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
230 del self.producer_fifo[0]
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
231 continue
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
232
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
233 # send the data
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
234 try:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
235 num_sent = self.send(data)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
236 except socket.error:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
237 self.handle_error()
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
238 return
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
240 if num_sent:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
241 if num_sent < len(data) or obs < len(first):
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
242 self.producer_fifo[0] = first[num_sent:]
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
243 else:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
244 del self.producer_fifo[0]
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
245 # we tried to send some actual data
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
246 return
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
248 def discard_buffers (self):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
249 # Emergencies only!
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
250 self.ac_in_buffer = ''
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
251 del self.incoming[:]
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35053
diff changeset
252 self.producer_fifo.clear()
14473
9537e7893f3c Match Sam Rushing's current version of asyncore.py and asynchat.py
Andrew M. Kuchling <amk@amk.ca>
parents: 13620
diff changeset
253
10934
c089020a7a1e Sam's latest versions
Guido van Rossum <guido@python.org>
parents: 9855
diff changeset
254 class simple_producer:
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
256 def __init__ (self, data, buffer_size=512):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
257 self.data = data
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
258 self.buffer_size = buffer_size
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
260 def more (self):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
261 if len (self.data) > self.buffer_size:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
262 result = self.data[:self.buffer_size]
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
263 self.data = self.data[self.buffer_size:]
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
264 return result
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
265 else:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
266 result = self.data
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
267 self.data = ''
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
268 return result
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
269
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 class fifo:
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
271 def __init__ (self, list=None):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
272 if not list:
31467
44d4438ed57a Use collection.deque() instead of a list for a FIFO queue.
Raymond Hettinger <python@rcn.com>
parents: 28255
diff changeset
273 self.list = deque()
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
274 else:
31467
44d4438ed57a Use collection.deque() instead of a list for a FIFO queue.
Raymond Hettinger <python@rcn.com>
parents: 28255
diff changeset
275 self.list = deque(list)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
276
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
277 def __len__ (self):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
278 return len(self.list)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
279
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
280 def is_empty (self):
33598
cf1aaa7e4005 Trivial bug fix: deque == [] is not a good way to check if a deque is empty.
Armin Rigo <arigo@tunes.org>
parents: 31607
diff changeset
281 return not self.list
10934
c089020a7a1e Sam's latest versions
Guido van Rossum <guido@python.org>
parents: 9855
diff changeset
282
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
283 def first (self):
31607
8ecee8f47108 Replace left(), right(), and __reversed__() with the more general purpose
Raymond Hettinger <python@rcn.com>
parents: 31603
diff changeset
284 return self.list[0]
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
286 def push (self, data):
31467
44d4438ed57a Use collection.deque() instead of a list for a FIFO queue.
Raymond Hettinger <python@rcn.com>
parents: 28255
diff changeset
287 self.list.append(data)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
288
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
289 def pop (self):
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
290 if self.list:
31467
44d4438ed57a Use collection.deque() instead of a list for a FIFO queue.
Raymond Hettinger <python@rcn.com>
parents: 28255
diff changeset
291 return (1, self.list.popleft())
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
292 else:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14473
diff changeset
293 return (0, None)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
294
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 # Given 'haystack', see if any prefix of 'needle' is at its end. This
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 # assumes an exact match has already been checked. Return the number of
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 # characters matched.
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 # for example:
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 # f_p_a_e ("qwerty\r", "\r\n") => 1
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 # f_p_a_e ("qwertydkjf", "\r\n") => 0
22415
701340c87723 A faster version of the find_prefix_at_end() function (that I found in the
Andrew M. Kuchling <amk@amk.ca>
parents: 22191
diff changeset
301 # f_p_a_e ("qwerty\r\n", "\r\n") => <undefined>
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
302
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 # this could maybe be made faster with a computed regex?
16231
3a20da2893c4 New asynchat.py from Sam Rushing: this foregoes using the regex module
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
304 # [answer: no; circa Python-2.0, Jan 2001]
22415
701340c87723 A faster version of the find_prefix_at_end() function (that I found in the
Andrew M. Kuchling <amk@amk.ca>
parents: 22191
diff changeset
305 # new python: 28961/s
701340c87723 A faster version of the find_prefix_at_end() function (that I found in the
Andrew M. Kuchling <amk@amk.ca>
parents: 22191
diff changeset
306 # old python: 18307/s
16231
3a20da2893c4 New asynchat.py from Sam Rushing: this foregoes using the regex module
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
307 # re: 12820/s
3a20da2893c4 New asynchat.py from Sam Rushing: this foregoes using the regex module
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
308 # regex: 14035/s
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 def find_prefix_at_end (haystack, needle):
23075
b7e87732f6ab Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 22415
diff changeset
311 l = len(needle) - 1
b7e87732f6ab Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 22415
diff changeset
312 while l and not haystack.endswith(needle[:l]):
b7e87732f6ab Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 22415
diff changeset
313 l -= 1
b7e87732f6ab Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 22415
diff changeset
314 return l