1 /* select - Module containing unix select(2) call.
2 Under Unix, the file descriptors are small integers.
3 Under Win32, select only exists for sockets, and sockets may
4 have any value except INVALID_SOCKET.
5 Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything
11 /* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.
12 64 is too small (too many people have bumped into that limit).
14 Users who want even more than the boosted limit should #define
15 FD_SETSIZE higher before this; e.g., via compiler /D switch.
17 #if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
18 #define FD_SETSIZE 512
29 /* This is missing from unistd.h */
30 extern void bzero(void *, int);
33 #ifndef DONT_HAVE_SYS_TYPES_H
34 #include <sys/types.h>
46 #include <net/socket.h>
53 static PyObject
*SelectError
;
55 /* list of Python objects and their file descriptor */
57 PyObject
*obj
; /* owned reference */
59 int sentinel
; /* -1 == sentinel */
63 reap_obj(pylist fd2obj
[FD_SETSIZE
+ 3])
66 for (i
= 0; i
< FD_SETSIZE
+ 3 && fd2obj
[i
].sentinel
>= 0; i
++) {
67 Py_XDECREF(fd2obj
[i
].obj
);
70 fd2obj
[0].sentinel
= -1;
74 /* returns -1 and sets the Python exception if an error occurred, otherwise
78 list2set(PyObject
*list
, fd_set
*set
, pylist fd2obj
[FD_SETSIZE
+ 3])
83 int len
= PyList_Size(list
);
86 fd2obj
[0].obj
= (PyObject
*)0; /* set list to zero size */
89 for (i
= 0; i
< len
; i
++) {
92 /* any intervening fileno() calls could decr this refcnt */
93 if (!(o
= PyList_GetItem(list
, i
)))
97 v
= PyObject_AsFileDescriptor( o
);
98 if (v
== -1) goto finally
;
100 #if defined(_MSC_VER)
101 max
= 0; /* not used for Win32 */
102 #else /* !_MSC_VER */
103 if (v
< 0 || v
>= FD_SETSIZE
) {
104 PyErr_SetString(PyExc_ValueError
,
105 "filedescriptor out of range in select()");
110 #endif /* _MSC_VER */
113 /* add object and its file descriptor to the list */
114 if (index
>= FD_SETSIZE
) {
115 PyErr_SetString(PyExc_ValueError
,
116 "too many file descriptors in select()");
119 fd2obj
[index
].obj
= o
;
120 fd2obj
[index
].fd
= v
;
121 fd2obj
[index
].sentinel
= 0;
122 fd2obj
[++index
].sentinel
= -1;
131 /* returns NULL and sets the Python exception if an error occurred */
133 set2list(fd_set
*set
, pylist fd2obj
[FD_SETSIZE
+ 3])
139 for (j
= 0; fd2obj
[j
].sentinel
>= 0; j
++) {
140 if (FD_ISSET(fd2obj
[j
].fd
, set
))
143 list
= PyList_New(count
);
148 for (j
= 0; fd2obj
[j
].sentinel
>= 0; j
++) {
150 if (FD_ISSET(fd
, set
)) {
152 if (fd
> FD_SETSIZE
) {
153 PyErr_SetString(PyExc_SystemError
,
154 "filedescriptor out of range returned in select()");
159 fd2obj
[j
].obj
= NULL
;
160 /* transfer ownership */
161 if (PyList_SetItem(list
, i
, o
) < 0)
175 select_select(PyObject
*self
, PyObject
*args
)
178 /* This would be an awful lot of stack space on Windows! */
179 pylist
*rfd2obj
, *wfd2obj
, *efd2obj
;
181 pylist rfd2obj
[FD_SETSIZE
+ 3];
182 pylist wfd2obj
[FD_SETSIZE
+ 3];
183 pylist efd2obj
[FD_SETSIZE
+ 3];
185 PyObject
*ifdlist
, *ofdlist
, *efdlist
;
186 PyObject
*ret
= NULL
;
187 PyObject
*tout
= Py_None
;
188 fd_set ifdset
, ofdset
, efdset
;
190 struct timeval tv
, *tvp
;
192 int imax
, omax
, emax
, max
;
195 /* convert arguments */
196 if (!PyArg_ParseTuple(args
, "OOO|O:select",
197 &ifdlist
, &ofdlist
, &efdlist
, &tout
))
201 tvp
= (struct timeval
*)0;
202 else if (!PyArg_Parse(tout
, "d", &timeout
)) {
203 PyErr_SetString(PyExc_TypeError
,
204 "timeout must be a float or None");
208 if (timeout
> (double)LONG_MAX
) {
209 PyErr_SetString(PyExc_OverflowError
, "timeout period too long");
212 seconds
= (long)timeout
;
213 timeout
= timeout
- (double)seconds
;
215 tv
.tv_usec
= (long)(timeout
*1000000.0);
219 /* sanity check first three arguments */
220 if (!PyList_Check(ifdlist
) ||
221 !PyList_Check(ofdlist
) ||
222 !PyList_Check(efdlist
))
224 PyErr_SetString(PyExc_TypeError
,
225 "arguments 1-3 must be lists");
230 /* Allocate memory for the lists */
231 rfd2obj
= PyMem_NEW(pylist
, FD_SETSIZE
+ 3);
232 wfd2obj
= PyMem_NEW(pylist
, FD_SETSIZE
+ 3);
233 efd2obj
= PyMem_NEW(pylist
, FD_SETSIZE
+ 3);
234 if (rfd2obj
== NULL
|| wfd2obj
== NULL
|| efd2obj
== NULL
) {
235 if (rfd2obj
) PyMem_DEL(rfd2obj
);
236 if (wfd2obj
) PyMem_DEL(wfd2obj
);
237 if (efd2obj
) PyMem_DEL(efd2obj
);
241 /* Convert lists to fd_sets, and get maximum fd number
242 * propagates the Python exception set in list2set()
244 rfd2obj
[0].sentinel
= -1;
245 wfd2obj
[0].sentinel
= -1;
246 efd2obj
[0].sentinel
= -1;
247 if ((imax
=list2set(ifdlist
, &ifdset
, rfd2obj
)) < 0)
249 if ((omax
=list2set(ofdlist
, &ofdset
, wfd2obj
)) < 0)
251 if ((emax
=list2set(efdlist
, &efdset
, efd2obj
)) < 0)
254 if (omax
> max
) max
= omax
;
255 if (emax
> max
) max
= emax
;
257 Py_BEGIN_ALLOW_THREADS
258 n
= select(max
, &ifdset
, &ofdset
, &efdset
, tvp
);
262 PyErr_SetFromErrno(SelectError
);
266 ifdlist
= PyList_New(0);
268 ret
= Py_BuildValue("OOO", ifdlist
, ifdlist
, ifdlist
);
273 /* any of these three calls can raise an exception. it's more
274 convenient to test for this after all three calls... but
277 ifdlist
= set2list(&ifdset
, rfd2obj
);
278 ofdlist
= set2list(&ofdset
, wfd2obj
);
279 efdlist
= set2list(&efdset
, efd2obj
);
280 if (PyErr_Occurred())
283 ret
= Py_BuildValue("OOO", ifdlist
, ofdlist
, efdlist
);
315 staticforward PyTypeObject poll_Type
;
317 /* Update the malloc'ed array of pollfds to match the dictionary
318 contained within a pollObject. Return 1 on success, 0 on an error.
322 update_ufd_array(pollObject
*self
)
325 PyObject
*key
, *value
;
327 self
->ufd_len
= PyDict_Size(self
->dict
);
328 PyMem_Resize(self
->ufds
, struct pollfd
, self
->ufd_len
);
329 if (self
->ufds
== NULL
) {
335 while ((j
= PyDict_Next(self
->dict
, &pos
, &key
, &value
))) {
336 self
->ufds
[i
].fd
= PyInt_AsLong(key
);
337 self
->ufds
[i
].events
= PyInt_AsLong(value
);
340 self
->ufd_uptodate
= 1;
344 static char poll_register_doc
[] =
345 "register(fd [, eventmask] ) -> None\n\n\
346 Register a file descriptor with the polling object.\n\
347 fd -- either an integer, or an object with a fileno() method returning an int.\n\
348 events -- an optional bitmask describing the type of events to check for";
351 poll_register(pollObject
*self
, PyObject
*args
)
353 PyObject
*o
, *key
, *value
;
354 int fd
, events
= POLLIN
| POLLPRI
| POLLOUT
;
356 if (!PyArg_ParseTuple(args
, "O|i", &o
, &events
)) {
360 fd
= PyObject_AsFileDescriptor(o
);
361 if (fd
== -1) return NULL
;
363 /* Add entry to the internal dictionary: the key is the
364 file descriptor, and the value is the event mask. */
365 if ( (NULL
== (key
= PyInt_FromLong(fd
))) ||
366 (NULL
== (value
= PyInt_FromLong(events
))) ||
367 (PyDict_SetItem(self
->dict
, key
, value
)) == -1) {
370 self
->ufd_uptodate
= 0;
376 static char poll_unregister_doc
[] =
377 "unregister(fd) -> None\n\n\
378 Remove a file descriptor being tracked by the polling object.";
381 poll_unregister(pollObject
*self
, PyObject
*args
)
386 if (!PyArg_ParseTuple(args
, "O", &o
)) {
390 fd
= PyObject_AsFileDescriptor( o
);
394 /* Check whether the fd is already in the array */
395 key
= PyInt_FromLong(fd
);
399 if (PyDict_DelItem(self
->dict
, key
) == -1) {
401 /* This will simply raise the KeyError set by PyDict_DelItem
402 if the file descriptor isn't registered. */
407 self
->ufd_uptodate
= 0;
413 static char poll_poll_doc
[] =
414 "poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
415 Polls the set of registered file descriptors, returning a list containing \n\
416 any descriptors that have events or errors to report.";
419 poll_poll(pollObject
*self
, PyObject
*args
)
421 PyObject
*result_list
= NULL
, *tout
= NULL
;
422 int timeout
= 0, poll_result
, i
, j
;
423 PyObject
*value
= NULL
, *num
= NULL
;
425 if (!PyArg_ParseTuple(args
, "|O", &tout
)) {
429 /* Check values for timeout */
430 if (tout
== NULL
|| tout
== Py_None
)
432 else if (!PyArg_Parse(tout
, "i", &timeout
)) {
433 PyErr_SetString(PyExc_TypeError
,
434 "timeout must be an integer or None");
438 /* Ensure the ufd array is up to date */
439 if (!self
->ufd_uptodate
)
440 if (update_ufd_array(self
) == 0)
444 Py_BEGIN_ALLOW_THREADS
;
445 poll_result
= poll(self
->ufds
, self
->ufd_len
, timeout
);
446 Py_END_ALLOW_THREADS
;
448 if (poll_result
< 0) {
449 PyErr_SetFromErrno(SelectError
);
453 /* build the result list */
455 result_list
= PyList_New(poll_result
);
459 for (i
= 0, j
= 0; j
< poll_result
; j
++) {
460 /* skip to the next fired descriptor */
461 while (!self
->ufds
[i
].revents
) {
464 /* if we hit a NULL return, set value to NULL
465 and break out of loop; code at end will
466 clean up result_list */
467 value
= PyTuple_New(2);
470 num
= PyInt_FromLong(self
->ufds
[i
].fd
);
475 PyTuple_SET_ITEM(value
, 0, num
);
477 num
= PyInt_FromLong(self
->ufds
[i
].revents
);
482 PyTuple_SET_ITEM(value
, 1, num
);
483 if ((PyList_SetItem(result_list
, j
, value
)) == -1) {
493 Py_DECREF(result_list
);
497 static PyMethodDef poll_methods
[] = {
498 {"register", (PyCFunction
)poll_register
,
499 METH_VARARGS
, poll_register_doc
},
500 {"unregister", (PyCFunction
)poll_unregister
,
501 METH_VARARGS
, poll_unregister_doc
},
502 {"poll", (PyCFunction
)poll_poll
,
503 METH_VARARGS
, poll_poll_doc
},
504 {NULL
, NULL
} /* sentinel */
511 self
= PyObject_New(pollObject
, &poll_Type
);
514 /* ufd_uptodate is a Boolean, denoting whether the
515 array pointed to by ufds matches the contents of the dictionary. */
516 self
->ufd_uptodate
= 0;
518 self
->dict
= PyDict_New();
519 if (self
->dict
== NULL
) {
527 poll_dealloc(pollObject
*self
)
529 if (self
->ufds
!= NULL
)
530 PyMem_DEL(self
->ufds
);
531 Py_XDECREF(self
->dict
);
536 poll_getattr(pollObject
*self
, char *name
)
538 return Py_FindMethod(poll_methods
, (PyObject
*)self
, name
);
541 statichere PyTypeObject poll_Type
= {
542 /* The ob_type field must be initialized in the module init function
543 * to be portable to Windows without using C++. */
544 PyObject_HEAD_INIT(NULL
)
547 sizeof(pollObject
), /*tp_basicsize*/
550 (destructor
)poll_dealloc
, /*tp_dealloc*/
552 (getattrfunc
)poll_getattr
, /*tp_getattr*/
557 0, /*tp_as_sequence*/
562 static char poll_doc
[] =
563 "Returns a polling object, which supports registering and\n\
564 unregistering file descriptors, and then polling them for I/O events.";
567 select_poll(PyObject
*self
, PyObject
*args
)
571 if (!PyArg_ParseTuple(args
, ":poll"))
573 rv
= newPollObject();
576 return (PyObject
*)rv
;
578 #endif /* HAVE_POLL */
580 static char select_doc
[] =
581 "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
583 Wait until one or more file descriptors are ready for some kind of I/O.\n\
584 The first three arguments are lists of file descriptors to be waited for:\n\
585 rlist -- wait until ready for reading\n\
586 wlist -- wait until ready for writing\n\
587 xlist -- wait for an ``exceptional condition''\n\
588 If only one kind of condition is required, pass [] for the other lists.\n\
589 A file descriptor is either a socket or file object, or a small integer\n\
590 gotten from a fileno() method call on one of those.\n\
592 The optional 4th argument specifies a timeout in seconds; it may be\n\
593 a floating point number to specify fractions of seconds. If it is absent\n\
594 or None, the call will never time out.\n\
596 The return value is a tuple of three lists corresponding to the first three\n\
597 arguments; each contains the subset of the corresponding file descriptors\n\
600 *** IMPORTANT NOTICE ***\n\
601 On Windows, only sockets are supported; on Unix, all file descriptors.";
603 static PyMethodDef select_methods
[] = {
604 {"select", select_select
, METH_VARARGS
, select_doc
},
606 {"poll", select_poll
, METH_VARARGS
, poll_doc
},
607 #endif /* HAVE_POLL */
608 {0, 0}, /* sentinel */
611 static char module_doc
[] =
612 "This module supports asynchronous I/O on multiple file descriptors.\n\
614 *** IMPORTANT NOTICE ***\n\
615 On Windows, only sockets are supported; on Unix, all file descriptors.";
618 * Convenience routine to export an integer value.
619 * For simplicity, errors (which are unlikely anyway) are ignored.
623 insint(PyObject
*d
, char *name
, int value
)
625 PyObject
*v
= PyInt_FromLong((long) value
);
627 /* Don't bother reporting this error */
631 PyDict_SetItemString(d
, name
, v
);
640 m
= Py_InitModule3("select", select_methods
, module_doc
);
641 d
= PyModule_GetDict(m
);
642 SelectError
= PyErr_NewException("select.error", NULL
, NULL
);
643 PyDict_SetItemString(d
, "error", SelectError
);
645 poll_Type
.ob_type
= &PyType_Type
;
646 insint(d
, "POLLIN", POLLIN
);
647 insint(d
, "POLLPRI", POLLPRI
);
648 insint(d
, "POLLOUT", POLLOUT
);
649 insint(d
, "POLLERR", POLLERR
);
650 insint(d
, "POLLHUP", POLLHUP
);
651 insint(d
, "POLLNVAL", POLLNVAL
);
654 insint(d
, "POLLRDNORM", POLLRDNORM
);
657 insint(d
, "POLLRDBAND", POLLRDBAND
);
660 insint(d
, "POLLWRNORM", POLLWRNORM
);
663 insint(d
, "POLLWRBAND", POLLWRBAND
);
666 insint(d
, "POLLMSG", POLLMSG
);
668 #endif /* HAVE_POLL */