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