annotate Lib/asyncore.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents c54b784a56d5
children 54efaf6d2f89
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
1 # -*- Mode: Python -*-
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14509
diff changeset
2 # Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
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: 14509
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: 14509
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: 14509
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
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
28 """Basic infrastructure for asynchronous socket service clients and servers.
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
29
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
30 There are only two ways to have a program on a single processor do "more
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14509
diff changeset
31 than one thing at a time". Multi-threaded programming is the simplest and
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
32 most popular way to do it, but there is another very different technique,
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
33 that lets you have nearly all the advantages of multi-threading, without
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
34 actually using multiple threads. it's really only practical if your program
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
35 is largely I/O bound. If your program is CPU bound, then pre-emptive
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
36 scheduled threads are probably what you really need. Network servers are
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14509
diff changeset
37 rarely CPU-bound, however.
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
38
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14509
diff changeset
39 If your operating system supports the select() system call in its I/O
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
40 library (and nearly all do), then you can use it to juggle multiple
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
41 communication channels at once; doing other work while your I/O is taking
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
42 place in the "background." Although this strategy can seem strange and
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
43 complex, especially at first, it is in many ways easier to understand and
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
44 control than multi-threaded programming. The module documented here solves
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
45 many of the difficult problems for you, making the task of building
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14509
diff changeset
46 sophisticated high-performance network servers and clients a snap.
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
diff changeset
47 """
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11285
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 select
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 import socket
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 import sys
25404
75c4a67dcb8c On Windows, select() does not accept empty lists.
Thomas Heller <theller@ctypes.org>
parents: 25249
diff changeset
52 import time
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 import os
19127
78752eae132e Remove hard-coded NT constants that are already defined in errno on NT.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17794
diff changeset
55 from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
56 ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EBADF, ECONNABORTED, errorcode
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
14473
9537e7893f3c Match Sam Rushing's current version of asyncore.py and asynchat.py
Andrew M. Kuchling <amk@amk.ca>
parents: 11851
diff changeset
58 try:
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
59 socket_map
14473
9537e7893f3c Match Sam Rushing's current version of asyncore.py and asynchat.py
Andrew M. Kuchling <amk@amk.ca>
parents: 11851
diff changeset
60 except NameError:
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
61 socket_map = {}
14473
9537e7893f3c Match Sam Rushing's current version of asyncore.py and asynchat.py
Andrew M. Kuchling <amk@amk.ca>
parents: 11851
diff changeset
62
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
63 def _strerror(err):
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
64 res = os.strerror(err)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
65 if res == 'Unknown error':
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
66 res = errorcode[err]
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
67 return res
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
68
35505
26ba3d4f4fc4 No need to import exceptions, they are builtins
Neal Norwitz <nnorwitz@gmail.com>
parents: 33388
diff changeset
69 class ExitNow(Exception):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
70 pass
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
72 _reraised_exceptions = (ExitNow, KeyboardInterrupt, SystemExit)
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
73
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
74 def read(obj):
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
75 try:
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
76 obj.handle_read_event()
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
77 except _reraised_exceptions:
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
78 raise
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
79 except:
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
80 obj.handle_error()
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
81
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
82 def write(obj):
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
83 try:
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
84 obj.handle_write_event()
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
85 except _reraised_exceptions:
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
86 raise
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
87 except:
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
88 obj.handle_error()
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
89
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
90 def _exception(obj):
32585
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
91 try:
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
92 obj.handle_expt_event()
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
93 except _reraised_exceptions:
32585
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
94 raise
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
95 except:
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
96 obj.handle_error()
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
97
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
98 def readwrite(obj, flags):
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
99 try:
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
100 if flags & select.POLLIN:
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
101 obj.handle_read_event()
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
102 if flags & select.POLLOUT:
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
103 obj.handle_write_event()
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
104 if flags & (select.POLLHUP | select.POLLERR | select.POLLNVAL):
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
105 obj.handle_close()
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
106 if flags & select.POLLPRI:
31744
3475e4568471 [Part of patch #909005] Handle POLLPRI flag, and various errors cases. Fixes bug #887279
Andrew M. Kuchling <amk@amk.ca>
parents: 31480
diff changeset
107 obj.handle_expt_event()
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
108 except _reraised_exceptions:
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
109 raise
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
110 except:
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
111 obj.handle_error()
14473
9537e7893f3c Match Sam Rushing's current version of asyncore.py and asynchat.py
Andrew M. Kuchling <amk@amk.ca>
parents: 11851
diff changeset
112
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
113 def poll(timeout=0.0, map=None):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
114 if map is None:
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
115 map = socket_map
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
116 if map:
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
117 r = []; w = []; e = []
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
118 for fd, obj in map.items():
33388
1100605870fe [Bug #1011606] Only check file descriptors for exceptional conditions if the fd is readable or writable
Andrew M. Kuchling <amk@amk.ca>
parents: 33120
diff changeset
119 is_r = obj.readable()
1100605870fe [Bug #1011606] Only check file descriptors for exceptional conditions if the fd is readable or writable
Andrew M. Kuchling <amk@amk.ca>
parents: 33120
diff changeset
120 is_w = obj.writable()
1100605870fe [Bug #1011606] Only check file descriptors for exceptional conditions if the fd is readable or writable
Andrew M. Kuchling <amk@amk.ca>
parents: 33120
diff changeset
121 if is_r:
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
122 r.append(fd)
33388
1100605870fe [Bug #1011606] Only check file descriptors for exceptional conditions if the fd is readable or writable
Andrew M. Kuchling <amk@amk.ca>
parents: 33120
diff changeset
123 if is_w:
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
124 w.append(fd)
33388
1100605870fe [Bug #1011606] Only check file descriptors for exceptional conditions if the fd is readable or writable
Andrew M. Kuchling <amk@amk.ca>
parents: 33120
diff changeset
125 if is_r or is_w:
1100605870fe [Bug #1011606] Only check file descriptors for exceptional conditions if the fd is readable or writable
Andrew M. Kuchling <amk@amk.ca>
parents: 33120
diff changeset
126 e.append(fd)
25404
75c4a67dcb8c On Windows, select() does not accept empty lists.
Thomas Heller <theller@ctypes.org>
parents: 25249
diff changeset
127 if [] == r == w == e:
75c4a67dcb8c On Windows, select() does not accept empty lists.
Thomas Heller <theller@ctypes.org>
parents: 25249
diff changeset
128 time.sleep(timeout)
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
129 return
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
130
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
131 try:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
132 r, w, e = select.select(r, w, e, timeout)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
133 except select.error, err:
48035
644fa7a240c8 Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents: 47934
diff changeset
134 if err.args[0] != EINTR:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
135 raise
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
136 else:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
137 return
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
139 for fd in r:
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
140 obj = map.get(fd)
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
141 if obj is None:
20325
8585535e404b Patch #468647: Fix exception propagation in asyncore.
Martin v. Löwis <martin@v.loewis.de>
parents: 20233
diff changeset
142 continue
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
143 read(obj)
14473
9537e7893f3c Match Sam Rushing's current version of asyncore.py and asynchat.py
Andrew M. Kuchling <amk@amk.ca>
parents: 11851
diff changeset
144
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
145 for fd in w:
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
146 obj = map.get(fd)
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
147 if obj is None:
20325
8585535e404b Patch #468647: Fix exception propagation in asyncore.
Martin v. Löwis <martin@v.loewis.de>
parents: 20233
diff changeset
148 continue
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
149 write(obj)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
32585
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
151 for fd in e:
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
152 obj = map.get(fd)
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
153 if obj is None:
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
154 continue
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
155 _exception(obj)
a03bca1654d0 In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents: 32581
diff changeset
156
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
157 def poll2(timeout=0.0, map=None):
16212
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
158 # Use the poll() support added to the select module in Python 2.0
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
159 if map is None:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
160 map = socket_map
19986
f42f89b5b8f9 Patch #461321: Support None as a timeout in poll2 and poll3.
Martin v. Löwis <martin@v.loewis.de>
parents: 19860
diff changeset
161 if timeout is not None:
f42f89b5b8f9 Patch #461321: Support None as a timeout in poll2 and poll3.
Martin v. Löwis <martin@v.loewis.de>
parents: 19860
diff changeset
162 # timeout is in milliseconds
f42f89b5b8f9 Patch #461321: Support None as a timeout in poll2 and poll3.
Martin v. Löwis <martin@v.loewis.de>
parents: 19860
diff changeset
163 timeout = int(timeout*1000)
16212
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
164 pollster = select.poll()
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
165 if map:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
166 for fd, obj in map.items():
33388
1100605870fe [Bug #1011606] Only check file descriptors for exceptional conditions if the fd is readable or writable
Andrew M. Kuchling <amk@amk.ca>
parents: 33120
diff changeset
167 flags = 0
16212
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
168 if obj.readable():
32516
4549e75a6243 For readable() objects, the previous value of 'flags' was ignored.
Andrew M. Kuchling <amk@amk.ca>
parents: 32447
diff changeset
169 flags |= select.POLLIN | select.POLLPRI
16212
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
170 if obj.writable():
32516
4549e75a6243 For readable() objects, the previous value of 'flags' was ignored.
Andrew M. Kuchling <amk@amk.ca>
parents: 32447
diff changeset
171 flags |= select.POLLOUT
16212
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
172 if flags:
33388
1100605870fe [Bug #1011606] Only check file descriptors for exceptional conditions if the fd is readable or writable
Andrew M. Kuchling <amk@amk.ca>
parents: 33120
diff changeset
173 # Only check for exceptions if object was either readable
1100605870fe [Bug #1011606] Only check file descriptors for exceptional conditions if the fd is readable or writable
Andrew M. Kuchling <amk@amk.ca>
parents: 33120
diff changeset
174 # or writable.
1100605870fe [Bug #1011606] Only check file descriptors for exceptional conditions if the fd is readable or writable
Andrew M. Kuchling <amk@amk.ca>
parents: 33120
diff changeset
175 flags |= select.POLLERR | select.POLLHUP | select.POLLNVAL
16212
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
176 pollster.register(fd, flags)
20739
3b87740d1bbc Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20519
diff changeset
177 try:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
178 r = pollster.poll(timeout)
20739
3b87740d1bbc Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20519
diff changeset
179 except select.error, err:
48035
644fa7a240c8 Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents: 47934
diff changeset
180 if err.args[0] != EINTR:
20739
3b87740d1bbc Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20519
diff changeset
181 raise
3b87740d1bbc Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20519
diff changeset
182 r = []
16212
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
183 for fd, flags in r:
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
184 obj = map.get(fd)
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
185 if obj is None:
20325
8585535e404b Patch #468647: Fix exception propagation in asyncore.
Martin v. Löwis <martin@v.loewis.de>
parents: 20233
diff changeset
186 continue
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
187 readwrite(obj, flags)
16212
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
188
30754
8b2eadfb8f0c [Part of patch #648322] Delete the poll2() function, which uses a 'poll' extension module that was once part of Medusa. Contributed by Kjetil Jacobsen
Andrew M. Kuchling <amk@amk.ca>
parents: 30752
diff changeset
189 poll3 = poll2 # Alias for backward compatibility
8b2eadfb8f0c [Part of patch #648322] Delete the poll2() function, which uses a 'poll' extension module that was once part of Medusa. Contributed by Kjetil Jacobsen
Andrew M. Kuchling <amk@amk.ca>
parents: 30752
diff changeset
190
32447
7db4c586e1d6 Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents: 32349
diff changeset
191 def loop(timeout=30.0, use_poll=False, map=None, count=None):
16212
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
192 if map is None:
25166
b8b92b3f5b17 A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22830
diff changeset
193 map = socket_map
16212
1df40d7724cc Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents: 15857
diff changeset
194
30754
8b2eadfb8f0c [Part of patch #648322] Delete the poll2() function, which uses a 'poll' extension module that was once part of Medusa. Contributed by Kjetil Jacobsen
Andrew M. Kuchling <amk@amk.ca>
parents: 30752
diff changeset
195 if use_poll and hasattr(select, 'poll'):
8b2eadfb8f0c [Part of patch #648322] Delete the poll2() function, which uses a 'poll' extension module that was once part of Medusa. Contributed by Kjetil Jacobsen
Andrew M. Kuchling <amk@amk.ca>
parents: 30752
diff changeset
196 poll_fun = poll2
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
197 else:
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
198 poll_fun = poll
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
32447
7db4c586e1d6 Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents: 32349
diff changeset
200 if count is None:
7db4c586e1d6 Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents: 32349
diff changeset
201 while map:
7db4c586e1d6 Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents: 32349
diff changeset
202 poll_fun(timeout, map)
7db4c586e1d6 Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents: 32349
diff changeset
203
7db4c586e1d6 Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents: 32349
diff changeset
204 else:
7db4c586e1d6 Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents: 32349
diff changeset
205 while map and count > 0:
7db4c586e1d6 Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents: 32349
diff changeset
206 poll_fun(timeout, map)
7db4c586e1d6 Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents: 32349
diff changeset
207 count = count - 1
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 class dispatcher:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
210
31747
14f8c933f88c [Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents: 31744
diff changeset
211 debug = False
14f8c933f88c [Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents: 31744
diff changeset
212 connected = False
14f8c933f88c [Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents: 31744
diff changeset
213 accepting = False
14f8c933f88c [Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents: 31744
diff changeset
214 closing = False
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
215 addr = None
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
216 ignore_log_types = frozenset(['warning'])
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
218 def __init__(self, sock=None, map=None):
30752
d770dc264212 [Bug #758241] When you use asyncore with a non-default map, methods
Andrew M. Kuchling <amk@amk.ca>
parents: 30723
diff changeset
219 if map is None:
d770dc264212 [Bug #758241] When you use asyncore with a non-default map, methods
Andrew M. Kuchling <amk@amk.ca>
parents: 30723
diff changeset
220 self._map = socket_map
d770dc264212 [Bug #758241] When you use asyncore with a non-default map, methods
Andrew M. Kuchling <amk@amk.ca>
parents: 30723
diff changeset
221 else:
d770dc264212 [Bug #758241] When you use asyncore with a non-default map, methods
Andrew M. Kuchling <amk@amk.ca>
parents: 30723
diff changeset
222 self._map = map
d770dc264212 [Bug #758241] When you use asyncore with a non-default map, methods
Andrew M. Kuchling <amk@amk.ca>
parents: 30723
diff changeset
223
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
224 self._fileno = None
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
225
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
226 if sock:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
227 # Set to nonblocking just to make sure for cases where we
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
228 # get a socket from a blocking source.
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
229 sock.setblocking(0)
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
230 self.set_socket(sock, map)
31747
14f8c933f88c [Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents: 31744
diff changeset
231 self.connected = True
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
232 # The constructor no longer requires that the socket
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
233 # passed be connected.
21406
9c80b5af8121 Partial fix for problem in SF buf #487458
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20770
diff changeset
234 try:
9c80b5af8121 Partial fix for problem in SF buf #487458
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20770
diff changeset
235 self.addr = sock.getpeername()
47554
bbe2077a401b Fixed test to reflect new filedispatcher semantics, as well as two
Josiah Carlson <josiah.carlson@gmail.com>
parents: 47539
diff changeset
236 except socket.error, err:
48035
644fa7a240c8 Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents: 47934
diff changeset
237 if err.args[0] == ENOTCONN:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
238 # To handle the case where we got an unconnected
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
239 # socket.
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
240 self.connected = False
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
241 else:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
242 # The socket is broken in some unknown way, alert
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
243 # the user and remove it from the map (to prevent
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
244 # polling of broken sockets).
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
245 self.del_channel(map)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
246 raise
20739
3b87740d1bbc Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20519
diff changeset
247 else:
3b87740d1bbc Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20519
diff changeset
248 self.socket = None
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
249
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
250 def __repr__(self):
20519
d01c5f29915a Reindent __repr__.
Martin v. Löwis <martin@v.loewis.de>
parents: 20509
diff changeset
251 status = [self.__class__.__module__+"."+self.__class__.__name__]
d01c5f29915a Reindent __repr__.
Martin v. Löwis <martin@v.loewis.de>
parents: 20509
diff changeset
252 if self.accepting and self.addr:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
253 status.append('listening')
20519
d01c5f29915a Reindent __repr__.
Martin v. Löwis <martin@v.loewis.de>
parents: 20509
diff changeset
254 elif self.connected:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
255 status.append('connected')
20519
d01c5f29915a Reindent __repr__.
Martin v. Löwis <martin@v.loewis.de>
parents: 20509
diff changeset
256 if self.addr is not None:
d01c5f29915a Reindent __repr__.
Martin v. Löwis <martin@v.loewis.de>
parents: 20509
diff changeset
257 try:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
258 status.append('%s:%d' % self.addr)
20519
d01c5f29915a Reindent __repr__.
Martin v. Löwis <martin@v.loewis.de>
parents: 20509
diff changeset
259 except TypeError:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
260 status.append(repr(self.addr))
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
261 return '<%s at %#x>' % (' '.join(status), id(self))
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
262
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
263 def add_channel(self, map=None):
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
264 #self.log_info('adding channel %s' % self)
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
265 if map is None:
30752
d770dc264212 [Bug #758241] When you use asyncore with a non-default map, methods
Andrew M. Kuchling <amk@amk.ca>
parents: 30723
diff changeset
266 map = self._map
25249
1dd7e51fe0be Fiddle with compact_traceback().
Guido van Rossum <guido@python.org>
parents: 25226
diff changeset
267 map[self._fileno] = self
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
268
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
269 def del_channel(self, map=None):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
270 fd = self._fileno
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
271 if map is None:
30752
d770dc264212 [Bug #758241] When you use asyncore with a non-default map, methods
Andrew M. Kuchling <amk@amk.ca>
parents: 30723
diff changeset
272 map = self._map
48134
f0bd37ea6940 Remove a dict.has_key() use to silence a warning when running under -3.
Brett Cannon <bcannon@gmail.com>
parents: 48035
diff changeset
273 if fd in map:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
274 #self.log_info('closing channel %d:%s' % (fd, self))
25249
1dd7e51fe0be Fiddle with compact_traceback().
Guido van Rossum <guido@python.org>
parents: 25226
diff changeset
275 del map[fd]
31480
91af65b932d6 SF bug #892492: Multiple close() for asyncore.dispatcher.
Raymond Hettinger <python@rcn.com>
parents: 30754
diff changeset
276 self._fileno = None
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
277
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
278 def create_socket(self, family, type):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
279 self.family_and_type = family, type
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
280 sock = socket.socket(family, type)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
281 sock.setblocking(0)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
282 self.set_socket(sock)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
283
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
284 def set_socket(self, sock, map=None):
20739
3b87740d1bbc Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20519
diff changeset
285 self.socket = sock
3b87740d1bbc Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20519
diff changeset
286 ## self.__dict__['socket'] = sock
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
287 self._fileno = sock.fileno()
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
288 self.add_channel(map)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
289
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
290 def set_reuse_addr(self):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
291 # try to re-use a server port if possible
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
292 try:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
293 self.socket.setsockopt(
33120
d7fcdf9776c4 Back out patch #982681
Andrew M. Kuchling <amk@amk.ca>
parents: 32736
diff changeset
294 socket.SOL_SOCKET, socket.SO_REUSEADDR,
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
295 self.socket.getsockopt(socket.SOL_SOCKET,
33120
d7fcdf9776c4 Back out patch #982681
Andrew M. Kuchling <amk@amk.ca>
parents: 32736
diff changeset
296 socket.SO_REUSEADDR) | 1
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
297 )
17794
1fb3eca8969e <socket>.getsockopt() and <socket>.setsockopt() can only raise socket.error,
Fred Drake <fdrake@acm.org>
parents: 17763
diff changeset
298 except socket.error:
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
299 pass
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 32697
diff changeset
300
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
301 # ==================================================
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
302 # predicates for select()
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
303 # these are used as filters for the lists of sockets
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
304 # to pass to select().
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
305 # ==================================================
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
306
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
307 def readable(self):
22830
a9dd5e6d6805 Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents: 22824
diff changeset
308 return True
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
31749
1d3e48ccad57 [Part of patch #909005] Remove Mac code for writable
Andrew M. Kuchling <amk@amk.ca>
parents: 31748
diff changeset
310 def writable(self):
1d3e48ccad57 [Part of patch #909005] Remove Mac code for writable
Andrew M. Kuchling <amk@amk.ca>
parents: 31748
diff changeset
311 return True
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
312
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
313 # ==================================================
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
314 # socket object methods.
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
315 # ==================================================
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
317 def listen(self, num):
31747
14f8c933f88c [Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents: 31744
diff changeset
318 self.accepting = True
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
319 if os.name == 'nt' and num > 5:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
320 num = 5
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
321 return self.socket.listen(num)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
322
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
323 def bind(self, addr):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
324 self.addr = addr
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
325 return self.socket.bind(addr)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
326
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
327 def connect(self, address):
31747
14f8c933f88c [Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents: 31744
diff changeset
328 self.connected = False
20740
ad45d22b195d Use connect_ex() instead of connect().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20739
diff changeset
329 err = self.socket.connect_ex(address)
26801
299621eca9ad Add XXX about Winsock error values
Guido van Rossum <guido@python.org>
parents: 26044
diff changeset
330 # XXX Should interpret Winsock return values
20740
ad45d22b195d Use connect_ex() instead of connect().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20739
diff changeset
331 if err in (EINPROGRESS, EALREADY, EWOULDBLOCK):
ad45d22b195d Use connect_ex() instead of connect().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20739
diff changeset
332 return
ad45d22b195d Use connect_ex() instead of connect().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20739
diff changeset
333 if err in (0, EISCONN):
ad45d22b195d Use connect_ex() instead of connect().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20739
diff changeset
334 self.addr = address
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
335 self.handle_connect_event()
20770
eba63dd0c2f5 /F observes that we need an else: in connect()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20740
diff changeset
336 else:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
337 raise socket.error(err, errorcode[err])
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
338
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
339 def accept(self):
22824
f68ddf7f21f8 Replace use of apply() with extended call syntax.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22285
diff changeset
340 # XXX can return either an address pair or None
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
341 try:
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
342 conn, addr = self.socket.accept()
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
343 return conn, addr
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
344 except socket.error, why:
48035
644fa7a240c8 Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents: 47934
diff changeset
345 if why.args[0] == EWOULDBLOCK:
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
346 pass
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
347 else:
32528
67539cfcf09f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32516
diff changeset
348 raise
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
350 def send(self, data):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
351 try:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
352 result = self.socket.send(data)
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
353 return result
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
354 except socket.error, why:
48035
644fa7a240c8 Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents: 47934
diff changeset
355 if why.args[0] == EWOULDBLOCK:
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
356 return 0
48035
644fa7a240c8 Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents: 47934
diff changeset
357 elif why.args[0] in (ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED):
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
358 self.handle_close()
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
359 return 0
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
360 else:
32528
67539cfcf09f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32516
diff changeset
361 raise
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
362
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
363 def recv(self, buffer_size):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
364 try:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
365 data = self.socket.recv(buffer_size)
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
366 if not data:
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
367 # a closed connection is indicated by signaling
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
368 # a read condition, and having recv() return 0.
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
369 self.handle_close()
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
370 return ''
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
371 else:
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
372 return data
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
373 except socket.error, why:
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
374 # winsock sometimes throws ENOTCONN
48035
644fa7a240c8 Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents: 47934
diff changeset
375 if why.args[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED]:
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
376 self.handle_close()
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
377 return ''
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
378 else:
32528
67539cfcf09f Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32516
diff changeset
379 raise
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
380
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
381 def close(self):
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
382 self.connected = False
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
383 self.accepting = False
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
384 self.del_channel()
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
385 try:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
386 self.socket.close()
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
387 except socket.error, why:
48035
644fa7a240c8 Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents: 47934
diff changeset
388 if why.args[0] not in (ENOTCONN, EBADF):
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
389 raise
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
391 # cheap inheritance, used to pass all other attribute
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
392 # references to the underlying socket object.
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
393 def __getattr__(self, attr):
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
394 return getattr(self.socket, attr)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
27883
520a8c0c9a25 Fix comment typo
Andrew M. Kuchling <amk@amk.ca>
parents: 26801
diff changeset
396 # log and log_info may be overridden to provide more sophisticated
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
397 # logging and warning methods. In general, log is for 'hit' logging
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14509
diff changeset
398 # and 'log_info' is for informational, warning and error logging.
14473
9537e7893f3c Match Sam Rushing's current version of asyncore.py and asynchat.py
Andrew M. Kuchling <amk@amk.ca>
parents: 11851
diff changeset
399
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
400 def log(self, message):
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
401 sys.stderr.write('log: %s\n' % str(message))
14473
9537e7893f3c Match Sam Rushing's current version of asyncore.py and asynchat.py
Andrew M. Kuchling <amk@amk.ca>
parents: 11851
diff changeset
402
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
403 def log_info(self, message, type='info'):
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
404 if type not in self.ignore_log_types:
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
405 print '%s: %s' % (type, message)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
406
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
407 def handle_read_event(self):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
408 if self.accepting:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
409 # accepting sockets are never connected, they "spawn" new
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
410 # sockets that are connected
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
411 self.handle_accept()
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
412 elif not self.connected:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
413 self.handle_connect_event()
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
414 self.handle_read()
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
415 else:
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
416 self.handle_read()
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
417
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
418 def handle_connect_event(self):
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
419 self.connected = True
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
420 self.handle_connect()
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
421
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
422 def handle_write_event(self):
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
423 if self.accepting:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
424 # Accepting sockets shouldn't get a write event.
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
425 # We will pretend it didn't happen.
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
426 return
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
427
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
428 if not self.connected:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
429 #check for errors
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
430 err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
431 if err != 0:
47554
bbe2077a401b Fixed test to reflect new filedispatcher semantics, as well as two
Josiah Carlson <josiah.carlson@gmail.com>
parents: 47539
diff changeset
432 raise socket.error(err, _strerror(err))
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
433
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
434 self.handle_connect_event()
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
435 self.handle_write()
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
436
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
437 def handle_expt_event(self):
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
438 # handle_expt_event() is called if there might be an error on the
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
439 # socket, or if there is OOB data
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
440 # check for the error condition first
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
441 err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
442 if err != 0:
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
443 # we can get here when select.select() says that there is an
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
444 # exceptional condition on the socket
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
445 # since there is an error, we'll go ahead and close the socket
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
446 # like we would in a subclassed handle_read() that received no
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
447 # data
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
448 self.handle_close()
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
449 else:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
450 self.handle_expt()
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
451
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
452 def handle_error(self):
19127
78752eae132e Remove hard-coded NT constants that are already defined in errno on NT.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17794
diff changeset
453 nil, t, v, tbinfo = compact_traceback()
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
454
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
455 # sometimes a user repr method will crash.
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
456 try:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
457 self_repr = repr(self)
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
458 except:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
459 self_repr = '<__repr__(self) failed for object at %0x>' % id(self)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
460
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
461 self.log_info(
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
462 'uncaptured python exception, closing channel %s (%s:%s %s)' % (
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
463 self_repr,
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
464 t,
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
465 v,
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
466 tbinfo
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
467 ),
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
468 'error'
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
469 )
47907
d7fd32ce07aa Fixed bugs 760475, 953599, and 1519.
Josiah Carlson <josiah.carlson@gmail.com>
parents: 47554
diff changeset
470 self.handle_close()
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
471
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
472 def handle_expt(self):
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
473 self.log_info('unhandled incoming priority event', 'warning')
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
474
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
475 def handle_read(self):
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
476 self.log_info('unhandled read event', 'warning')
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
477
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
478 def handle_write(self):
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
479 self.log_info('unhandled write event', 'warning')
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
480
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
481 def handle_connect(self):
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
482 self.log_info('unhandled connect event', 'warning')
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
483
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
484 def handle_accept(self):
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
485 self.log_info('unhandled accept event', 'warning')
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
486
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
487 def handle_close(self):
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
488 self.log_info('unhandled close event', 'warning')
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
489 self.close()
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
490
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 # ---------------------------------------------------------------------------
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 # adds simple buffered output capability, useful for simple clients.
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 # [for more sophisticated usage use asynchat.async_chat]
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 # ---------------------------------------------------------------------------
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
495
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
496 class dispatcher_with_send(dispatcher):
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
497
31752
c2b01aa490e1 [Part of patch #909005] Added map parameter for file_dispatcher and dispatcher_with_send
Andrew M. Kuchling <amk@amk.ca>
parents: 31751
diff changeset
498 def __init__(self, sock=None, map=None):
c2b01aa490e1 [Part of patch #909005] Added map parameter for file_dispatcher and dispatcher_with_send
Andrew M. Kuchling <amk@amk.ca>
parents: 31751
diff changeset
499 dispatcher.__init__(self, sock, map)
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
500 self.out_buffer = ''
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
501
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
502 def initiate_send(self):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
503 num_sent = 0
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
504 num_sent = dispatcher.send(self, self.out_buffer[:512])
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
505 self.out_buffer = self.out_buffer[num_sent:]
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
506
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
507 def handle_write(self):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
508 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
509
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
510 def writable(self):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
511 return (not self.connected) or len(self.out_buffer)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
512
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
513 def send(self, data):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
514 if self.debug:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
515 self.log_info('sending %s' % repr(data))
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
516 self.out_buffer = self.out_buffer + data
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
517 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
518
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 # ---------------------------------------------------------------------------
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 # used for debugging.
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 # ---------------------------------------------------------------------------
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
522
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
523 def compact_traceback():
25249
1dd7e51fe0be Fiddle with compact_traceback().
Guido van Rossum <guido@python.org>
parents: 25226
diff changeset
524 t, v, tb = sys.exc_info()
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
525 tbinfo = []
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
526 if not tb: # Must have a traceback
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
527 raise AssertionError("traceback does not exist")
25249
1dd7e51fe0be Fiddle with compact_traceback().
Guido van Rossum <guido@python.org>
parents: 25226
diff changeset
528 while tb:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
529 tbinfo.append((
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
530 tb.tb_frame.f_code.co_filename,
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 14509
diff changeset
531 tb.tb_frame.f_code.co_name,
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
532 str(tb.tb_lineno)
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
533 ))
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
534 tb = tb.tb_next
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
535
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
536 # just to be safe
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
537 del tb
10934
c089020a7a1e Sam's latest versions
Guido van Rossum <guido@python.org>
parents: 9855
diff changeset
538
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
539 file, function, line = tbinfo[-1]
25249
1dd7e51fe0be Fiddle with compact_traceback().
Guido van Rossum <guido@python.org>
parents: 25226
diff changeset
540 info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo])
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
541 return (file, function, line), t, v, info
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
542
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
543 def close_all(map=None, ignore_all=False):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
544 if map is None:
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
545 map = socket_map
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
546 for x in map.values():
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
547 try:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
548 x.close()
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
549 except OSError, x:
48035
644fa7a240c8 Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents: 47934
diff changeset
550 if x.args[0] == EBADF:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
551 pass
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
552 elif not ignore_all:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
553 raise
53167
c54b784a56d5 Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 49766
diff changeset
554 except _reraised_exceptions:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
555 raise
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
556 except:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
557 if not ignore_all:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
558 raise
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
559 map.clear()
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
560
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 # Asynchronous File I/O:
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
562 #
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 # After a little research (reading man pages on various unixen, and
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 # digging through the linux kernel), I've determined that select()
30723
6f15e73b59f2 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 27883
diff changeset
565 # isn't meant for doing asynchronous file i/o.
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 # Heartening, though - reading linux/mm/filemap.c shows that linux
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 # supports asynchronous read-ahead. So _MOST_ of the time, the data
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 # will be sitting in memory for us already when we go to read it.
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 #
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 # What other OS's (besides NT) support async file i/o? [VMS?]
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 #
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 # Regardless, this is useful for pipes, and stdin/stdout...
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
573
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 if os.name == 'posix':
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
575 import fcntl
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
576
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
577 class file_wrapper:
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
578 # Here we override just enough to make a file
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
579 # look like a socket for the purposes of asyncore.
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
580 # The passed fd is automatically os.dup()'d
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
581
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
582 def __init__(self, fd):
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
583 self.fd = os.dup(fd)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
584
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
585 def recv(self, *args):
22824
f68ddf7f21f8 Replace use of apply() with extended call syntax.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22285
diff changeset
586 return os.read(self.fd, *args)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
587
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
588 def send(self, *args):
22824
f68ddf7f21f8 Replace use of apply() with extended call syntax.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22285
diff changeset
589 return os.write(self.fd, *args)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
590
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
591 read = recv
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
592 write = send
14473
9537e7893f3c Match Sam Rushing's current version of asyncore.py and asynchat.py
Andrew M. Kuchling <amk@amk.ca>
parents: 11851
diff changeset
593
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
594 def close(self):
32581
0dd473a8e53c Return value from .close(); move .set_file up
Andrew M. Kuchling <amk@amk.ca>
parents: 32528
diff changeset
595 os.close(self.fd)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
596
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
597 def fileno(self):
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
598 return self.fd
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
599
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
600 class file_dispatcher(dispatcher):
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
601
31752
c2b01aa490e1 [Part of patch #909005] Added map parameter for file_dispatcher and dispatcher_with_send
Andrew M. Kuchling <amk@amk.ca>
parents: 31751
diff changeset
602 def __init__(self, fd, map=None):
c2b01aa490e1 [Part of patch #909005] Added map parameter for file_dispatcher and dispatcher_with_send
Andrew M. Kuchling <amk@amk.ca>
parents: 31751
diff changeset
603 dispatcher.__init__(self, None, map)
31747
14f8c933f88c [Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents: 31744
diff changeset
604 self.connected = True
47539
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
605 try:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
606 fd = fd.fileno()
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
607 except AttributeError:
53c60c7cccd9 Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents: 35505
diff changeset
608 pass
32581
0dd473a8e53c Return value from .close(); move .set_file up
Andrew M. Kuchling <amk@amk.ca>
parents: 32528
diff changeset
609 self.set_file(fd)
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
610 # set it to non-blocking mode
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
611 flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
17763
65e26113b3fb Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents: 17652
diff changeset
612 flags = flags | os.O_NONBLOCK
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
613 fcntl.fcntl(fd, fcntl.F_SETFL, flags)
9855
30c1c40cd80f A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff changeset
614
25226
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
615 def set_file(self, fd):
19904750c365 I don't think it's safe to use map.iteritems() in the various poll
Guido van Rossum <guido@python.org>
parents: 25166
diff changeset
616 self.socket = file_wrapper(fd)
49766
f04df10e2050 This fixes issue 4332 in 2.6 maintenance.
Josiah Carlson <josiah.carlson@gmail.com>
parents: 49065
diff changeset
617 self._fileno = self.socket.fileno()
14509
a0392f438960 Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents: 14473
diff changeset
618 self.add_channel()