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