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