Mercurial > cpython-lru_cache-weakref
annotate Modules/selectmodule.c @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 65d2d8e67da3 |
| children | d36250aefb82 |
| rev | line source |
|---|---|
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 /*********************************************************** |
|
2061
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
3 The Netherlands. |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 All Rights Reserved |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
15 permission. |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3738
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 ******************************************************************/ |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
32 /* select - Module containing unix select(2) call. |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
33 Under Unix, the file descriptors are small integers. |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
34 Under Win32, select only exists for sockets, and sockets may |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
35 have any value except INVALID_SOCKET. |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
36 */ |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 #include "allobjects.h" |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 #include "modsupport.h" |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 #include "ceval.h" |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1392
diff
changeset
|
42 #include <sys/types.h> |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
43 |
|
3738
ce74bba8a6a6
Slightly different Windows ifdefs
Guido van Rossum <guido@python.org>
parents:
3675
diff
changeset
|
44 #ifdef MS_WINDOWS |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
45 #include <winsock.h> |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
46 #else |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1392
diff
changeset
|
47 #include "myselect.h" /* Also includes mytime.h */ |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
48 #define SOCKET int |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
49 #endif |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 static object *SelectError; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
53 typedef struct { /* list of Python objects and their file descriptor */ |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
54 object *obj; |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
55 SOCKET fd; |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
56 } pylist; |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
57 |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
58 static int |
|
922
c12b445cdace
* myselect.h: bzero -> memset
Guido van Rossum <guido@python.org>
parents:
904
diff
changeset
|
59 list2set(list, set, fd2obj) |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 object *list; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 fd_set *set; |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
62 pylist fd2obj[FD_SETSIZE + 3]; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 { |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
64 int i, len, index, max = -1; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 object *o, *filenomethod, *fno; |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
66 SOCKET v; |
| 2611 | 67 |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
68 index = 0; |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
69 fd2obj[0].obj = (object*)0; /* set list to zero size */ |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 FD_ZERO(set); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 len = getlistsize(list); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 for( i=0; i<len; i++ ) { |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 o = getlistitem(list, i); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 if ( is_intobject(o) ) { |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 v = getintvalue(o); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 } else if ( (filenomethod = getattr(o, "fileno")) != NULL ) { |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 fno = call_object(filenomethod, NULL); |
|
1284
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
79 DECREF(filenomethod); |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 if ( fno == NULL ) |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 return -1; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 if ( !is_intobject(fno) ) { |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 err_badarg(); |
|
1284
dbce72afe261
* selectmodule.c: fix (another!) two memory leaks -- this time in list2set
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
84 DECREF(fno); |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 return -1; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 v = getintvalue(fno); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 DECREF(fno); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 } else { |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 err_badarg(); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 return -1; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 } |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
93 #ifdef _MSC_VER |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
94 max = 0; /* not used for Win32 */ |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
95 #else |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1392
diff
changeset
|
96 if ( v < 0 || v >= FD_SETSIZE ) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1392
diff
changeset
|
97 err_setstr(ValueError, "filedescriptor out of range in select()"); |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 return -1; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 if ( v > max ) max = v; |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
101 #endif |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 FD_SET(v, set); |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
103 /* add object and its file descriptor to the list */ |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
104 if ( index >= FD_SETSIZE ) { |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
105 err_setstr(ValueError, "too many file descriptors in select()"); |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
106 return -1; |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
107 } |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
108 fd2obj[index].obj = o; |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
109 fd2obj[index].fd = v; |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
110 fd2obj[++index].obj = (object *)0; /* sentinel */ |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 return max+1; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 static object * |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
116 set2list(set, fd2obj) |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 fd_set *set; |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
118 pylist fd2obj[FD_SETSIZE + 3]; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 { |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
120 int j, num=0; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 object *list, *o; |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
122 SOCKET fd; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
124 for(j=0; fd2obj[j].obj; j++) |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
125 if ( FD_ISSET(fd2obj[j].fd, set) ) |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 num++; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 list = newlistobject(num); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 num = 0; |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
129 for(j=0; fd2obj[j].obj; j++) { |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
130 fd = fd2obj[j].fd; |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
131 if ( FD_ISSET(fd, set) ) { |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
132 #ifndef _MSC_VER |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
133 if ( fd > FD_SETSIZE ) { |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1392
diff
changeset
|
134 err_setstr(SystemError, |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1392
diff
changeset
|
135 "filedescriptor out of range returned in select()"); |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 return NULL; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 } |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
138 #endif |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
139 o = fd2obj[j].obj; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 INCREF(o); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 setlistitem(list, num, o); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 num++; |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
143 } |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 return list; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 static object * |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 select_select(self, args) |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 object *self; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 object *args; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 { |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
153 pylist rfd2obj[FD_SETSIZE + 3], wfd2obj[FD_SETSIZE + 3], efd2obj[FD_SETSIZE + 3]; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 object *ifdlist, *ofdlist, *efdlist; |
|
1390
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
155 object *ret, *tout; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 fd_set ifdset, ofdset, efdset; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 double timeout; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 struct timeval tv, *tvp; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 int seconds; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 int imax, omax, emax, max; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 int n; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 /* Get args. Looks funny because of optional timeout argument */ |
|
1390
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
165 if ( getargs(args, "(OOOO)", &ifdlist, &ofdlist, &efdlist, &tout) ) { |
|
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
166 if (tout == None) |
|
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
167 tvp = (struct timeval *)0; |
|
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
168 else { |
|
1392
0ffff40d13ff
Fixed a couple of bugs: getargs doesn't use % escapes, and seconds
Sjoerd Mullender <sjoerd@acm.org>
parents:
1390
diff
changeset
|
169 if (!getargs(tout, "d;timeout must be float or None", &timeout)) |
|
1390
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
170 return NULL; |
|
1392
0ffff40d13ff
Fixed a couple of bugs: getargs doesn't use % escapes, and seconds
Sjoerd Mullender <sjoerd@acm.org>
parents:
1390
diff
changeset
|
171 seconds = (int)timeout; |
|
1390
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
172 timeout = timeout - (double)seconds; |
|
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
173 tv.tv_sec = seconds; |
|
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
174 tv.tv_usec = (int)(timeout*1000000.0); |
|
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
175 tvp = &tv; |
|
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
176 } |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 } else { |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 /* Doesn't have 4 args, that means no timeout */ |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 err_clear(); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 if (!getargs(args, "(OOO)", &ifdlist, &ofdlist, &efdlist) ) |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 return 0; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 tvp = (struct timeval *)0; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 if ( !is_listobject(ifdlist) || !is_listobject(ofdlist) || |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 !is_listobject(efdlist) ) { |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 err_badarg(); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 return 0; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 /* Convert lists to fd_sets, and get maximum fd number */ |
| 2611 | 191 if( (imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0 ) |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 return 0; |
| 2611 | 193 if( (omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0 ) |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 return 0; |
| 2611 | 195 if( (emax=list2set(efdlist, &efdset, efd2obj)) < 0 ) |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 return 0; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 max = imax; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 if ( omax > max ) max = omax; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 if ( emax > max ) max = emax; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 |
| 926 | 201 BGN_SAVE |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 n = select(max, &ifdset, &ofdset, &efdset, tvp); |
| 926 | 203 END_SAVE |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 if ( n < 0 ) { |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 err_errno(SelectError); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 return 0; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
210 if ( n == 0 ) { /* Speedup hack */ |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
211 ifdlist = newlistobject(0); |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
212 ret = mkvalue("OOO", ifdlist, ifdlist, ifdlist); |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
213 XDECREF(ifdlist); |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
214 return ret; |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
215 } |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
217 ifdlist = set2list(&ifdset, rfd2obj); |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
218 ofdlist = set2list(&ofdset, wfd2obj); |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
219 efdlist = set2list(&efdset, efd2obj); |
|
1207
b564bd4e57e1
* ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents:
927
diff
changeset
|
220 ret = mkvalue("OOO", ifdlist, ofdlist, efdlist); |
|
b564bd4e57e1
* ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents:
927
diff
changeset
|
221 XDECREF(ifdlist); |
|
b564bd4e57e1
* ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents:
927
diff
changeset
|
222 XDECREF(ofdlist); |
|
b564bd4e57e1
* ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents:
927
diff
changeset
|
223 XDECREF(efdlist); |
|
b564bd4e57e1
* ceval.c: ifdef out the last argument passing compat hack.
Guido van Rossum <guido@python.org>
parents:
927
diff
changeset
|
224 return ret; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 static struct methodlist select_methods[] = { |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 { "select", select_select }, |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 { 0, 0 }, |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 }; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 void |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 initselect() |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 { |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 object *m, *d; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 m = initmodule("select", select_methods); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 d = getmoduledict(m); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 SelectError = newstringobject("select.error"); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 if ( SelectError == NULL || dictinsert(d, "error", SelectError) ) |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 fatal("Cannot define select.error"); |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 } |
