Mercurial > cpython-lru_cache-weakref
annotate Lib/asyncore.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 8c19c9914c22 |
| children | 90a46f8943d0 |
| 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 |
|
61324
cc92502de4c9
Fix asyncore issues 8573 and 8483: _strerror might throw ValueError; asyncore.__getattr__ cheap inheritance caused confusing error messages when accessing undefined class attributes; added an alias for __str__ which now is used as a fallback for __repr__
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
54547
diff
changeset
|
53 import warnings |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 import os |
|
64251
b95ec171f503
Merged revisions 84284 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63717
diff
changeset
|
56 from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \ |
|
68064
965ab9911fcd
Merged revisions 88722 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
65861
diff
changeset
|
57 ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EBADF, ECONNABORTED, EPIPE, EAGAIN, \ |
|
965ab9911fcd
Merged revisions 88722 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
65861
diff
changeset
|
58 errorcode |
|
965ab9911fcd
Merged revisions 88722 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
65861
diff
changeset
|
59 |
|
965ab9911fcd
Merged revisions 88722 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
65861
diff
changeset
|
60 _DISCONNECTED = frozenset((ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE, |
|
965ab9911fcd
Merged revisions 88722 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
65861
diff
changeset
|
61 EBADF)) |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 |
|
14473
9537e7893f3c
Match Sam Rushing's current version of asyncore.py and asynchat.py
Andrew M. Kuchling <amk@amk.ca>
parents:
11851
diff
changeset
|
63 try: |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
64 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
|
65 except NameError: |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
66 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
|
67 |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
68 def _strerror(err): |
|
61326
54efaf6d2f89
Merged revisions 80875 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
53167
diff
changeset
|
69 try: |
|
61696
f247146f8a95
Merged revisions 81294 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
61326
diff
changeset
|
70 return os.strerror(err) |
|
f247146f8a95
Merged revisions 81294 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
61326
diff
changeset
|
71 except (ValueError, OverflowError, NameError): |
|
61326
54efaf6d2f89
Merged revisions 80875 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
53167
diff
changeset
|
72 if err in errorcode: |
|
54efaf6d2f89
Merged revisions 80875 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
53167
diff
changeset
|
73 return errorcode[err] |
|
54efaf6d2f89
Merged revisions 80875 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
53167
diff
changeset
|
74 return "Unknown error %s" %err |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
75 |
|
35505
26ba3d4f4fc4
No need to import exceptions, they are builtins
Neal Norwitz <nnorwitz@gmail.com>
parents:
33388
diff
changeset
|
76 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
|
77 pass |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 |
|
53167
c54b784a56d5
Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
49766
diff
changeset
|
79 _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
|
80 |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
81 def read(obj): |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
82 try: |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
83 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
|
84 except _reraised_exceptions: |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
85 raise |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
86 except: |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
87 obj.handle_error() |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
88 |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
89 def write(obj): |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
90 try: |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
91 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
|
92 except _reraised_exceptions: |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
93 raise |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
94 except: |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
95 obj.handle_error() |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
96 |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
97 def _exception(obj): |
|
32585
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
98 try: |
|
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
99 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
|
100 except _reraised_exceptions: |
|
32585
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
101 raise |
|
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
102 except: |
|
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
103 obj.handle_error() |
|
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
104 |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
105 def readwrite(obj, flags): |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
106 try: |
|
53167
c54b784a56d5
Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
49766
diff
changeset
|
107 if flags & select.POLLIN: |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
108 obj.handle_read_event() |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
109 if flags & select.POLLOUT: |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
110 obj.handle_write_event() |
|
63732
0e4529d733ac
Issue #5798: Handle select.poll flag oddities properly on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents:
63718
diff
changeset
|
111 if flags & select.POLLPRI: |
|
0e4529d733ac
Issue #5798: Handle select.poll flag oddities properly on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents:
63718
diff
changeset
|
112 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
|
113 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
|
114 obj.handle_close() |
|
63732
0e4529d733ac
Issue #5798: Handle select.poll flag oddities properly on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents:
63718
diff
changeset
|
115 except socket.error, e: |
|
68064
965ab9911fcd
Merged revisions 88722 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
65861
diff
changeset
|
116 if e.args[0] not in _DISCONNECTED: |
|
63732
0e4529d733ac
Issue #5798: Handle select.poll flag oddities properly on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents:
63718
diff
changeset
|
117 obj.handle_error() |
|
0e4529d733ac
Issue #5798: Handle select.poll flag oddities properly on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents:
63718
diff
changeset
|
118 else: |
|
0e4529d733ac
Issue #5798: Handle select.poll flag oddities properly on OS X.
Mark Dickinson <dickinsm@gmail.com>
parents:
63718
diff
changeset
|
119 obj.handle_close() |
|
53167
c54b784a56d5
Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
49766
diff
changeset
|
120 except _reraised_exceptions: |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
121 raise |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
122 except: |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
123 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
|
124 |
|
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
|
125 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
|
126 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
|
127 map = socket_map |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
128 if map: |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 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
|
133 if is_r: |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
134 r.append(fd) |
|
71333
16bc59d37866
Issue #12502: asyncore: fix polling loop with AF_UNIX sockets.
Charles-François Natali <neologix@free.fr>
parents:
68256
diff
changeset
|
135 # accepting sockets should not be writable |
|
16bc59d37866
Issue #12502: asyncore: fix polling loop with AF_UNIX sockets.
Charles-François Natali <neologix@free.fr>
parents:
68256
diff
changeset
|
136 if is_w and not obj.accepting: |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
137 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
|
138 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
|
139 e.append(fd) |
|
25404
75c4a67dcb8c
On Windows, select() does not accept empty lists.
Thomas Heller <theller@ctypes.org>
parents:
25249
diff
changeset
|
140 if [] == r == w == e: |
|
75c4a67dcb8c
On Windows, select() does not accept empty lists.
Thomas Heller <theller@ctypes.org>
parents:
25249
diff
changeset
|
141 time.sleep(timeout) |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
142 return |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
143 |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
144 try: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
145 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
|
146 except select.error, err: |
|
48035
644fa7a240c8
Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents:
47934
diff
changeset
|
147 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
|
148 raise |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
149 else: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
150 return |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
152 for fd in r: |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
153 obj = map.get(fd) |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
154 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
|
155 continue |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
156 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
|
157 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
158 for fd in w: |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
159 obj = map.get(fd) |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
160 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
|
161 continue |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
162 write(obj) |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 |
|
32585
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
164 for fd in e: |
|
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
165 obj = map.get(fd) |
|
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
166 if obj is None: |
|
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
167 continue |
|
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
168 _exception(obj) |
|
a03bca1654d0
In poll(), check connections for exceptional conditions
Andrew M. Kuchling <amk@amk.ca>
parents:
32581
diff
changeset
|
169 |
|
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
|
170 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
|
171 # 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
|
172 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
|
173 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
|
174 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
|
175 # 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
|
176 timeout = int(timeout*1000) |
|
16212
1df40d7724cc
Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents:
15857
diff
changeset
|
177 pollster = select.poll() |
|
1df40d7724cc
Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents:
15857
diff
changeset
|
178 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
|
179 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
|
180 flags = 0 |
|
16212
1df40d7724cc
Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents:
15857
diff
changeset
|
181 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
|
182 flags |= select.POLLIN | select.POLLPRI |
|
71333
16bc59d37866
Issue #12502: asyncore: fix polling loop with AF_UNIX sockets.
Charles-François Natali <neologix@free.fr>
parents:
68256
diff
changeset
|
183 # accepting sockets should not be writable |
|
16bc59d37866
Issue #12502: asyncore: fix polling loop with AF_UNIX sockets.
Charles-François Natali <neologix@free.fr>
parents:
68256
diff
changeset
|
184 if obj.writable() and not obj.accepting: |
|
32516
4549e75a6243
For readable() objects, the previous value of 'flags' was ignored.
Andrew M. Kuchling <amk@amk.ca>
parents:
32447
diff
changeset
|
185 flags |= select.POLLOUT |
|
16212
1df40d7724cc
Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents:
15857
diff
changeset
|
186 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
|
187 # 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
|
188 # 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
|
189 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
|
190 pollster.register(fd, flags) |
|
20739
3b87740d1bbc
Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20519
diff
changeset
|
191 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
|
192 r = pollster.poll(timeout) |
|
20739
3b87740d1bbc
Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20519
diff
changeset
|
193 except select.error, err: |
|
48035
644fa7a240c8
Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents:
47934
diff
changeset
|
194 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
|
195 raise |
|
3b87740d1bbc
Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20519
diff
changeset
|
196 r = [] |
|
16212
1df40d7724cc
Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents:
15857
diff
changeset
|
197 for fd, flags in r: |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
198 obj = map.get(fd) |
|
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
199 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
|
200 continue |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
201 readwrite(obj, flags) |
|
16212
1df40d7724cc
Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents:
15857
diff
changeset
|
202 |
|
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
|
203 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
|
204 |
|
32447
7db4c586e1d6
Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents:
32349
diff
changeset
|
205 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
|
206 if map is None: |
|
25166
b8b92b3f5b17
A little refactoring.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22830
diff
changeset
|
207 map = socket_map |
|
16212
1df40d7724cc
Updated version of asyncore.py from Sam Rushing:
Andrew M. Kuchling <amk@amk.ca>
parents:
15857
diff
changeset
|
208 |
|
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
|
209 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
|
210 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
|
211 else: |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
212 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
|
213 |
|
32447
7db4c586e1d6
Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents:
32349
diff
changeset
|
214 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
|
215 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
|
216 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
|
217 |
|
7db4c586e1d6
Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents:
32349
diff
changeset
|
218 else: |
|
7db4c586e1d6
Check in the updated version of patch #957240, which doesn't rely
Michael W. Hudson <mwh@python.net>
parents:
32349
diff
changeset
|
219 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
|
220 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
|
221 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
|
222 |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 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
|
224 |
|
31747
14f8c933f88c
[Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents:
31744
diff
changeset
|
225 debug = False |
|
14f8c933f88c
[Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents:
31744
diff
changeset
|
226 connected = False |
|
14f8c933f88c
[Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents:
31744
diff
changeset
|
227 accepting = False |
|
75880
8c19c9914c22
fix #10340: properly handle EINVAL on OSX and also avoid to call handle_connect() in case of a disconnetected socket which is not meant to connect.
Giampaolo Rodola' <g.rodola@gmail.com>
parents:
75840
diff
changeset
|
228 connecting = False |
|
31747
14f8c933f88c
[Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents:
31744
diff
changeset
|
229 closing = False |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
230 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
|
231 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
|
232 |
|
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
|
233 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
|
234 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
|
235 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
|
236 else: |
|
d770dc264212
[Bug #758241] When you use asyncore with a non-default map, methods
Andrew M. Kuchling <amk@amk.ca>
parents:
30723
diff
changeset
|
237 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
|
238 |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
239 self._fileno = None |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
240 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
241 if sock: |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
242 # 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
|
243 # 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
|
244 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
|
245 self.set_socket(sock, map) |
|
31747
14f8c933f88c
[Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents:
31744
diff
changeset
|
246 self.connected = True |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
247 # 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
|
248 # passed be connected. |
|
21406
9c80b5af8121
Partial fix for problem in SF buf #487458
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20770
diff
changeset
|
249 try: |
|
9c80b5af8121
Partial fix for problem in SF buf #487458
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20770
diff
changeset
|
250 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
|
251 except socket.error, err: |
|
75880
8c19c9914c22
fix #10340: properly handle EINVAL on OSX and also avoid to call handle_connect() in case of a disconnetected socket which is not meant to connect.
Giampaolo Rodola' <g.rodola@gmail.com>
parents:
75840
diff
changeset
|
252 if err.args[0] in (ENOTCONN, EINVAL): |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
253 # 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
|
254 # socket. |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
255 self.connected = False |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
256 else: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
257 # 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
|
258 # 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
|
259 # polling of broken sockets). |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
260 self.del_channel(map) |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
261 raise |
|
20739
3b87740d1bbc
Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20519
diff
changeset
|
262 else: |
|
3b87740d1bbc
Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20519
diff
changeset
|
263 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
|
264 |
|
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
|
265 def __repr__(self): |
| 20519 | 266 status = [self.__class__.__module__+"."+self.__class__.__name__] |
| 267 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
|
268 status.append('listening') |
| 20519 | 269 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
|
270 status.append('connected') |
| 20519 | 271 if self.addr is not None: |
| 272 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
|
273 status.append('%s:%d' % self.addr) |
| 20519 | 274 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
|
275 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
|
276 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
|
277 |
|
61324
cc92502de4c9
Fix asyncore issues 8573 and 8483: _strerror might throw ValueError; asyncore.__getattr__ cheap inheritance caused confusing error messages when accessing undefined class attributes; added an alias for __str__ which now is used as a fallback for __repr__
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
54547
diff
changeset
|
278 __str__ = __repr__ |
|
cc92502de4c9
Fix asyncore issues 8573 and 8483: _strerror might throw ValueError; asyncore.__getattr__ cheap inheritance caused confusing error messages when accessing undefined class attributes; added an alias for __str__ which now is used as a fallback for __repr__
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
54547
diff
changeset
|
279 |
|
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
|
280 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
|
281 #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
|
282 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
|
283 map = self._map |
|
25249
1dd7e51fe0be
Fiddle with compact_traceback().
Guido van Rossum <guido@python.org>
parents:
25226
diff
changeset
|
284 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
|
285 |
|
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
|
286 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
|
287 fd = self._fileno |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
288 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
|
289 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
|
290 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
|
291 #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
|
292 del map[fd] |
|
31480
91af65b932d6
SF bug #892492: Multiple close() for asyncore.dispatcher.
Raymond Hettinger <python@rcn.com>
parents:
30754
diff
changeset
|
293 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
|
294 |
|
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 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
|
296 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
|
297 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
|
298 sock.setblocking(0) |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
299 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
|
300 |
|
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
|
301 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
|
302 self.socket = sock |
|
3b87740d1bbc
Fix for SF bug 453099 -- select not defensive
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20519
diff
changeset
|
303 ## 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
|
304 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
|
305 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
|
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 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
|
308 # 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
|
309 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
|
310 self.socket.setsockopt( |
| 33120 | 311 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
|
312 self.socket.getsockopt(socket.SOL_SOCKET, |
| 33120 | 313 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
|
314 ) |
|
17794
1fb3eca8969e
<socket>.getsockopt() and <socket>.setsockopt() can only raise socket.error,
Fred Drake <fdrake@acm.org>
parents:
17763
diff
changeset
|
315 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
|
316 pass |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
32697
diff
changeset
|
317 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
318 # ================================================== |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
319 # predicates for select() |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
320 # 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
|
321 # 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
|
322 # ================================================== |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 |
|
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
|
324 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
|
325 return True |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 |
|
31749
1d3e48ccad57
[Part of patch #909005] Remove Mac code for writable
Andrew M. Kuchling <amk@amk.ca>
parents:
31748
diff
changeset
|
327 def writable(self): |
|
1d3e48ccad57
[Part of patch #909005] Remove Mac code for writable
Andrew M. Kuchling <amk@amk.ca>
parents:
31748
diff
changeset
|
328 return True |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
330 # ================================================== |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
331 # socket object methods. |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
332 # ================================================== |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 |
|
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
|
334 def listen(self, num): |
|
31747
14f8c933f88c
[Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents:
31744
diff
changeset
|
335 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
|
336 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
|
337 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
|
338 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
|
339 |
|
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
|
340 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
|
341 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
|
342 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
|
343 |
|
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
|
344 def connect(self, address): |
|
31747
14f8c933f88c
[Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents:
31744
diff
changeset
|
345 self.connected = False |
|
75880
8c19c9914c22
fix #10340: properly handle EINVAL on OSX and also avoid to call handle_connect() in case of a disconnetected socket which is not meant to connect.
Giampaolo Rodola' <g.rodola@gmail.com>
parents:
75840
diff
changeset
|
346 self.connecting = True |
|
20740
ad45d22b195d
Use connect_ex() instead of connect().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20739
diff
changeset
|
347 err = self.socket.connect_ex(address) |
|
64251
b95ec171f503
Merged revisions 84284 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63717
diff
changeset
|
348 if err in (EINPROGRESS, EALREADY, EWOULDBLOCK) \ |
|
b95ec171f503
Merged revisions 84284 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63717
diff
changeset
|
349 or err == EINVAL and os.name in ('nt', 'ce'): |
|
75840
4e5ddde76dbe
Fix issue #13694: asynchronous connect in asyncore.dispatcher does not set addr.
Giampaolo Rodola' <g.rodola@gmail.com>
parents:
71333
diff
changeset
|
350 self.addr = address |
|
20740
ad45d22b195d
Use connect_ex() instead of connect().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20739
diff
changeset
|
351 return |
|
ad45d22b195d
Use connect_ex() instead of connect().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20739
diff
changeset
|
352 if err in (0, EISCONN): |
|
ad45d22b195d
Use connect_ex() instead of connect().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20739
diff
changeset
|
353 self.addr = address |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
354 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
|
355 else: |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
356 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
|
357 |
|
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
|
358 def accept(self): |
|
22824
f68ddf7f21f8
Replace use of apply() with extended call syntax.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22285
diff
changeset
|
359 # 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
|
360 try: |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
361 conn, addr = self.socket.accept() |
|
65861
f0300584c824
Fix Issue 6706: return None on connect() in case of EWOULDBLOCK/ECONNABORTED error.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64251
diff
changeset
|
362 except TypeError: |
|
f0300584c824
Fix Issue 6706: return None on connect() in case of EWOULDBLOCK/ECONNABORTED error.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64251
diff
changeset
|
363 return None |
|
f0300584c824
Fix Issue 6706: return None on connect() in case of EWOULDBLOCK/ECONNABORTED error.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64251
diff
changeset
|
364 except socket.error as why: |
|
68064
965ab9911fcd
Merged revisions 88722 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
65861
diff
changeset
|
365 if why.args[0] in (EWOULDBLOCK, ECONNABORTED, EAGAIN): |
|
65861
f0300584c824
Fix Issue 6706: return None on connect() in case of EWOULDBLOCK/ECONNABORTED error.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64251
diff
changeset
|
366 return None |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
367 else: |
|
32528
67539cfcf09f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32516
diff
changeset
|
368 raise |
|
65861
f0300584c824
Fix Issue 6706: return None on connect() in case of EWOULDBLOCK/ECONNABORTED error.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64251
diff
changeset
|
369 else: |
|
f0300584c824
Fix Issue 6706: return None on connect() in case of EWOULDBLOCK/ECONNABORTED error.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64251
diff
changeset
|
370 return conn, addr |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 |
|
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
|
372 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
|
373 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
|
374 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
|
375 return result |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
376 except socket.error, why: |
|
48035
644fa7a240c8
Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents:
47934
diff
changeset
|
377 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
|
378 return 0 |
|
68064
965ab9911fcd
Merged revisions 88722 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
65861
diff
changeset
|
379 elif why.args[0] in _DISCONNECTED: |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
380 self.handle_close() |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
381 return 0 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
382 else: |
|
32528
67539cfcf09f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32516
diff
changeset
|
383 raise |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 |
|
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
|
385 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
|
386 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
|
387 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
|
388 if not data: |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
389 # 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
|
390 # 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
|
391 self.handle_close() |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
392 return '' |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
393 else: |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
394 return data |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
395 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
|
396 # winsock sometimes throws ENOTCONN |
|
68064
965ab9911fcd
Merged revisions 88722 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
65861
diff
changeset
|
397 if why.args[0] in _DISCONNECTED: |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
398 self.handle_close() |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
399 return '' |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
400 else: |
|
32528
67539cfcf09f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32516
diff
changeset
|
401 raise |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
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 close(self): |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
404 self.connected = False |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
405 self.accepting = False |
|
75880
8c19c9914c22
fix #10340: properly handle EINVAL on OSX and also avoid to call handle_connect() in case of a disconnetected socket which is not meant to connect.
Giampaolo Rodola' <g.rodola@gmail.com>
parents:
75840
diff
changeset
|
406 self.connecting = False |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
407 self.del_channel() |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
408 try: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
409 self.socket.close() |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
410 except socket.error, why: |
|
48035
644fa7a240c8
Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents:
47934
diff
changeset
|
411 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
|
412 raise |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
414 # 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
|
415 # 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
|
416 def __getattr__(self, attr): |
|
61326
54efaf6d2f89
Merged revisions 80875 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
53167
diff
changeset
|
417 try: |
|
61324
cc92502de4c9
Fix asyncore issues 8573 and 8483: _strerror might throw ValueError; asyncore.__getattr__ cheap inheritance caused confusing error messages when accessing undefined class attributes; added an alias for __str__ which now is used as a fallback for __repr__
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
54547
diff
changeset
|
418 retattr = getattr(self.socket, attr) |
|
61326
54efaf6d2f89
Merged revisions 80875 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
53167
diff
changeset
|
419 except AttributeError: |
|
54efaf6d2f89
Merged revisions 80875 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
53167
diff
changeset
|
420 raise AttributeError("%s instance has no attribute '%s'" |
|
54efaf6d2f89
Merged revisions 80875 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
53167
diff
changeset
|
421 %(self.__class__.__name__, attr)) |
|
61324
cc92502de4c9
Fix asyncore issues 8573 and 8483: _strerror might throw ValueError; asyncore.__getattr__ cheap inheritance caused confusing error messages when accessing undefined class attributes; added an alias for __str__ which now is used as a fallback for __repr__
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
54547
diff
changeset
|
422 else: |
|
61328
9af960fbaccc
provides a clearer warning message when cheap inheritance with the underlying socket object is used
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
61324
diff
changeset
|
423 msg = "%(me)s.%(attr)s is deprecated. Use %(me)s.socket.%(attr)s " \ |
|
9af960fbaccc
provides a clearer warning message when cheap inheritance with the underlying socket object is used
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
61324
diff
changeset
|
424 "instead." % {'me': self.__class__.__name__, 'attr':attr} |
|
9af960fbaccc
provides a clearer warning message when cheap inheritance with the underlying socket object is used
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
61324
diff
changeset
|
425 warnings.warn(msg, DeprecationWarning, stacklevel=2) |
|
61324
cc92502de4c9
Fix asyncore issues 8573 and 8483: _strerror might throw ValueError; asyncore.__getattr__ cheap inheritance caused confusing error messages when accessing undefined class attributes; added an alias for __str__ which now is used as a fallback for __repr__
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
54547
diff
changeset
|
426 return retattr |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
427 |
| 27883 | 428 # 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
|
429 # 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
|
430 # 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
|
431 |
|
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
|
432 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
|
433 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
|
434 |
|
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
|
435 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
|
436 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
|
437 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
|
438 |
|
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
|
439 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
|
440 if self.accepting: |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
441 # 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
|
442 # 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
|
443 self.handle_accept() |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
444 elif not self.connected: |
|
75880
8c19c9914c22
fix #10340: properly handle EINVAL on OSX and also avoid to call handle_connect() in case of a disconnetected socket which is not meant to connect.
Giampaolo Rodola' <g.rodola@gmail.com>
parents:
75840
diff
changeset
|
445 if self.connecting: |
|
8c19c9914c22
fix #10340: properly handle EINVAL on OSX and also avoid to call handle_connect() in case of a disconnetected socket which is not meant to connect.
Giampaolo Rodola' <g.rodola@gmail.com>
parents:
75840
diff
changeset
|
446 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
|
447 self.handle_read() |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
448 else: |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
449 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
|
450 |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
451 def handle_connect_event(self): |
|
63717
583343578732
fix issue #2944: asyncore doesn't handle connection refused correctly (patch by Alexander Shigin)
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63477
diff
changeset
|
452 err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) |
|
583343578732
fix issue #2944: asyncore doesn't handle connection refused correctly (patch by Alexander Shigin)
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63477
diff
changeset
|
453 if err != 0: |
|
583343578732
fix issue #2944: asyncore doesn't handle connection refused correctly (patch by Alexander Shigin)
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63477
diff
changeset
|
454 raise socket.error(err, _strerror(err)) |
|
63718
b3fdd445e037
Merged revisions 83703 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63505
diff
changeset
|
455 self.handle_connect() |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
456 self.connected = True |
|
75880
8c19c9914c22
fix #10340: properly handle EINVAL on OSX and also avoid to call handle_connect() in case of a disconnetected socket which is not meant to connect.
Giampaolo Rodola' <g.rodola@gmail.com>
parents:
75840
diff
changeset
|
457 self.connecting = False |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
458 |
|
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 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
|
460 if self.accepting: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
461 # 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
|
462 # 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
|
463 return |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
464 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
465 if not self.connected: |
|
75880
8c19c9914c22
fix #10340: properly handle EINVAL on OSX and also avoid to call handle_connect() in case of a disconnetected socket which is not meant to connect.
Giampaolo Rodola' <g.rodola@gmail.com>
parents:
75840
diff
changeset
|
466 if self.connecting: |
|
8c19c9914c22
fix #10340: properly handle EINVAL on OSX and also avoid to call handle_connect() in case of a disconnetected socket which is not meant to connect.
Giampaolo Rodola' <g.rodola@gmail.com>
parents:
75840
diff
changeset
|
467 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
|
468 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
|
469 |
|
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
|
470 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
|
471 # 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
|
472 # 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
|
473 # 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
|
474 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
|
475 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
|
476 # 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
|
477 # 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
|
478 # 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
|
479 # 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
|
480 # data |
|
c54b784a56d5
Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
49766
diff
changeset
|
481 self.handle_close() |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
482 else: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
483 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
|
484 |
|
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
|
485 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
|
486 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
|
487 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
488 # 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
|
489 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
|
490 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
|
491 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
|
492 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
|
493 |
|
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
|
494 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
|
495 '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
|
496 self_repr, |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
497 t, |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
498 v, |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
499 tbinfo |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
500 ), |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
501 'error' |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
502 ) |
|
47907
d7fd32ce07aa
Fixed bugs 760475, 953599, and 1519.
Josiah Carlson <josiah.carlson@gmail.com>
parents:
47554
diff
changeset
|
503 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
|
504 |
|
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
|
505 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
|
506 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
|
507 |
|
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
|
508 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
|
509 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
|
510 |
|
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
|
511 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
|
512 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
|
513 |
|
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
|
514 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
|
515 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
|
516 |
|
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
|
517 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
|
518 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
|
519 |
|
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
|
520 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
|
521 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
|
522 self.close() |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
523 |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
524 # --------------------------------------------------------------------------- |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
525 # 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
|
526 # [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
|
527 # --------------------------------------------------------------------------- |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
528 |
|
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 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
|
530 |
|
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
|
531 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
|
532 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
|
533 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
|
534 |
|
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
|
535 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
|
536 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
|
537 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
|
538 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
|
539 |
|
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
|
540 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
|
541 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
|
542 |
|
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
|
543 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
|
544 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
|
545 |
|
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
|
546 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
|
547 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
|
548 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
|
549 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
|
550 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
|
551 |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
552 # --------------------------------------------------------------------------- |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
553 # used for debugging. |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
554 # --------------------------------------------------------------------------- |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
555 |
|
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
|
556 def compact_traceback(): |
|
25249
1dd7e51fe0be
Fiddle with compact_traceback().
Guido van Rossum <guido@python.org>
parents:
25226
diff
changeset
|
557 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
|
558 tbinfo = [] |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
559 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
|
560 raise AssertionError("traceback does not exist") |
|
25249
1dd7e51fe0be
Fiddle with compact_traceback().
Guido van Rossum <guido@python.org>
parents:
25226
diff
changeset
|
561 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
|
562 tbinfo.append(( |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
563 tb.tb_frame.f_code.co_filename, |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14509
diff
changeset
|
564 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
|
565 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
|
566 )) |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
567 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
|
568 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
569 # 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
|
570 del tb |
| 10934 | 571 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
572 file, function, line = tbinfo[-1] |
|
25249
1dd7e51fe0be
Fiddle with compact_traceback().
Guido van Rossum <guido@python.org>
parents:
25226
diff
changeset
|
573 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
|
574 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
|
575 |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
576 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
|
577 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
|
578 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
|
579 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
|
580 try: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
581 x.close() |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
582 except OSError, x: |
|
48035
644fa7a240c8
Remove exception indexing in asyncore.
Georg Brandl <georg@python.org>
parents:
47934
diff
changeset
|
583 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
|
584 pass |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
585 elif not ignore_all: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
586 raise |
|
53167
c54b784a56d5
Merged revisions 70873,70904,70934,71490,71553,71579 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
49766
diff
changeset
|
587 except _reraised_exceptions: |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
588 raise |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
589 except: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
590 if not ignore_all: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
591 raise |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
592 map.clear() |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
593 |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
594 # 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
|
595 # |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
596 # 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
|
597 # digging through the linux kernel), I've determined that select() |
| 30723 | 598 # 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
|
599 # 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
|
600 # 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
|
601 # 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
|
602 # |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
603 # 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
|
604 # |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
605 # 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
|
606 |
|
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
607 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
|
608 import fcntl |
|
9855
30c1c40cd80f
A gift from Sam Rushing - modules asyncore and asynchat for the
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
609 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
610 class file_wrapper: |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
611 # 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
|
612 # 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
|
613 # 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
|
614 |
|
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 __init__(self, fd): |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
616 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
|
617 |
|
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
|
618 def recv(self, *args): |
|
22824
f68ddf7f21f8
Replace use of apply() with extended call syntax.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22285
diff
changeset
|
619 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
|
620 |
|
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
|
621 def send(self, *args): |
|
22824
f68ddf7f21f8
Replace use of apply() with extended call syntax.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22285
diff
changeset
|
622 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
|
623 |
|
63505
2a4b2fec8277
Merged revisions 83461 via svnmerge from
Georg Brandl <georg@python.org>
parents:
61696
diff
changeset
|
624 def getsockopt(self, level, optname, buflen=None): |
|
2a4b2fec8277
Merged revisions 83461 via svnmerge from
Georg Brandl <georg@python.org>
parents:
61696
diff
changeset
|
625 if (level == socket.SOL_SOCKET and |
|
2a4b2fec8277
Merged revisions 83461 via svnmerge from
Georg Brandl <georg@python.org>
parents:
61696
diff
changeset
|
626 optname == socket.SO_ERROR and |
|
2a4b2fec8277
Merged revisions 83461 via svnmerge from
Georg Brandl <georg@python.org>
parents:
61696
diff
changeset
|
627 not buflen): |
|
2a4b2fec8277
Merged revisions 83461 via svnmerge from
Georg Brandl <georg@python.org>
parents:
61696
diff
changeset
|
628 return 0 |
|
2a4b2fec8277
Merged revisions 83461 via svnmerge from
Georg Brandl <georg@python.org>
parents:
61696
diff
changeset
|
629 raise NotImplementedError("Only asyncore specific behaviour " |
|
2a4b2fec8277
Merged revisions 83461 via svnmerge from
Georg Brandl <georg@python.org>
parents:
61696
diff
changeset
|
630 "implemented.") |
|
2a4b2fec8277
Merged revisions 83461 via svnmerge from
Georg Brandl <georg@python.org>
parents:
61696
diff
changeset
|
631 |
|
14509
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
632 read = recv |
|
a0392f438960
Untabify. When compiling in -tt mode, an inconsistent Tab use error
Fred Drake <fdrake@acm.org>
parents:
14473
diff
changeset
|
633 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
|
634 |
|
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
|
635 def close(self): |
|
32581
0dd473a8e53c
Return value from .close(); move .set_file up
Andrew M. Kuchling <amk@amk.ca>
parents:
32528
diff
changeset
|
636 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
|
637 |
|
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
|
638 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
|
639 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
|
640 |
|
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
|
641 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
|
642 |
|
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
|
643 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
|
644 dispatcher.__init__(self, None, map) |
|
31747
14f8c933f88c
[Part of patch #909005] Use True/False
Andrew M. Kuchling <amk@amk.ca>
parents:
31744
diff
changeset
|
645 self.connected = True |
|
47539
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
646 try: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
647 fd = fd.fileno() |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
648 except AttributeError: |
|
53c60c7cccd9
Applying updated patch from Issue 1736190, which addresses partial
Josiah Carlson <josiah.carlson@gmail.com>
parents:
35505
diff
changeset
|
649 pass |
|
32581
0dd473a8e53c
Return value from .close(); move .set_file up
Andrew M. Kuchling <amk@amk.ca>
parents:
32528
diff
changeset
|
650 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
|
651 # 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
|
652 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
|
653 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
|
654 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
|
655 |
|
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
|
656 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
|
657 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
|
658 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
|
659 self.add_channel() |
