Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 11 | /* select - Module containing unix select(2) call. |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 12 | Under Unix, the file descriptors are small integers. |
| 13 | Under Win32, select only exists for sockets, and sockets may |
| 14 | have any value except INVALID_SOCKET. |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 15 | Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything |
| 16 | >= 0. |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 17 | */ |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 18 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 19 | #include "Python.h" |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 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 |
| 24 | |
Guido van Rossum | 3727317 | 1996-12-09 18:47:43 +0000 | [diff] [blame] | 25 | #ifdef __sgi |
| 26 | /* This is missing from unistd.h */ |
| 27 | extern void bzero(); |
| 28 | #endif |
| 29 | |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 30 | #ifndef DONT_HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 32 | #endif |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 34 | #if defined(PYOS_OS2) |
| 35 | #include <sys/time.h> |
| 36 | #include <utils.h> |
| 37 | #endif |
| 38 | |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 39 | #ifdef MS_WINDOWS |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 40 | #include <winsock.h> |
| 41 | #else |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 42 | #ifdef __BEOS__ |
| 43 | #include <net/socket.h> |
| 44 | #define SOCKET int |
| 45 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | #include "myselect.h" /* Also includes mytime.h */ |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 47 | #define SOCKET int |
| 48 | #endif |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 49 | #endif |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 50 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 51 | static PyObject *SelectError; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 52 | |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 53 | /* list of Python objects and their file descriptor */ |
| 54 | typedef struct { |
| 55 | PyObject *obj; /* owned reference */ |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 56 | SOCKET fd; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 57 | int sentinel; /* -1 == sentinel */ |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 58 | } pylist; |
| 59 | |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 60 | static void |
| 61 | reap_obj(fd2obj) |
| 62 | pylist fd2obj[FD_SETSIZE + 3]; |
| 63 | { |
| 64 | int i; |
| 65 | for (i = 0; i < FD_SETSIZE + 3 && fd2obj[i].sentinel >= 0; i++) { |
| 66 | Py_XDECREF(fd2obj[i].obj); |
| 67 | fd2obj[i].obj = NULL; |
| 68 | } |
| 69 | fd2obj[0].sentinel = -1; |
| 70 | } |
| 71 | |
| 72 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 73 | /* returns -1 and sets the Python exception if an error occurred, otherwise |
| 74 | returns a number >= 0 |
| 75 | */ |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 76 | static int |
Guido van Rossum | 4fbf798 | 1992-08-04 09:13:45 +0000 | [diff] [blame] | 77 | list2set(list, set, fd2obj) |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 78 | PyObject *list; |
| 79 | fd_set *set; |
| 80 | pylist fd2obj[FD_SETSIZE + 3]; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 81 | { |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 82 | int i; |
| 83 | int max = -1; |
| 84 | int index = 0; |
| 85 | int len = PyList_Size(list); |
| 86 | PyObject* o = NULL; |
Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 87 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 88 | fd2obj[0].obj = (PyObject*)0; /* set list to zero size */ |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 89 | FD_ZERO(set); |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 90 | |
| 91 | for (i = 0; i < len; i++) { |
| 92 | PyObject *meth; |
| 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); |
| 100 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 101 | if (PyInt_Check(o)) { |
| 102 | v = PyInt_AsLong(o); |
| 103 | } |
| 104 | else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) |
| 105 | { |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 106 | PyObject *fno = PyEval_CallObject(meth, NULL); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 107 | Py_DECREF(meth); |
| 108 | if (fno == NULL) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 109 | goto finally; |
| 110 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 111 | if (!PyInt_Check(fno)) { |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 112 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 113 | "fileno method returned a non-integer"); |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 114 | Py_DECREF(fno); |
| 115 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 116 | } |
| 117 | v = PyInt_AsLong(fno); |
| 118 | Py_DECREF(fno); |
| 119 | } |
| 120 | else { |
| 121 | PyErr_SetString(PyExc_TypeError, |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 122 | "argument must be an int, or have a fileno() method."); |
| 123 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 124 | } |
Guido van Rossum | 947a0fa | 2000-01-14 16:33:09 +0000 | [diff] [blame] | 125 | #if defined(_MSC_VER) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 126 | max = 0; /* not used for Win32 */ |
| 127 | #else /* !_MSC_VER */ |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 128 | if (v < 0 || v >= FD_SETSIZE) { |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 129 | PyErr_SetString(PyExc_ValueError, |
| 130 | "filedescriptor out of range in select()"); |
| 131 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 132 | } |
| 133 | if (v > max) |
| 134 | max = v; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 135 | #endif /* _MSC_VER */ |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 136 | FD_SET(v, set); |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 137 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 138 | /* add object and its file descriptor to the list */ |
| 139 | if (index >= FD_SETSIZE) { |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 140 | PyErr_SetString(PyExc_ValueError, |
| 141 | "too many file descriptors in select()"); |
| 142 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 143 | } |
| 144 | fd2obj[index].obj = o; |
| 145 | fd2obj[index].fd = v; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 146 | fd2obj[index].sentinel = 0; |
| 147 | fd2obj[++index].sentinel = -1; |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 148 | } |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 149 | return max+1; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 150 | |
| 151 | finally: |
| 152 | Py_XDECREF(o); |
| 153 | return -1; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 156 | /* returns NULL and sets the Python exception if an error occurred */ |
| 157 | static PyObject * |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 158 | set2list(set, fd2obj) |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 159 | fd_set *set; |
| 160 | pylist fd2obj[FD_SETSIZE + 3]; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 161 | { |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 162 | int i, j, count=0; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 163 | PyObject *list, *o; |
| 164 | SOCKET fd; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 165 | |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 166 | for (j = 0; fd2obj[j].sentinel >= 0; j++) { |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 167 | if (FD_ISSET(fd2obj[j].fd, set)) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 168 | count++; |
| 169 | } |
| 170 | list = PyList_New(count); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 171 | if (!list) |
| 172 | return NULL; |
| 173 | |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 174 | i = 0; |
| 175 | for (j = 0; fd2obj[j].sentinel >= 0; j++) { |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 176 | fd = fd2obj[j].fd; |
| 177 | if (FD_ISSET(fd, set)) { |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 178 | #ifndef _MSC_VER |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 179 | if (fd > FD_SETSIZE) { |
| 180 | PyErr_SetString(PyExc_SystemError, |
| 181 | "filedescriptor out of range returned in select()"); |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 182 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 183 | } |
Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 184 | #endif |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 185 | o = fd2obj[j].obj; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 186 | fd2obj[j].obj = NULL; |
| 187 | /* transfer ownership */ |
| 188 | if (PyList_SetItem(list, i, o) < 0) |
| 189 | goto finally; |
| 190 | |
| 191 | i++; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | return list; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 195 | finally: |
| 196 | Py_DECREF(list); |
| 197 | return NULL; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 198 | } |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 199 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 200 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 201 | static PyObject * |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 202 | select_select(self, args) |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 203 | PyObject *self; |
| 204 | PyObject *args; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 205 | { |
Guido van Rossum | d20781b | 1998-07-02 02:53:36 +0000 | [diff] [blame] | 206 | #ifdef MS_WINDOWS |
| 207 | /* This would be an awful lot of stack space on Windows! */ |
| 208 | pylist *rfd2obj, *wfd2obj, *efd2obj; |
| 209 | #else |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 210 | pylist rfd2obj[FD_SETSIZE + 3]; |
| 211 | pylist wfd2obj[FD_SETSIZE + 3]; |
| 212 | pylist efd2obj[FD_SETSIZE + 3]; |
Guido van Rossum | d20781b | 1998-07-02 02:53:36 +0000 | [diff] [blame] | 213 | #endif |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 214 | PyObject *ifdlist, *ofdlist, *efdlist; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 215 | PyObject *ret = NULL; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 216 | PyObject *tout = Py_None; |
| 217 | fd_set ifdset, ofdset, efdset; |
| 218 | double timeout; |
| 219 | struct timeval tv, *tvp; |
Guido van Rossum | 3262e16 | 2000-06-28 21:18:13 +0000 | [diff] [blame] | 220 | long seconds; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 221 | int imax, omax, emax, max; |
| 222 | int n; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 223 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 224 | /* convert arguments */ |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 225 | if (!PyArg_ParseTuple(args, "OOO|O:select", |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 226 | &ifdlist, &ofdlist, &efdlist, &tout)) |
| 227 | return NULL; |
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 | if (tout == Py_None) |
| 230 | tvp = (struct timeval *)0; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 231 | else if (!PyArg_Parse(tout, "d", &timeout)) { |
| 232 | PyErr_SetString(PyExc_TypeError, |
| 233 | "timeout must be a float or None"); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 234 | return NULL; |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 235 | } |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 236 | else { |
Guido van Rossum | 3262e16 | 2000-06-28 21:18:13 +0000 | [diff] [blame] | 237 | if (timeout > (double)LONG_MAX) { |
| 238 | PyErr_SetString(PyExc_OverflowError, "timeout period too long"); |
| 239 | return NULL; |
| 240 | } |
| 241 | seconds = (long)timeout; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 242 | timeout = timeout - (double)seconds; |
| 243 | tv.tv_sec = seconds; |
Guido van Rossum | 3262e16 | 2000-06-28 21:18:13 +0000 | [diff] [blame] | 244 | tv.tv_usec = (long)(timeout*1000000.0); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 245 | tvp = &tv; |
Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 246 | } |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 247 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 248 | /* sanity check first three arguments */ |
| 249 | if (!PyList_Check(ifdlist) || |
| 250 | !PyList_Check(ofdlist) || |
| 251 | !PyList_Check(efdlist)) |
| 252 | { |
| 253 | PyErr_SetString(PyExc_TypeError, |
| 254 | "arguments 1-3 must be lists"); |
| 255 | return NULL; |
| 256 | } |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | d20781b | 1998-07-02 02:53:36 +0000 | [diff] [blame] | 258 | #ifdef MS_WINDOWS |
| 259 | /* Allocate memory for the lists */ |
| 260 | rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); |
| 261 | wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); |
| 262 | efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); |
| 263 | if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 264 | if (rfd2obj) PyMem_DEL(rfd2obj); |
| 265 | if (wfd2obj) PyMem_DEL(wfd2obj); |
| 266 | if (efd2obj) PyMem_DEL(efd2obj); |
Guido van Rossum | d20781b | 1998-07-02 02:53:36 +0000 | [diff] [blame] | 267 | return NULL; |
| 268 | } |
| 269 | #endif |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 270 | /* Convert lists to fd_sets, and get maximum fd number |
| 271 | * propagates the Python exception set in list2set() |
| 272 | */ |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 273 | rfd2obj[0].sentinel = -1; |
| 274 | wfd2obj[0].sentinel = -1; |
| 275 | efd2obj[0].sentinel = -1; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 276 | if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 277 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 278 | if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 279 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 280 | if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0) |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 281 | goto finally; |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 282 | max = imax; |
| 283 | if (omax > max) max = omax; |
| 284 | if (emax > max) max = emax; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 285 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 286 | Py_BEGIN_ALLOW_THREADS |
| 287 | n = select(max, &ifdset, &ofdset, &efdset, tvp); |
| 288 | Py_END_ALLOW_THREADS |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 289 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 290 | if (n < 0) { |
| 291 | PyErr_SetFromErrno(SelectError); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 292 | } |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 293 | else if (n == 0) { |
| 294 | /* optimization */ |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 295 | ifdlist = PyList_New(0); |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 296 | if (ifdlist) { |
| 297 | ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist); |
| 298 | Py_DECREF(ifdlist); |
| 299 | } |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 300 | } |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 301 | else { |
| 302 | /* any of these three calls can raise an exception. it's more |
| 303 | convenient to test for this after all three calls... but |
| 304 | is that acceptable? |
| 305 | */ |
| 306 | ifdlist = set2list(&ifdset, rfd2obj); |
| 307 | ofdlist = set2list(&ofdset, wfd2obj); |
| 308 | efdlist = set2list(&efdset, efd2obj); |
| 309 | if (PyErr_Occurred()) |
| 310 | ret = NULL; |
| 311 | else |
| 312 | ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 313 | |
Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 314 | Py_DECREF(ifdlist); |
| 315 | Py_DECREF(ofdlist); |
| 316 | Py_DECREF(efdlist); |
| 317 | } |
| 318 | |
| 319 | finally: |
| 320 | reap_obj(rfd2obj); |
| 321 | reap_obj(wfd2obj); |
| 322 | reap_obj(efd2obj); |
Guido van Rossum | d20781b | 1998-07-02 02:53:36 +0000 | [diff] [blame] | 323 | #ifdef MS_WINDOWS |
| 324 | PyMem_DEL(rfd2obj); |
| 325 | PyMem_DEL(wfd2obj); |
| 326 | PyMem_DEL(efd2obj); |
| 327 | #endif |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 328 | return ret; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 331 | static char select_doc[] = |
| 332 | "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\ |
| 333 | \n\ |
| 334 | Wait until one or more file descriptors are ready for some kind of I/O.\n\ |
| 335 | The first three arguments are lists of file descriptors to be waited for:\n\ |
| 336 | rlist -- wait until ready for reading\n\ |
| 337 | wlist -- wait until ready for writing\n\ |
| 338 | xlist -- wait for an ``exceptional condition''\n\ |
| 339 | If only one kind of condition is required, pass [] for the other lists.\n\ |
| 340 | A file descriptor is either a socket or file object, or a small integer\n\ |
| 341 | gotten from a fileno() method call on one of those.\n\ |
| 342 | \n\ |
| 343 | The optional 4th argument specifies a timeout in seconds; it may be\n\ |
| 344 | a floating point number to specify fractions of seconds. If it is absent\n\ |
| 345 | or None, the call will never time out.\n\ |
| 346 | \n\ |
| 347 | The return value is a tuple of three lists corresponding to the first three\n\ |
| 348 | arguments; each contains the subset of the corresponding file descriptors\n\ |
| 349 | that are ready.\n\ |
| 350 | \n\ |
| 351 | *** IMPORTANT NOTICE ***\n\ |
| 352 | On Windows, only sockets are supported; on Unix, all file descriptors."; |
| 353 | |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 354 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 355 | static PyMethodDef select_methods[] = { |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 356 | {"select", select_select, 1, select_doc}, |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 357 | {0, 0}, /* sentinel */ |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 358 | }; |
| 359 | |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 360 | static char module_doc[] = |
| 361 | "This module supports asynchronous I/O on multiple file descriptors.\n\ |
| 362 | \n\ |
| 363 | *** IMPORTANT NOTICE ***\n\ |
| 364 | On Windows, only sockets are supported; on Unix, all file descriptors."; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 366 | DL_EXPORT(void) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 367 | initselect() |
| 368 | { |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 369 | PyObject *m, *d; |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 370 | m = Py_InitModule3("select", select_methods, module_doc); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 371 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 372 | SelectError = PyErr_NewException("select.error", NULL, NULL); |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 373 | PyDict_SetItemString(d, "error", SelectError); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 374 | } |