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