| 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 | { | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 218 | 	pylist rfd2obj[FD_SETSIZE + 3]; | 
 | 219 | 	pylist wfd2obj[FD_SETSIZE + 3]; | 
 | 220 | 	pylist efd2obj[FD_SETSIZE + 3]; | 
 | 221 | 	PyObject *ifdlist, *ofdlist, *efdlist; | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 222 | 	PyObject *ret = NULL; | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 223 | 	PyObject *tout = Py_None; | 
 | 224 | 	fd_set ifdset, ofdset, efdset; | 
 | 225 | 	double timeout; | 
 | 226 | 	struct timeval tv, *tvp; | 
 | 227 | 	int seconds; | 
 | 228 | 	int imax, omax, emax, max; | 
 | 229 | 	int n; | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 230 |  | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 231 | 	/* convert arguments */ | 
 | 232 | 	if (!PyArg_ParseTuple(args, "OOO|O", | 
 | 233 | 			      &ifdlist, &ofdlist, &efdlist, &tout)) | 
 | 234 | 		return NULL; | 
| 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 | 	if (tout == Py_None) | 
 | 237 | 		tvp = (struct timeval *)0; | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 238 | 	else if (!PyArg_Parse(tout, "d", &timeout)) { | 
 | 239 | 		PyErr_SetString(PyExc_TypeError, | 
 | 240 | 				"timeout must be a float or None"); | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 241 | 		return NULL; | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 242 | 	} | 
| Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 243 | 	else { | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 244 | 		seconds = (int)timeout; | 
 | 245 | 		timeout = timeout - (double)seconds; | 
 | 246 | 		tv.tv_sec = seconds; | 
 | 247 | 		tv.tv_usec = (int)(timeout*1000000.0); | 
 | 248 | 		tvp = &tv; | 
| Guido van Rossum | c7a2270 | 1993-11-01 16:27:16 +0000 | [diff] [blame] | 249 | 	} | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 250 |  | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 251 | 	/* sanity check first three arguments */ | 
 | 252 | 	if (!PyList_Check(ifdlist) || | 
 | 253 | 	    !PyList_Check(ofdlist) || | 
 | 254 | 	    !PyList_Check(efdlist)) | 
 | 255 | 	{ | 
 | 256 | 		PyErr_SetString(PyExc_TypeError, | 
 | 257 | 				"arguments 1-3 must be lists"); | 
 | 258 | 		return NULL; | 
 | 259 | 	} | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 260 |  | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 261 | 	/* Convert lists to fd_sets, and get maximum fd number | 
 | 262 | 	 * propagates the Python exception set in list2set() | 
 | 263 | 	 */ | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 264 | 	rfd2obj[0].sentinel = -1; | 
 | 265 | 	wfd2obj[0].sentinel = -1; | 
 | 266 | 	efd2obj[0].sentinel = -1; | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 267 | 	if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0)  | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 268 | 		goto finally; | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 269 | 	if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0)  | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 270 | 		goto finally; | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 271 | 	if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0)  | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 272 | 		goto finally; | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 273 | 	max = imax; | 
 | 274 | 	if (omax > max) max = omax; | 
 | 275 | 	if (emax > max) max = emax; | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 276 |  | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 277 | 	Py_BEGIN_ALLOW_THREADS | 
 | 278 | 	n = select(max, &ifdset, &ofdset, &efdset, tvp); | 
 | 279 | 	Py_END_ALLOW_THREADS | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 280 |  | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 281 | 	if (n < 0) { | 
 | 282 | 		PyErr_SetFromErrno(SelectError); | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 283 | 	} | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 284 | 	else if (n == 0) { | 
 | 285 |                 /* optimization */ | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 286 | 		ifdlist = PyList_New(0); | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 287 | 		if (ifdlist) { | 
 | 288 | 			ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist); | 
 | 289 | 			Py_DECREF(ifdlist); | 
 | 290 | 		} | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 291 | 	} | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 292 | 	else { | 
 | 293 | 		/* any of these three calls can raise an exception.  it's more | 
 | 294 | 		   convenient to test for this after all three calls... but | 
 | 295 | 		   is that acceptable? | 
 | 296 | 		*/ | 
 | 297 | 		ifdlist = set2list(&ifdset, rfd2obj); | 
 | 298 | 		ofdlist = set2list(&ofdset, wfd2obj); | 
 | 299 | 		efdlist = set2list(&efdset, efd2obj); | 
 | 300 | 		if (PyErr_Occurred()) | 
 | 301 | 			ret = NULL; | 
 | 302 | 		else | 
 | 303 | 			ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist); | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 304 |  | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 305 | 		Py_DECREF(ifdlist); | 
 | 306 | 		Py_DECREF(ofdlist); | 
 | 307 | 		Py_DECREF(efdlist); | 
 | 308 | 	} | 
 | 309 | 	 | 
 | 310 |   finally: | 
 | 311 | 	reap_obj(rfd2obj); | 
 | 312 | 	reap_obj(wfd2obj); | 
 | 313 | 	reap_obj(efd2obj); | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 314 | 	return ret; | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 315 | } | 
 | 316 |  | 
 | 317 |  | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 318 | static PyMethodDef select_methods[] = { | 
 | 319 |     {"select",	select_select, 1}, | 
 | 320 |     {0,  	0},			     /* sentinel */ | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 321 | }; | 
 | 322 |  | 
 | 323 |  | 
 | 324 | void | 
 | 325 | initselect() | 
 | 326 | { | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 327 | 	PyObject *m, *d; | 
 | 328 | 	m = Py_InitModule("select", select_methods); | 
 | 329 | 	d = PyModule_GetDict(m); | 
| Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 330 | 	SelectError = PyErr_NewException("select.error", NULL, NULL); | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 331 | 	PyDict_SetItemString(d, "error", SelectError); | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 332 | } |