Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 1 | /* select - Module containing unix select(2) call. |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 2 | Under Unix, the file descriptors are small integers. |
| 3 | Under Win32, select only exists for sockets, and sockets may |
| 4 | have any value except INVALID_SOCKET. |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 5 | Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything |
| 6 | >= 0. |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 7 | */ |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 8 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 9 | #include "Python.h" |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 10 | |
Tim Peters | d92dfe0 | 2000-12-12 01:18:41 +0000 | [diff] [blame] | 11 | /* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined. |
| 12 | 64 is too small (too many people have bumped into that limit). |
| 13 | Here we boost it. |
| 14 | Users who want even more than the boosted limit should #define |
| 15 | FD_SETSIZE higher before this; e.g., via compiler /D switch. |
| 16 | */ |
| 17 | #if defined(MS_WINDOWS) && !defined(FD_SETSIZE) |
| 18 | #define FD_SETSIZE 512 |
| 19 | #endif |
| 20 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 21 | #ifdef HAVE_UNISTD_H |
| 22 | #include <unistd.h> |
| 23 | #endif |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 24 | #ifdef HAVE_POLL_H |
| 25 | #include <poll.h> |
| 26 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 3727317 | 1996-12-09 18:47:43 +0000 | [diff] [blame] | 28 | #ifdef __sgi |
| 29 | /* This is missing from unistd.h */ |
Thomas Wouters | bd4bc4e | 2000-07-22 23:57:55 +0000 | [diff] [blame] | 30 | extern void bzero(void *, int); |
Guido van Rossum | 3727317 | 1996-12-09 18:47:43 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 33 | #ifndef DONT_HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 34 | #include <sys/types.h> |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 35 | #endif |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 37 | #if defined(PYOS_OS2) |
| 38 | #include <sys/time.h> |
| 39 | #include <utils.h> |
| 40 | #endif |
| 41 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 42 | #ifdef MS_WINDOWS |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 43 | #include <winsock.h> |
| 44 | #else |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 45 | #ifdef __BEOS__ |
| 46 | #include <net/socket.h> |
| 47 | #define SOCKET int |
| 48 | #else |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 49 | #define SOCKET int |
| 50 | #endif |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 51 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 52 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 53 | static PyObject *SelectError; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 54 | |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 55 | /* list of Python objects and their file descriptor */ |
| 56 | typedef struct { |
| 57 | PyObject *obj; /* owned reference */ |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 58 | SOCKET fd; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 59 | int sentinel; /* -1 == sentinel */ |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 60 | } pylist; |
| 61 | |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 62 | static void |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 63 | reap_obj(pylist fd2obj[FD_SETSIZE + 3]) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 64 | { |
| 65 | int i; |
| 66 | for (i = 0; i < FD_SETSIZE + 3 && fd2obj[i].sentinel >= 0; i++) { |
| 67 | Py_XDECREF(fd2obj[i].obj); |
| 68 | fd2obj[i].obj = NULL; |
| 69 | } |
| 70 | fd2obj[0].sentinel = -1; |
| 71 | } |
| 72 | |
| 73 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 74 | /* returns -1 and sets the Python exception if an error occurred, otherwise |
| 75 | returns a number >= 0 |
| 76 | */ |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 77 | static int |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 78 | list2set(PyObject *list, fd_set *set, pylist fd2obj[FD_SETSIZE + 3]) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 79 | { |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 80 | int i; |
| 81 | int max = -1; |
| 82 | int index = 0; |
| 83 | int len = PyList_Size(list); |
| 84 | PyObject* o = NULL; |
Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 85 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 86 | fd2obj[0].obj = (PyObject*)0; /* set list to zero size */ |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 87 | FD_ZERO(set); |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 88 | |
| 89 | for (i = 0; i < len; i++) { |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 90 | SOCKET v; |
| 91 | |
| 92 | /* any intervening fileno() calls could decr this refcnt */ |
Barry Warsaw | 24c4b3d | 1996-12-13 23:22:42 +0000 | [diff] [blame] | 93 | if (!(o = PyList_GetItem(list, i))) |
Barry Warsaw | 529fcfe | 1996-12-16 18:15:34 +0000 | [diff] [blame] | 94 | return -1; |
Barry Warsaw | 24c4b3d | 1996-12-13 23:22:42 +0000 | [diff] [blame] | 95 | |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 96 | Py_INCREF(o); |
Andrew M. Kuchling | 9f28a03 | 2000-07-13 23:59:35 +0000 | [diff] [blame] | 97 | v = PyObject_AsFileDescriptor( o ); |
| 98 | if (v == -1) goto finally; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 947a0fa | 2000-01-14 16:33:09 +0000 | [diff] [blame] | 100 | #if defined(_MSC_VER) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 101 | max = 0; /* not used for Win32 */ |
| 102 | #else /* !_MSC_VER */ |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 103 | if (v < 0 || v >= FD_SETSIZE) { |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 104 | PyErr_SetString(PyExc_ValueError, |
| 105 | "filedescriptor out of range in select()"); |
| 106 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 107 | } |
| 108 | if (v > max) |
| 109 | max = v; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 110 | #endif /* _MSC_VER */ |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 111 | FD_SET(v, set); |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 112 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 113 | /* add object and its file descriptor to the list */ |
| 114 | if (index >= FD_SETSIZE) { |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 115 | PyErr_SetString(PyExc_ValueError, |
| 116 | "too many file descriptors in select()"); |
| 117 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 118 | } |
| 119 | fd2obj[index].obj = o; |
| 120 | fd2obj[index].fd = v; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 121 | fd2obj[index].sentinel = 0; |
| 122 | fd2obj[++index].sentinel = -1; |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 123 | } |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 124 | return max+1; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 125 | |
| 126 | finally: |
| 127 | Py_XDECREF(o); |
| 128 | return -1; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 131 | /* returns NULL and sets the Python exception if an error occurred */ |
| 132 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 133 | set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 3]) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 134 | { |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 135 | int i, j, count=0; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 136 | PyObject *list, *o; |
| 137 | SOCKET fd; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 138 | |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 139 | for (j = 0; fd2obj[j].sentinel >= 0; j++) { |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 140 | if (FD_ISSET(fd2obj[j].fd, set)) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 141 | count++; |
| 142 | } |
| 143 | list = PyList_New(count); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 144 | if (!list) |
| 145 | return NULL; |
| 146 | |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 147 | i = 0; |
| 148 | for (j = 0; fd2obj[j].sentinel >= 0; j++) { |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 149 | fd = fd2obj[j].fd; |
| 150 | if (FD_ISSET(fd, set)) { |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 151 | #ifndef _MSC_VER |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 152 | if (fd > FD_SETSIZE) { |
| 153 | PyErr_SetString(PyExc_SystemError, |
| 154 | "filedescriptor out of range returned in select()"); |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 155 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 156 | } |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 157 | #endif |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 158 | o = fd2obj[j].obj; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 159 | fd2obj[j].obj = NULL; |
| 160 | /* transfer ownership */ |
| 161 | if (PyList_SetItem(list, i, o) < 0) |
| 162 | goto finally; |
| 163 | |
| 164 | i++; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | return list; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 168 | finally: |
| 169 | Py_DECREF(list); |
| 170 | return NULL; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 171 | } |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 173 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 174 | static PyObject * |
Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 175 | select_select(PyObject *self, PyObject *args) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 176 | { |
Guido van Rossum | d20781b | 1998-07-02 02:53:36 +0000 | [diff] [blame] | 177 | #ifdef MS_WINDOWS |
| 178 | /* This would be an awful lot of stack space on Windows! */ |
| 179 | pylist *rfd2obj, *wfd2obj, *efd2obj; |
| 180 | #else |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 181 | pylist rfd2obj[FD_SETSIZE + 3]; |
| 182 | pylist wfd2obj[FD_SETSIZE + 3]; |
| 183 | pylist efd2obj[FD_SETSIZE + 3]; |
Guido van Rossum | d20781b | 1998-07-02 02:53:36 +0000 | [diff] [blame] | 184 | #endif |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 185 | PyObject *ifdlist, *ofdlist, *efdlist; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 186 | PyObject *ret = NULL; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 187 | PyObject *tout = Py_None; |
| 188 | fd_set ifdset, ofdset, efdset; |
| 189 | double timeout; |
| 190 | struct timeval tv, *tvp; |
Guido van Rossum | 3262e16 | 2000-06-28 21:18:13 +0000 | [diff] [blame] | 191 | long seconds; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 192 | int imax, omax, emax, max; |
| 193 | int n; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 194 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 195 | /* convert arguments */ |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 196 | if (!PyArg_ParseTuple(args, "OOO|O:select", |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 197 | &ifdlist, &ofdlist, &efdlist, &tout)) |
| 198 | return NULL; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 199 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 200 | if (tout == Py_None) |
| 201 | tvp = (struct timeval *)0; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 202 | else if (!PyArg_Parse(tout, "d", &timeout)) { |
| 203 | PyErr_SetString(PyExc_TypeError, |
| 204 | "timeout must be a float or None"); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 205 | return NULL; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 206 | } |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 207 | else { |
Guido van Rossum | 3262e16 | 2000-06-28 21:18:13 +0000 | [diff] [blame] | 208 | if (timeout > (double)LONG_MAX) { |
| 209 | PyErr_SetString(PyExc_OverflowError, "timeout period too long"); |
| 210 | return NULL; |
| 211 | } |
| 212 | seconds = (long)timeout; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 213 | timeout = timeout - (double)seconds; |
| 214 | tv.tv_sec = seconds; |
Guido van Rossum | 3262e16 | 2000-06-28 21:18:13 +0000 | [diff] [blame] | 215 | tv.tv_usec = (long)(timeout*1000000.0); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 216 | tvp = &tv; |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 217 | } |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 218 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 219 | /* sanity check first three arguments */ |
| 220 | if (!PyList_Check(ifdlist) || |
| 221 | !PyList_Check(ofdlist) || |
| 222 | !PyList_Check(efdlist)) |
| 223 | { |
| 224 | PyErr_SetString(PyExc_TypeError, |
| 225 | "arguments 1-3 must be lists"); |
| 226 | return NULL; |
| 227 | } |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 228 | |
Guido van Rossum | d20781b | 1998-07-02 02:53:36 +0000 | [diff] [blame] | 229 | #ifdef MS_WINDOWS |
| 230 | /* Allocate memory for the lists */ |
| 231 | rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); |
| 232 | wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); |
| 233 | efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); |
| 234 | if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 235 | if (rfd2obj) PyMem_DEL(rfd2obj); |
| 236 | if (wfd2obj) PyMem_DEL(wfd2obj); |
| 237 | if (efd2obj) PyMem_DEL(efd2obj); |
Guido van Rossum | d20781b | 1998-07-02 02:53:36 +0000 | [diff] [blame] | 238 | return NULL; |
| 239 | } |
| 240 | #endif |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 241 | /* Convert lists to fd_sets, and get maximum fd number |
| 242 | * propagates the Python exception set in list2set() |
| 243 | */ |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 244 | rfd2obj[0].sentinel = -1; |
| 245 | wfd2obj[0].sentinel = -1; |
| 246 | efd2obj[0].sentinel = -1; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 247 | if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 248 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 249 | if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 250 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 251 | if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 252 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 253 | max = imax; |
| 254 | if (omax > max) max = omax; |
| 255 | if (emax > max) max = emax; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 256 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 257 | Py_BEGIN_ALLOW_THREADS |
| 258 | n = select(max, &ifdset, &ofdset, &efdset, tvp); |
| 259 | Py_END_ALLOW_THREADS |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 260 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 261 | if (n < 0) { |
| 262 | PyErr_SetFromErrno(SelectError); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 263 | } |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 264 | else if (n == 0) { |
| 265 | /* optimization */ |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 266 | ifdlist = PyList_New(0); |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 267 | if (ifdlist) { |
| 268 | ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist); |
| 269 | Py_DECREF(ifdlist); |
| 270 | } |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 271 | } |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 272 | else { |
| 273 | /* any of these three calls can raise an exception. it's more |
| 274 | convenient to test for this after all three calls... but |
| 275 | is that acceptable? |
| 276 | */ |
| 277 | ifdlist = set2list(&ifdset, rfd2obj); |
| 278 | ofdlist = set2list(&ofdset, wfd2obj); |
| 279 | efdlist = set2list(&efdset, efd2obj); |
| 280 | if (PyErr_Occurred()) |
| 281 | ret = NULL; |
| 282 | else |
| 283 | ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 284 | |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 285 | Py_DECREF(ifdlist); |
| 286 | Py_DECREF(ofdlist); |
| 287 | Py_DECREF(efdlist); |
| 288 | } |
| 289 | |
| 290 | finally: |
| 291 | reap_obj(rfd2obj); |
| 292 | reap_obj(wfd2obj); |
| 293 | reap_obj(efd2obj); |
Guido van Rossum | d20781b | 1998-07-02 02:53:36 +0000 | [diff] [blame] | 294 | #ifdef MS_WINDOWS |
| 295 | PyMem_DEL(rfd2obj); |
| 296 | PyMem_DEL(wfd2obj); |
| 297 | PyMem_DEL(efd2obj); |
| 298 | #endif |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 299 | return ret; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 302 | #ifdef HAVE_POLL |
| 303 | /* |
| 304 | * poll() support |
| 305 | */ |
| 306 | |
| 307 | typedef struct { |
| 308 | PyObject_HEAD |
| 309 | PyObject *dict; |
| 310 | int ufd_uptodate; |
| 311 | int ufd_len; |
| 312 | struct pollfd *ufds; |
| 313 | } pollObject; |
| 314 | |
| 315 | staticforward PyTypeObject poll_Type; |
| 316 | |
| 317 | /* Update the malloc'ed array of pollfds to match the dictionary |
| 318 | contained within a pollObject. Return 1 on success, 0 on an error. |
| 319 | */ |
| 320 | |
| 321 | static int |
| 322 | update_ufd_array(pollObject *self) |
| 323 | { |
| 324 | int i, j, pos; |
| 325 | PyObject *key, *value; |
| 326 | |
| 327 | self->ufd_len = PyDict_Size(self->dict); |
| 328 | PyMem_Resize(self->ufds, struct pollfd, self->ufd_len); |
| 329 | if (self->ufds == NULL) { |
| 330 | PyErr_NoMemory(); |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | i = pos = 0; |
| 335 | while ((j = PyDict_Next(self->dict, &pos, &key, &value))) { |
| 336 | self->ufds[i].fd = PyInt_AsLong(key); |
| 337 | self->ufds[i].events = PyInt_AsLong(value); |
| 338 | i++; |
| 339 | } |
| 340 | self->ufd_uptodate = 1; |
| 341 | return 1; |
| 342 | } |
| 343 | |
| 344 | static char poll_register_doc[] = |
| 345 | "register(fd [, eventmask] ) -> None\n\n\ |
| 346 | Register a file descriptor with the polling object.\n\ |
| 347 | fd -- either an integer, or an object with a fileno() method returning an int.\n\ |
| 348 | events -- an optional bitmask describing the type of events to check for"; |
| 349 | |
| 350 | static PyObject * |
| 351 | poll_register(pollObject *self, PyObject *args) |
| 352 | { |
| 353 | PyObject *o, *key, *value; |
| 354 | int fd, events = POLLIN | POLLPRI | POLLOUT; |
| 355 | |
| 356 | if (!PyArg_ParseTuple(args, "O|i", &o, &events)) { |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | fd = PyObject_AsFileDescriptor(o); |
| 361 | if (fd == -1) return NULL; |
| 362 | |
| 363 | /* Add entry to the internal dictionary: the key is the |
| 364 | file descriptor, and the value is the event mask. */ |
| 365 | if ( (NULL == (key = PyInt_FromLong(fd))) || |
| 366 | (NULL == (value = PyInt_FromLong(events))) || |
| 367 | (PyDict_SetItem(self->dict, key, value)) == -1) { |
| 368 | return NULL; |
| 369 | } |
| 370 | self->ufd_uptodate = 0; |
| 371 | |
| 372 | Py_INCREF(Py_None); |
| 373 | return Py_None; |
| 374 | } |
| 375 | |
| 376 | static char poll_unregister_doc[] = |
| 377 | "unregister(fd) -> None\n\n\ |
| 378 | Remove a file descriptor being tracked by the polling object."; |
| 379 | |
| 380 | static PyObject * |
| 381 | poll_unregister(pollObject *self, PyObject *args) |
| 382 | { |
| 383 | PyObject *o, *key; |
| 384 | int fd; |
| 385 | |
| 386 | if (!PyArg_ParseTuple(args, "O", &o)) { |
| 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | fd = PyObject_AsFileDescriptor( o ); |
| 391 | if (fd == -1) |
| 392 | return NULL; |
| 393 | |
| 394 | /* Check whether the fd is already in the array */ |
| 395 | key = PyInt_FromLong(fd); |
| 396 | if (key == NULL) |
| 397 | return NULL; |
| 398 | |
| 399 | if (PyDict_DelItem(self->dict, key) == -1) { |
| 400 | Py_DECREF(key); |
| 401 | /* This will simply raise the KeyError set by PyDict_DelItem |
| 402 | if the file descriptor isn't registered. */ |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | Py_DECREF(key); |
| 407 | self->ufd_uptodate = 0; |
| 408 | |
| 409 | Py_INCREF(Py_None); |
| 410 | return Py_None; |
| 411 | } |
| 412 | |
| 413 | static char poll_poll_doc[] = |
| 414 | "poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\ |
| 415 | Polls the set of registered file descriptors, returning a list containing \n\ |
| 416 | any descriptors that have events or errors to report."; |
| 417 | |
| 418 | static PyObject * |
| 419 | poll_poll(pollObject *self, PyObject *args) |
| 420 | { |
| 421 | PyObject *result_list = NULL, *tout = NULL; |
| 422 | int timeout = 0, poll_result, i, j; |
| 423 | PyObject *value = NULL, *num = NULL; |
| 424 | |
| 425 | if (!PyArg_ParseTuple(args, "|O", &tout)) { |
| 426 | return NULL; |
| 427 | } |
| 428 | |
| 429 | /* Check values for timeout */ |
| 430 | if (tout == NULL || tout == Py_None) |
| 431 | timeout = -1; |
| 432 | else if (!PyArg_Parse(tout, "i", &timeout)) { |
| 433 | PyErr_SetString(PyExc_TypeError, |
| 434 | "timeout must be an integer or None"); |
| 435 | return NULL; |
| 436 | } |
| 437 | |
| 438 | /* Ensure the ufd array is up to date */ |
| 439 | if (!self->ufd_uptodate) |
| 440 | if (update_ufd_array(self) == 0) |
| 441 | return NULL; |
| 442 | |
| 443 | /* call poll() */ |
| 444 | Py_BEGIN_ALLOW_THREADS; |
| 445 | poll_result = poll(self->ufds, self->ufd_len, timeout); |
| 446 | Py_END_ALLOW_THREADS; |
| 447 | |
| 448 | if (poll_result < 0) { |
| 449 | PyErr_SetFromErrno(SelectError); |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | /* build the result list */ |
| 454 | |
| 455 | result_list = PyList_New(poll_result); |
| 456 | if (!result_list) |
| 457 | return NULL; |
| 458 | else { |
| 459 | for (i = 0, j = 0; j < poll_result; j++) { |
| 460 | /* skip to the next fired descriptor */ |
| 461 | while (!self->ufds[i].revents) { |
| 462 | i++; |
| 463 | } |
| 464 | /* if we hit a NULL return, set value to NULL |
| 465 | and break out of loop; code at end will |
| 466 | clean up result_list */ |
| 467 | value = PyTuple_New(2); |
| 468 | if (value == NULL) |
| 469 | goto error; |
| 470 | num = PyInt_FromLong(self->ufds[i].fd); |
| 471 | if (num == NULL) { |
| 472 | Py_DECREF(value); |
| 473 | goto error; |
| 474 | } |
| 475 | PyTuple_SET_ITEM(value, 0, num); |
| 476 | |
| 477 | num = PyInt_FromLong(self->ufds[i].revents); |
| 478 | if (num == NULL) { |
| 479 | Py_DECREF(value); |
| 480 | goto error; |
| 481 | } |
| 482 | PyTuple_SET_ITEM(value, 1, num); |
| 483 | if ((PyList_SetItem(result_list, j, value)) == -1) { |
| 484 | Py_DECREF(value); |
| 485 | goto error; |
| 486 | } |
| 487 | i++; |
| 488 | } |
| 489 | } |
| 490 | return result_list; |
| 491 | |
| 492 | error: |
| 493 | Py_DECREF(result_list); |
| 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | static PyMethodDef poll_methods[] = { |
| 498 | {"register", (PyCFunction)poll_register, |
| 499 | METH_VARARGS, poll_register_doc}, |
| 500 | {"unregister", (PyCFunction)poll_unregister, |
| 501 | METH_VARARGS, poll_unregister_doc}, |
| 502 | {"poll", (PyCFunction)poll_poll, |
| 503 | METH_VARARGS, poll_poll_doc}, |
| 504 | {NULL, NULL} /* sentinel */ |
| 505 | }; |
| 506 | |
| 507 | static pollObject * |
Fred Drake | 8ce159a | 2000-08-31 05:18:54 +0000 | [diff] [blame] | 508 | newPollObject(void) |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 509 | { |
| 510 | pollObject *self; |
| 511 | self = PyObject_New(pollObject, &poll_Type); |
| 512 | if (self == NULL) |
| 513 | return NULL; |
| 514 | /* ufd_uptodate is a Boolean, denoting whether the |
| 515 | array pointed to by ufds matches the contents of the dictionary. */ |
| 516 | self->ufd_uptodate = 0; |
| 517 | self->ufds = NULL; |
| 518 | self->dict = PyDict_New(); |
| 519 | if (self->dict == NULL) { |
| 520 | Py_DECREF(self); |
| 521 | return NULL; |
| 522 | } |
| 523 | return self; |
| 524 | } |
| 525 | |
| 526 | static void |
| 527 | poll_dealloc(pollObject *self) |
| 528 | { |
| 529 | if (self->ufds != NULL) |
| 530 | PyMem_DEL(self->ufds); |
| 531 | Py_XDECREF(self->dict); |
| 532 | PyObject_Del(self); |
| 533 | } |
| 534 | |
| 535 | static PyObject * |
| 536 | poll_getattr(pollObject *self, char *name) |
| 537 | { |
| 538 | return Py_FindMethod(poll_methods, (PyObject *)self, name); |
| 539 | } |
| 540 | |
| 541 | statichere PyTypeObject poll_Type = { |
| 542 | /* The ob_type field must be initialized in the module init function |
| 543 | * to be portable to Windows without using C++. */ |
| 544 | PyObject_HEAD_INIT(NULL) |
| 545 | 0, /*ob_size*/ |
| 546 | "poll", /*tp_name*/ |
| 547 | sizeof(pollObject), /*tp_basicsize*/ |
| 548 | 0, /*tp_itemsize*/ |
| 549 | /* methods */ |
| 550 | (destructor)poll_dealloc, /*tp_dealloc*/ |
| 551 | 0, /*tp_print*/ |
| 552 | (getattrfunc)poll_getattr, /*tp_getattr*/ |
| 553 | 0, /*tp_setattr*/ |
| 554 | 0, /*tp_compare*/ |
| 555 | 0, /*tp_repr*/ |
| 556 | 0, /*tp_as_number*/ |
| 557 | 0, /*tp_as_sequence*/ |
| 558 | 0, /*tp_as_mapping*/ |
| 559 | 0, /*tp_hash*/ |
| 560 | }; |
| 561 | |
| 562 | static char poll_doc[] = |
| 563 | "Returns a polling object, which supports registering and\n\ |
| 564 | unregistering file descriptors, and then polling them for I/O events."; |
| 565 | |
| 566 | static PyObject * |
| 567 | select_poll(PyObject *self, PyObject *args) |
| 568 | { |
| 569 | pollObject *rv; |
| 570 | |
| 571 | if (!PyArg_ParseTuple(args, ":poll")) |
| 572 | return NULL; |
| 573 | rv = newPollObject(); |
| 574 | if ( rv == NULL ) |
| 575 | return NULL; |
| 576 | return (PyObject *)rv; |
| 577 | } |
| 578 | #endif /* HAVE_POLL */ |
| 579 | |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 580 | static char select_doc[] = |
| 581 | "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\ |
| 582 | \n\ |
| 583 | Wait until one or more file descriptors are ready for some kind of I/O.\n\ |
| 584 | The first three arguments are lists of file descriptors to be waited for:\n\ |
| 585 | rlist -- wait until ready for reading\n\ |
| 586 | wlist -- wait until ready for writing\n\ |
| 587 | xlist -- wait for an ``exceptional condition''\n\ |
| 588 | If only one kind of condition is required, pass [] for the other lists.\n\ |
| 589 | A file descriptor is either a socket or file object, or a small integer\n\ |
| 590 | gotten from a fileno() method call on one of those.\n\ |
| 591 | \n\ |
| 592 | The optional 4th argument specifies a timeout in seconds; it may be\n\ |
| 593 | a floating point number to specify fractions of seconds. If it is absent\n\ |
| 594 | or None, the call will never time out.\n\ |
| 595 | \n\ |
| 596 | The return value is a tuple of three lists corresponding to the first three\n\ |
| 597 | arguments; each contains the subset of the corresponding file descriptors\n\ |
| 598 | that are ready.\n\ |
| 599 | \n\ |
| 600 | *** IMPORTANT NOTICE ***\n\ |
| 601 | On Windows, only sockets are supported; on Unix, all file descriptors."; |
| 602 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 603 | static PyMethodDef select_methods[] = { |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 604 | {"select", select_select, METH_VARARGS, select_doc}, |
| 605 | #ifdef HAVE_POLL |
| 606 | {"poll", select_poll, METH_VARARGS, poll_doc}, |
| 607 | #endif /* HAVE_POLL */ |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 608 | {0, 0}, /* sentinel */ |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 609 | }; |
| 610 | |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 611 | static char module_doc[] = |
| 612 | "This module supports asynchronous I/O on multiple file descriptors.\n\ |
| 613 | \n\ |
| 614 | *** IMPORTANT NOTICE ***\n\ |
| 615 | On Windows, only sockets are supported; on Unix, all file descriptors."; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 616 | |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 617 | /* |
| 618 | * Convenience routine to export an integer value. |
| 619 | * For simplicity, errors (which are unlikely anyway) are ignored. |
| 620 | */ |
| 621 | |
| 622 | static void |
| 623 | insint(PyObject *d, char *name, int value) |
| 624 | { |
| 625 | PyObject *v = PyInt_FromLong((long) value); |
| 626 | if (v == NULL) { |
| 627 | /* Don't bother reporting this error */ |
| 628 | PyErr_Clear(); |
| 629 | } |
| 630 | else { |
| 631 | PyDict_SetItemString(d, name, v); |
| 632 | Py_DECREF(v); |
| 633 | } |
| 634 | } |
| 635 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 636 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 637 | initselect(void) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 638 | { |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 639 | PyObject *m, *d; |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 640 | m = Py_InitModule3("select", select_methods, module_doc); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 641 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 642 | SelectError = PyErr_NewException("select.error", NULL, NULL); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 643 | PyDict_SetItemString(d, "error", SelectError); |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 644 | #ifdef HAVE_POLL |
| 645 | poll_Type.ob_type = &PyType_Type; |
| 646 | insint(d, "POLLIN", POLLIN); |
| 647 | insint(d, "POLLPRI", POLLPRI); |
| 648 | insint(d, "POLLOUT", POLLOUT); |
| 649 | insint(d, "POLLERR", POLLERR); |
| 650 | insint(d, "POLLHUP", POLLHUP); |
| 651 | insint(d, "POLLNVAL", POLLNVAL); |
| 652 | |
Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 653 | #ifdef POLLRDNORM |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 654 | insint(d, "POLLRDNORM", POLLRDNORM); |
Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 655 | #endif |
| 656 | #ifdef POLLRDBAND |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 657 | insint(d, "POLLRDBAND", POLLRDBAND); |
Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 658 | #endif |
| 659 | #ifdef POLLWRNORM |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 660 | insint(d, "POLLWRNORM", POLLWRNORM); |
Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 661 | #endif |
| 662 | #ifdef POLLWRBAND |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 663 | insint(d, "POLLWRBAND", POLLWRBAND); |
Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 664 | #endif |
Sjoerd Mullender | 239f836 | 2000-08-25 13:59:18 +0000 | [diff] [blame] | 665 | #ifdef POLLMSG |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 666 | insint(d, "POLLMSG", POLLMSG); |
Sjoerd Mullender | 239f836 | 2000-08-25 13:59:18 +0000 | [diff] [blame] | 667 | #endif |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 668 | #endif /* HAVE_POLL */ |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 669 | } |