| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | /***********************************************************
 | 
					
						
							| 
									
										
										
										
											1995-01-04 19:10:35 +00:00
										 |  |  | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, | 
					
						
							|  |  |  | The Netherlands. | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         All Rights Reserved | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-10-25 14:44:06 +00:00
										 |  |  | Permission to use, copy, modify, and distribute this software and its | 
					
						
							|  |  |  | documentation for any purpose and without fee is hereby granted, | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | provided that the above copyright notice appear in all copies and that | 
					
						
							| 
									
										
										
										
											1996-10-25 14:44:06 +00:00
										 |  |  | both that copyright notice and this permission notice appear in | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | supporting documentation, and that the names of Stichting Mathematisch | 
					
						
							| 
									
										
										
										
											1996-10-25 14:44:06 +00:00
										 |  |  | Centrum or CWI or Corporation for National Research Initiatives or | 
					
						
							|  |  |  | CNRI not be used in advertising or publicity pertaining to | 
					
						
							|  |  |  | distribution of the software without specific, written prior | 
					
						
							|  |  |  | permission. | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-10-25 14:44:06 +00:00
										 |  |  | While CWI is the initial source for this software, a modified version | 
					
						
							|  |  |  | is made available by the Corporation for National Research Initiatives | 
					
						
							|  |  |  | (CNRI) at the Internet address ftp://ftp.python.org.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH | 
					
						
							|  |  |  | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF | 
					
						
							|  |  |  | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH | 
					
						
							|  |  |  | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL | 
					
						
							|  |  |  | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | 
					
						
							|  |  |  | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | 
					
						
							|  |  |  | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | 
					
						
							|  |  |  | PERFORMANCE OF THIS SOFTWARE. | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ******************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | /* select - Module containing unix select(2) call.
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  |    Under Unix, the file descriptors are small integers. | 
					
						
							|  |  |  |    Under Win32, select only exists for sockets, and sockets may | 
					
						
							|  |  |  |    have any value except INVALID_SOCKET. | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:43:35 +00:00
										 |  |  | #ifdef HAVE_UNISTD_H
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-09 18:47:43 +00:00
										 |  |  | #ifdef __sgi
 | 
					
						
							|  |  |  | /* This is missing from unistd.h */ | 
					
						
							|  |  |  | extern void bzero(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-01 11:34:53 +00:00
										 |  |  | #include <sys/types.h>
 | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-11-22 21:53:48 +00:00
										 |  |  | #if defined(PYOS_OS2)
 | 
					
						
							|  |  |  | #include <sys/time.h>
 | 
					
						
							|  |  |  | #include <utils.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-06-28 20:15:15 +00:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | #include <winsock.h>
 | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											1994-08-01 11:34:53 +00:00
										 |  |  | #include "myselect.h" /* Also includes mytime.h */
 | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | #define SOCKET int
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | static PyObject *SelectError; | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | /* list of Python objects and their file descriptor */ | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  | 	PyObject *obj;			     /* owned reference */ | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | 	SOCKET fd; | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 	int sentinel;			     /* -1 == sentinel */ | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | } pylist; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | reap_obj(fd2obj) | 
					
						
							|  |  |  | 	pylist fd2obj[FD_SETSIZE + 3]; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	for (i = 0; i < FD_SETSIZE + 3 && fd2obj[i].sentinel >= 0; i++) { | 
					
						
							|  |  |  | 		Py_XDECREF(fd2obj[i].obj); | 
					
						
							|  |  |  | 		fd2obj[i].obj = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	fd2obj[0].sentinel = -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | /* returns -1 and sets the Python exception if an error occurred, otherwise
 | 
					
						
							|  |  |  |    returns a number >= 0 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											1992-08-04 09:13:45 +00:00
										 |  |  | list2set(list, set, fd2obj) | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	PyObject *list; | 
					
						
							|  |  |  | 	fd_set *set; | 
					
						
							|  |  |  | 	pylist fd2obj[FD_SETSIZE + 3]; | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 	int i; | 
					
						
							|  |  |  | 	int max = -1; | 
					
						
							|  |  |  | 	int index = 0; | 
					
						
							|  |  |  | 	int len = PyList_Size(list); | 
					
						
							|  |  |  | 	PyObject* o = NULL; | 
					
						
							| 
									
										
										
										
											1995-03-29 16:47:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	fd2obj[0].obj = (PyObject*)0;	     /* set list to zero size */ | 
					
						
							|  |  |  | 	FD_ZERO(set); | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < len; i++)  { | 
					
						
							|  |  |  | 		PyObject *meth; | 
					
						
							|  |  |  | 		SOCKET v; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* any intervening fileno() calls could decr this refcnt */ | 
					
						
							| 
									
										
										
										
											1996-12-13 23:22:42 +00:00
										 |  |  | 		if (!(o = PyList_GetItem(list, i))) | 
					
						
							| 
									
										
										
										
											1996-12-16 18:15:34 +00:00
										 |  |  |                     return -1; | 
					
						
							| 
									
										
										
										
											1996-12-13 23:22:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 		Py_INCREF(o); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		if (PyInt_Check(o)) { | 
					
						
							|  |  |  | 			v = PyInt_AsLong(o); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 			PyObject *fno = PyEval_CallObject(meth, NULL); | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 			Py_DECREF(meth); | 
					
						
							|  |  |  | 			if (fno == NULL) | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 				goto finally; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  |                         if (!PyInt_Check(fno)) { | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 				PyErr_SetString(PyExc_TypeError, | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  |                                        "fileno method returned a non-integer"); | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 				Py_DECREF(fno); | 
					
						
							|  |  |  | 				goto finally; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  |                         } | 
					
						
							|  |  |  |                         v = PyInt_AsLong(fno); | 
					
						
							|  |  |  | 			Py_DECREF(fno); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			PyErr_SetString(PyExc_TypeError, | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 			"argument must be an int, or have a fileno() method."); | 
					
						
							|  |  |  | 			goto finally; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | #ifdef _MSC_VER
 | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 		max = 0;		     /* not used for Win32 */ | 
					
						
							|  |  |  | #else  /* !_MSC_VER */
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		if (v < 0 || v >= FD_SETSIZE) { | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 			PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 				    "filedescriptor out of range in select()"); | 
					
						
							|  |  |  | 			goto finally; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (v > max) | 
					
						
							|  |  |  | 			max = v; | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | #endif /* _MSC_VER */
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		FD_SET(v, set); | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		/* add object and its file descriptor to the list */ | 
					
						
							|  |  |  | 		if (index >= FD_SETSIZE) { | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 			PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 				      "too many file descriptors in select()"); | 
					
						
							|  |  |  | 			goto finally; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		fd2obj[index].obj = o; | 
					
						
							|  |  |  | 		fd2obj[index].fd = v; | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 		fd2obj[index].sentinel = 0; | 
					
						
							|  |  |  | 		fd2obj[++index].sentinel = -1; | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	return max+1; | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   finally: | 
					
						
							|  |  |  | 	Py_XDECREF(o); | 
					
						
							|  |  |  | 	return -1; | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | /* returns NULL and sets the Python exception if an error occurred */ | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | set2list(set, fd2obj) | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	fd_set *set; | 
					
						
							|  |  |  | 	pylist fd2obj[FD_SETSIZE + 3]; | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 	int i, j, count=0; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	PyObject *list, *o; | 
					
						
							|  |  |  | 	SOCKET fd; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 	for (j = 0; fd2obj[j].sentinel >= 0; j++) { | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		if (FD_ISSET(fd2obj[j].fd, set)) | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 			count++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	list = PyList_New(count); | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	if (!list) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 	i = 0; | 
					
						
							|  |  |  | 	for (j = 0; fd2obj[j].sentinel >= 0; j++) { | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		fd = fd2obj[j].fd; | 
					
						
							|  |  |  | 		if (FD_ISSET(fd, set)) { | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | #ifndef _MSC_VER
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 			if (fd > FD_SETSIZE) { | 
					
						
							|  |  |  | 				PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  | 			   "filedescriptor out of range returned in select()"); | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 				goto finally; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											1996-06-12 04:22:53 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 			o = fd2obj[j].obj; | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 			fd2obj[j].obj = NULL; | 
					
						
							|  |  |  | 			/* transfer ownership */ | 
					
						
							|  |  |  | 			if (PyList_SetItem(list, i, o) < 0) | 
					
						
							|  |  |  | 				goto finally; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			i++; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return list; | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  |   finally: | 
					
						
							|  |  |  | 	Py_DECREF(list); | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | select_select(self, args) | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	PyObject *self; | 
					
						
							|  |  |  | 	PyObject *args; | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1998-07-02 02:53:36 +00:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  | 	/* This would be an awful lot of stack space on Windows! */ | 
					
						
							|  |  |  | 	pylist *rfd2obj, *wfd2obj, *efd2obj; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	pylist rfd2obj[FD_SETSIZE + 3]; | 
					
						
							|  |  |  | 	pylist wfd2obj[FD_SETSIZE + 3]; | 
					
						
							|  |  |  | 	pylist efd2obj[FD_SETSIZE + 3]; | 
					
						
							| 
									
										
										
										
											1998-07-02 02:53:36 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	PyObject *ifdlist, *ofdlist, *efdlist; | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 	PyObject *ret = NULL; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	PyObject *tout = Py_None; | 
					
						
							|  |  |  | 	fd_set ifdset, ofdset, efdset; | 
					
						
							|  |  |  | 	double timeout; | 
					
						
							|  |  |  | 	struct timeval tv, *tvp; | 
					
						
							|  |  |  | 	int seconds; | 
					
						
							|  |  |  | 	int imax, omax, emax, max; | 
					
						
							|  |  |  | 	int n; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* convert arguments */ | 
					
						
							|  |  |  | 	if (!PyArg_ParseTuple(args, "OOO|O", | 
					
						
							|  |  |  | 			      &ifdlist, &ofdlist, &efdlist, &tout)) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (tout == Py_None) | 
					
						
							|  |  |  | 		tvp = (struct timeval *)0; | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 	else if (!PyArg_Parse(tout, "d", &timeout)) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"timeout must be a float or None"); | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1993-11-01 16:27:16 +00:00
										 |  |  | 	else { | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		seconds = (int)timeout; | 
					
						
							|  |  |  | 		timeout = timeout - (double)seconds; | 
					
						
							|  |  |  | 		tv.tv_sec = seconds; | 
					
						
							|  |  |  | 		tv.tv_usec = (int)(timeout*1000000.0); | 
					
						
							|  |  |  | 		tvp = &tv; | 
					
						
							| 
									
										
										
										
											1993-11-01 16:27:16 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* sanity check first three arguments */ | 
					
						
							|  |  |  | 	if (!PyList_Check(ifdlist) || | 
					
						
							|  |  |  | 	    !PyList_Check(ofdlist) || | 
					
						
							|  |  |  | 	    !PyList_Check(efdlist)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"arguments 1-3 must be lists"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-07-02 02:53:36 +00:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  | 	/* Allocate memory for the lists */ | 
					
						
							|  |  |  | 	rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); | 
					
						
							|  |  |  | 	wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); | 
					
						
							|  |  |  | 	efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3); | 
					
						
							|  |  |  | 	if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) { | 
					
						
							|  |  |  | 		PyMem_XDEL(rfd2obj); | 
					
						
							|  |  |  | 		PyMem_XDEL(wfd2obj); | 
					
						
							|  |  |  | 		PyMem_XDEL(efd2obj); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	/* Convert lists to fd_sets, and get maximum fd number
 | 
					
						
							|  |  |  | 	 * propagates the Python exception set in list2set() | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 	rfd2obj[0].sentinel = -1; | 
					
						
							|  |  |  | 	wfd2obj[0].sentinel = -1; | 
					
						
							|  |  |  | 	efd2obj[0].sentinel = -1; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0)  | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 		goto finally; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0)  | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 		goto finally; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0)  | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 		goto finally; | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	max = imax; | 
					
						
							|  |  |  | 	if (omax > max) max = omax; | 
					
						
							|  |  |  | 	if (emax > max) max = emax; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  | 	n = select(max, &ifdset, &ofdset, &efdset, tvp); | 
					
						
							|  |  |  | 	Py_END_ALLOW_THREADS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (n < 0) { | 
					
						
							|  |  |  | 		PyErr_SetFromErrno(SelectError); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 	else if (n == 0) { | 
					
						
							|  |  |  |                 /* optimization */ | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 		ifdlist = PyList_New(0); | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 		if (ifdlist) { | 
					
						
							|  |  |  | 			ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist); | 
					
						
							|  |  |  | 			Py_DECREF(ifdlist); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1996-12-12 22:16:21 +00:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		/* any of these three calls can raise an exception.  it's more
 | 
					
						
							|  |  |  | 		   convenient to test for this after all three calls... but | 
					
						
							|  |  |  | 		   is that acceptable? | 
					
						
							|  |  |  | 		*/ | 
					
						
							|  |  |  | 		ifdlist = set2list(&ifdset, rfd2obj); | 
					
						
							|  |  |  | 		ofdlist = set2list(&ofdset, wfd2obj); | 
					
						
							|  |  |  | 		efdlist = set2list(&efdset, efd2obj); | 
					
						
							|  |  |  | 		if (PyErr_Occurred()) | 
					
						
							|  |  |  | 			ret = NULL; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Py_DECREF(ifdlist); | 
					
						
							|  |  |  | 		Py_DECREF(ofdlist); | 
					
						
							|  |  |  | 		Py_DECREF(efdlist); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  |   finally: | 
					
						
							|  |  |  | 	reap_obj(rfd2obj); | 
					
						
							|  |  |  | 	reap_obj(wfd2obj); | 
					
						
							|  |  |  | 	reap_obj(efd2obj); | 
					
						
							| 
									
										
										
										
											1998-07-02 02:53:36 +00:00
										 |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  | 	PyMem_DEL(rfd2obj); | 
					
						
							|  |  |  | 	PyMem_DEL(wfd2obj); | 
					
						
							|  |  |  | 	PyMem_DEL(efd2obj); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	return ret; | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-06-28 16:54:49 +00:00
										 |  |  | static char select_doc[] = | 
					
						
							|  |  |  | "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
 | 
					
						
							|  |  |  | \n\ | 
					
						
							|  |  |  | Wait until one or more file descriptors are ready for some kind of I/O.\n\ | 
					
						
							|  |  |  | The first three arguments are lists of file descriptors to be waited for:\n\ | 
					
						
							|  |  |  | rlist -- wait until ready for reading\n\ | 
					
						
							|  |  |  | wlist -- wait until ready for writing\n\ | 
					
						
							|  |  |  | xlist -- wait for an ``exceptional condition''\n\ | 
					
						
							|  |  |  | If only one kind of condition is required, pass [] for the other lists.\n\ | 
					
						
							|  |  |  | A file descriptor is either a socket or file object, or a small integer\n\ | 
					
						
							|  |  |  | gotten from a fileno() method call on one of those.\n\ | 
					
						
							|  |  |  | \n\ | 
					
						
							|  |  |  | The optional 4th argument specifies a timeout in seconds; it may be\n\ | 
					
						
							|  |  |  | a floating point number to specify fractions of seconds.  If it is absent\n\ | 
					
						
							|  |  |  | or None, the call will never time out.\n\ | 
					
						
							|  |  |  | \n\ | 
					
						
							|  |  |  | The return value is a tuple of three lists corresponding to the first three\n\ | 
					
						
							|  |  |  | arguments; each contains the subset of the corresponding file descriptors\n\ | 
					
						
							|  |  |  | that are ready.\n\ | 
					
						
							|  |  |  | \n\ | 
					
						
							|  |  |  | *** IMPORTANT NOTICE ***\n\ | 
					
						
							|  |  |  | On Windows, only sockets are supported; on Unix, all file descriptors."; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | static PyMethodDef select_methods[] = { | 
					
						
							| 
									
										
										
										
											1998-06-28 16:54:49 +00:00
										 |  |  |     {"select",	select_select, 1, select_doc}, | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  |     {0,  	0},			     /* sentinel */ | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-06-28 16:54:49 +00:00
										 |  |  | static char module_doc[] = | 
					
						
							|  |  |  | "This module supports asynchronous I/O on multiple file descriptors.\n\
 | 
					
						
							|  |  |  | \n\ | 
					
						
							|  |  |  | *** IMPORTANT NOTICE ***\n\ | 
					
						
							|  |  |  | On Windows, only sockets are supported; on Unix, all file descriptors."; | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | initselect() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	PyObject *m, *d; | 
					
						
							| 
									
										
										
										
											1998-06-28 16:54:49 +00:00
										 |  |  | 	m = Py_InitModule3("select", select_methods, module_doc); | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	d = PyModule_GetDict(m); | 
					
						
							| 
									
										
										
										
											1997-10-01 04:29:29 +00:00
										 |  |  | 	SelectError = PyErr_NewException("select.error", NULL, NULL); | 
					
						
							| 
									
										
										
										
											1996-12-12 00:04:35 +00:00
										 |  |  | 	PyDict_SetItemString(d, "error", SelectError); | 
					
						
							| 
									
										
										
										
											1992-06-23 09:07:03 +00:00
										 |  |  | } |