Mercurial > cpython-lru_cache-weakref
annotate Modules/selectmodule.c @ 13778:f6e4a9dd67cd legacy-trunk
Rename the "parser" package to "parsers" to be compatible with the PyXML
project.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Mon, 31 Jul 2000 23:59:54 +0000 |
| parents | 59f8cedcf049 |
| children | 35b7989ba20c |
| 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 /*********************************************************** |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13079
diff
changeset
|
2 Copyright (c) 2000, BeOpen.com. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13079
diff
changeset
|
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13079
diff
changeset
|
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13079
diff
changeset
|
5 All rights reserved. |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13079
diff
changeset
|
7 See the file "Misc/COPYRIGHT" for information on usage and |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
13079
diff
changeset
|
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
|
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 ******************************************************************/ |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
11 /* select - Module containing unix select(2) call. |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
12 Under Unix, the file descriptors are small integers. |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
13 Under Win32, select only exists for sockets, and sockets may |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
14 have any value except INVALID_SOCKET. |
|
8823
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8616
diff
changeset
|
15 Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything |
|
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8616
diff
changeset
|
16 >= 0. |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
17 */ |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
19 #include "Python.h" |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 |
| 4600 | 21 #ifdef HAVE_UNISTD_H |
| 22 #include <unistd.h> | |
| 23 #endif | |
|
13550
95b9ddb00352
One more include of limits.h (possibly to be moved elsewhere when there is
Jack Jansen <jack.jansen@cwi.nl>
parents:
13502
diff
changeset
|
24 #ifdef HAVE_LIMITS_H |
|
95b9ddb00352
One more include of limits.h (possibly to be moved elsewhere when there is
Jack Jansen <jack.jansen@cwi.nl>
parents:
13502
diff
changeset
|
25 #include <limits.h> |
|
95b9ddb00352
One more include of limits.h (possibly to be moved elsewhere when there is
Jack Jansen <jack.jansen@cwi.nl>
parents:
13502
diff
changeset
|
26 #endif |
| 4600 | 27 |
|
4621
a2f171ce25f8
Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4600
diff
changeset
|
28 #ifdef __sgi |
|
a2f171ce25f8
Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4600
diff
changeset
|
29 /* This is missing from unistd.h */ |
|
13667
a6c7bfdd6777
Even more ANSIfication: fix as many function pointers and declarations as
Thomas Wouters <thomas@python.org>
parents:
13656
diff
changeset
|
30 extern void bzero(void *, int); |
|
4621
a2f171ce25f8
Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4600
diff
changeset
|
31 #endif |
|
a2f171ce25f8
Add ``extern void bzero();'' for SGI, to keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents:
4600
diff
changeset
|
32 |
|
11243
6aac31cd2102
Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
33 #ifndef DONT_HAVE_SYS_TYPES_H |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1392
diff
changeset
|
34 #include <sys/types.h> |
|
11243
6aac31cd2102
Patch by Mark Hammond to avoid certain header files on Windows/CE.
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
35 #endif |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
36 |
|
6626
5e31d381fa47
os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents:
6367
diff
changeset
|
37 #if defined(PYOS_OS2) |
|
5e31d381fa47
os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents:
6367
diff
changeset
|
38 #include <sys/time.h> |
|
5e31d381fa47
os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents:
6367
diff
changeset
|
39 #include <utils.h> |
|
5e31d381fa47
os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents:
6367
diff
changeset
|
40 #endif |
|
5e31d381fa47
os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents:
6367
diff
changeset
|
41 |
|
3738
ce74bba8a6a6
Slightly different Windows ifdefs
Guido van Rossum <guido@python.org>
parents:
3675
diff
changeset
|
42 #ifdef MS_WINDOWS |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
43 #include <winsock.h> |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
44 #else |
|
8823
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8616
diff
changeset
|
45 #ifdef __BEOS__ |
|
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8616
diff
changeset
|
46 #include <net/socket.h> |
|
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8616
diff
changeset
|
47 #define SOCKET int |
|
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8616
diff
changeset
|
48 #else |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
49 #define SOCKET int |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
50 #endif |
|
8823
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8616
diff
changeset
|
51 #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
|
52 |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
53 static PyObject *SelectError; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
55 /* list of Python objects and their file descriptor */ |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
56 typedef struct { |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
57 PyObject *obj; /* owned reference */ |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
58 SOCKET fd; |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
59 int sentinel; /* -1 == sentinel */ |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
60 } pylist; |
|
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
61 |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
62 static void |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
63 reap_obj(pylist fd2obj[FD_SETSIZE + 3]) |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
64 { |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
65 int i; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
66 for (i = 0; i < FD_SETSIZE + 3 && fd2obj[i].sentinel >= 0; i++) { |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
67 Py_XDECREF(fd2obj[i].obj); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
68 fd2obj[i].obj = NULL; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
69 } |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
70 fd2obj[0].sentinel = -1; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
71 } |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
72 |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
73 |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
74 /* returns -1 and sets the Python exception if an error occurred, otherwise |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
75 returns a number >= 0 |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
76 */ |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
77 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
78 list2set(PyObject *list, fd_set *set, 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
|
79 { |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
80 int i; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
81 int max = -1; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
82 int index = 0; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
83 int len = PyList_Size(list); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
84 PyObject* o = NULL; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
85 |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
86 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */ |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
87 FD_ZERO(set); |
| 2611 | 88 |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
89 for (i = 0; i < len; i++) { |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
90 SOCKET v; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
91 |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
92 /* any intervening fileno() calls could decr this refcnt */ |
|
4730
62cf7d589300
list2set(): PyList_GetItem could fail.
Barry Warsaw <barry@python.org>
parents:
4700
diff
changeset
|
93 if (!(o = PyList_GetItem(list, i))) |
|
4734
3db423515626
list2set(): correct return value (an int, not a PyObject*).
Barry Warsaw <barry@python.org>
parents:
4730
diff
changeset
|
94 return -1; |
|
4730
62cf7d589300
list2set(): PyList_GetItem could fail.
Barry Warsaw <barry@python.org>
parents:
4700
diff
changeset
|
95 |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
96 Py_INCREF(o); |
|
13593
df64cc0e84e2
Use PyObject_AsFileDescriptor
Andrew M. Kuchling <amk@amk.ca>
parents:
13550
diff
changeset
|
97 v = PyObject_AsFileDescriptor( o ); |
|
df64cc0e84e2
Use PyObject_AsFileDescriptor
Andrew M. Kuchling <amk@amk.ca>
parents:
13550
diff
changeset
|
98 if (v == -1) goto finally; |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
99 |
|
11656
db756d31e727
According to Ron Bickers, and with apparent approval of Chris
Guido van Rossum <guido@python.org>
parents:
11243
diff
changeset
|
100 #if defined(_MSC_VER) |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
101 max = 0; /* not used for Win32 */ |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
102 #else /* !_MSC_VER */ |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
103 if (v < 0 || v >= FD_SETSIZE) { |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
104 PyErr_SetString(PyExc_ValueError, |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
105 "filedescriptor out of range in select()"); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
106 goto finally; |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
107 } |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
108 if (v > max) |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
109 max = v; |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
110 #endif /* _MSC_VER */ |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
111 FD_SET(v, set); |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
112 |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
113 /* add object and its file descriptor to the list */ |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
114 if (index >= FD_SETSIZE) { |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
115 PyErr_SetString(PyExc_ValueError, |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
116 "too many file descriptors in select()"); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
117 goto finally; |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
118 } |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
119 fd2obj[index].obj = o; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
120 fd2obj[index].fd = v; |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
121 fd2obj[index].sentinel = 0; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
122 fd2obj[++index].sentinel = -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
|
123 } |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
124 return max+1; |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
125 |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
126 finally: |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
127 Py_XDECREF(o); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
128 return -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
|
129 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
131 /* returns NULL and sets the Python exception if an error occurred */ |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
132 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
133 set2list(fd_set *set, 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
|
134 { |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
135 int i, j, count=0; |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
136 PyObject *list, *o; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
137 SOCKET fd; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
138 |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
139 for (j = 0; fd2obj[j].sentinel >= 0; j++) { |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
140 if (FD_ISSET(fd2obj[j].fd, set)) |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
141 count++; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
142 } |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
143 list = PyList_New(count); |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
144 if (!list) |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
145 return NULL; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
147 i = 0; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
148 for (j = 0; fd2obj[j].sentinel >= 0; j++) { |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
149 fd = fd2obj[j].fd; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
150 if (FD_ISSET(fd, set)) { |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
151 #ifndef _MSC_VER |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
152 if (fd > FD_SETSIZE) { |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
153 PyErr_SetString(PyExc_SystemError, |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
154 "filedescriptor out of range returned in select()"); |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
155 goto finally; |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
156 } |
|
3675
d617de15774b
Yet another pass at fd2obj -- this time for Windows
Guido van Rossum <guido@python.org>
parents:
2611
diff
changeset
|
157 #endif |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
158 o = fd2obj[j].obj; |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
159 fd2obj[j].obj = NULL; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
160 /* transfer ownership */ |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
161 if (PyList_SetItem(list, i, o) < 0) |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
162 goto finally; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
163 |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
164 i++; |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
165 } |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
166 } |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
167 return list; |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
168 finally: |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
169 Py_DECREF(list); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
170 return NULL; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 } |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
172 |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
174 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
175 select_select(PyObject *self, PyObject *args) |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 { |
|
8616
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
177 #ifdef MS_WINDOWS |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
178 /* This would be an awful lot of stack space on Windows! */ |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
179 pylist *rfd2obj, *wfd2obj, *efd2obj; |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
180 #else |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
181 pylist rfd2obj[FD_SETSIZE + 3]; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
182 pylist wfd2obj[FD_SETSIZE + 3]; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
183 pylist efd2obj[FD_SETSIZE + 3]; |
|
8616
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
184 #endif |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
185 PyObject *ifdlist, *ofdlist, *efdlist; |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
186 PyObject *ret = NULL; |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
187 PyObject *tout = Py_None; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
188 fd_set ifdset, ofdset, efdset; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
189 double timeout; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
190 struct timeval tv, *tvp; |
| 13079 | 191 long seconds; |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
192 int imax, omax, emax, max; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
193 int n; |
|
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 |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
195 /* convert arguments */ |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11656
diff
changeset
|
196 if (!PyArg_ParseTuple(args, "OOO|O:select", |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
197 &ifdlist, &ofdlist, &efdlist, &tout)) |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
198 return NULL; |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
200 if (tout == Py_None) |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
201 tvp = (struct timeval *)0; |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
202 else if (!PyArg_Parse(tout, "d", &timeout)) { |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
203 PyErr_SetString(PyExc_TypeError, |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
204 "timeout must be a float or None"); |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
205 return NULL; |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
206 } |
|
1390
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
207 else { |
| 13079 | 208 if (timeout > (double)LONG_MAX) { |
| 209 PyErr_SetString(PyExc_OverflowError, "timeout period too long"); | |
| 210 return NULL; | |
| 211 } | |
| 212 seconds = (long)timeout; | |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
213 timeout = timeout - (double)seconds; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
214 tv.tv_sec = seconds; |
| 13079 | 215 tv.tv_usec = (long)(timeout*1000000.0); |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
216 tvp = &tv; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
217 } |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
218 |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
219 /* sanity check first three arguments */ |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
220 if (!PyList_Check(ifdlist) || |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
221 !PyList_Check(ofdlist) || |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
222 !PyList_Check(efdlist)) |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
223 { |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
224 PyErr_SetString(PyExc_TypeError, |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
225 "arguments 1-3 must be lists"); |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
226 return NULL; |
|
1390
cffe8d6b2f09
* selectmodule.c (select_select): timeout argument may be None with same
Guido van Rossum <guido@python.org>
parents:
1284
diff
changeset
|
227 } |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 |
|
8616
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
229 #ifdef MS_WINDOWS |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
230 /* Allocate memory for the lists */ |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
231 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
232 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
233 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
234 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
235 if (rfd2obj) PyMem_DEL(rfd2obj); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
236 if (wfd2obj) PyMem_DEL(wfd2obj); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
237 if (efd2obj) PyMem_DEL(efd2obj); |
|
8616
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
238 return NULL; |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
239 } |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
240 #endif |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
241 /* Convert lists to fd_sets, and get maximum fd number |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
242 * propagates the Python exception set in list2set() |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
243 */ |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
244 rfd2obj[0].sentinel = -1; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
245 wfd2obj[0].sentinel = -1; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
246 efd2obj[0].sentinel = -1; |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
247 if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0) |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
248 goto finally; |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
249 if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0) |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
250 goto finally; |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
251 if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0) |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
252 goto finally; |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
253 max = imax; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
254 if (omax > max) max = omax; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
255 if (emax > max) max = emax; |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
256 |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
257 Py_BEGIN_ALLOW_THREADS |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
258 n = select(max, &ifdset, &ofdset, &efdset, tvp); |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
259 Py_END_ALLOW_THREADS |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
260 |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
261 if (n < 0) { |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
262 PyErr_SetFromErrno(SelectError); |
|
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
263 } |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
264 else if (n == 0) { |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
265 /* optimization */ |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
266 ifdlist = PyList_New(0); |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
267 if (ifdlist) { |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
268 ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
269 Py_DECREF(ifdlist); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
270 } |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
271 } |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
272 else { |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
273 /* any of these three calls can raise an exception. it's more |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
274 convenient to test for this after all three calls... but |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
275 is that acceptable? |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
276 */ |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
277 ifdlist = set2list(&ifdset, rfd2obj); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
278 ofdlist = set2list(&ofdset, wfd2obj); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
279 efdlist = set2list(&efdset, efd2obj); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
280 if (PyErr_Occurred()) |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
281 ret = NULL; |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
282 else |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
283 ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist); |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 |
|
4700
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
285 Py_DECREF(ifdlist); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
286 Py_DECREF(ofdlist); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
287 Py_DECREF(efdlist); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
288 } |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
289 |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
290 finally: |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
291 reap_obj(rfd2obj); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
292 reap_obj(wfd2obj); |
|
825038d98ab6
Reworked to eliminate all potential memory problems, including
Barry Warsaw <barry@python.org>
parents:
4692
diff
changeset
|
293 reap_obj(efd2obj); |
|
8616
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
294 #ifdef MS_WINDOWS |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
295 PyMem_DEL(rfd2obj); |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
296 PyMem_DEL(wfd2obj); |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
297 PyMem_DEL(efd2obj); |
|
2417f17fa3e5
On Windows, put the select file descriptor arrays on the heap.
Guido van Rossum <guido@python.org>
parents:
8570
diff
changeset
|
298 #endif |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
299 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
|
300 } |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 |
| 8570 | 302 static char select_doc[] = |
| 303 "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\ | |
| 304 \n\ | |
| 305 Wait until one or more file descriptors are ready for some kind of I/O.\n\ | |
| 306 The first three arguments are lists of file descriptors to be waited for:\n\ | |
| 307 rlist -- wait until ready for reading\n\ | |
| 308 wlist -- wait until ready for writing\n\ | |
| 309 xlist -- wait for an ``exceptional condition''\n\ | |
| 310 If only one kind of condition is required, pass [] for the other lists.\n\ | |
| 311 A file descriptor is either a socket or file object, or a small integer\n\ | |
| 312 gotten from a fileno() method call on one of those.\n\ | |
| 313 \n\ | |
| 314 The optional 4th argument specifies a timeout in seconds; it may be\n\ | |
| 315 a floating point number to specify fractions of seconds. If it is absent\n\ | |
| 316 or None, the call will never time out.\n\ | |
| 317 \n\ | |
| 318 The return value is a tuple of three lists corresponding to the first three\n\ | |
| 319 arguments; each contains the subset of the corresponding file descriptors\n\ | |
| 320 that are ready.\n\ | |
| 321 \n\ | |
| 322 *** IMPORTANT NOTICE ***\n\ | |
| 323 On Windows, only sockets are supported; on Unix, all file descriptors."; | |
| 324 | |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
326 static PyMethodDef select_methods[] = { |
| 8570 | 327 {"select", select_select, 1, select_doc}, |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
328 {0, 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
|
329 }; |
|
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 |
| 8570 | 331 static char module_doc[] = |
| 332 "This module supports asynchronous I/O on multiple file descriptors.\n\ | |
| 333 \n\ | |
| 334 *** IMPORTANT NOTICE ***\n\ | |
| 335 On Windows, only sockets are supported; on Unix, all file descriptors."; | |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 |
|
9588
38023a425e48
Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents:
8823
diff
changeset
|
337 DL_EXPORT(void) |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13593
diff
changeset
|
338 initselect(void) |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 { |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
340 PyObject *m, *d; |
| 8570 | 341 m = Py_InitModule3("select", select_methods, module_doc); |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
342 d = PyModule_GetDict(m); |
|
6367
2c29b8982643
Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents:
4734
diff
changeset
|
343 SelectError = PyErr_NewException("select.error", NULL, NULL); |
|
4692
cb405b9696d4
Several changes... Guido *please* take a look!
Barry Warsaw <barry@python.org>
parents:
4621
diff
changeset
|
344 PyDict_SetItemString(d, "error", SelectError); |
|
902
a4793ae26682
Changes for new UNIX-specific built-in module 'select' and new header for
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 } |
