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