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