Saved and restored logging._handlerList at the same time as saving/restoring logging...
[python.git] / Modules / selectmodule.c
blob53c68c1a55983959c6019098ce132537153a6002
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
6 >= 0.
7 */
9 #include "Python.h"
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).
13 Here we boost it.
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
19 #endif
21 #if defined(HAVE_POLL_H)
22 #include <poll.h>
23 #elif defined(HAVE_SYS_POLL_H)
24 #include <sys/poll.h>
25 #endif
27 #ifdef __sgi
28 /* This is missing from unistd.h */
29 extern void bzero(void *, int);
30 #endif
32 #ifndef DONT_HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
36 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
37 #include <sys/time.h>
38 #include <utils.h>
39 #endif
41 #ifdef MS_WINDOWS
42 #include <winsock.h>
43 #else
44 #ifdef __BEOS__
45 #include <net/socket.h>
46 #define SOCKET int
47 #else
48 #define SOCKET int
49 #endif
50 #endif
53 static PyObject *SelectError;
55 /* list of Python objects and their file descriptor */
56 typedef struct {
57 PyObject *obj; /* owned reference */
58 SOCKET fd;
59 int sentinel; /* -1 == sentinel */
60 } pylist;
62 static void
63 reap_obj(pylist fd2obj[FD_SETSIZE + 1])
65 int i;
66 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
67 Py_XDECREF(fd2obj[i].obj);
68 fd2obj[i].obj = NULL;
70 fd2obj[0].sentinel = -1;
74 /* returns -1 and sets the Python exception if an error occurred, otherwise
75 returns a number >= 0
77 static int
78 seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
80 int i;
81 int max = -1;
82 int index = 0;
83 int len = -1;
84 PyObject* fast_seq = NULL;
85 PyObject* o = NULL;
87 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
88 FD_ZERO(set);
90 fast_seq=PySequence_Fast(seq, "arguments 1-3 must be sequences");
91 if (!fast_seq)
92 return -1;
94 len = PySequence_Fast_GET_SIZE(fast_seq);
96 for (i = 0; i < len; i++) {
97 SOCKET v;
99 /* any intervening fileno() calls could decr this refcnt */
100 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
101 return -1;
103 Py_INCREF(o);
104 v = PyObject_AsFileDescriptor( o );
105 if (v == -1) goto finally;
107 #if defined(_MSC_VER)
108 max = 0; /* not used for Win32 */
109 #else /* !_MSC_VER */
110 if (v < 0 || v >= FD_SETSIZE) {
111 PyErr_SetString(PyExc_ValueError,
112 "filedescriptor out of range in select()");
113 goto finally;
115 if (v > max)
116 max = v;
117 #endif /* _MSC_VER */
118 FD_SET(v, set);
120 /* add object and its file descriptor to the list */
121 if (index >= FD_SETSIZE) {
122 PyErr_SetString(PyExc_ValueError,
123 "too many file descriptors in select()");
124 goto finally;
126 fd2obj[index].obj = o;
127 fd2obj[index].fd = v;
128 fd2obj[index].sentinel = 0;
129 fd2obj[++index].sentinel = -1;
131 Py_DECREF(fast_seq);
132 return max+1;
134 finally:
135 Py_XDECREF(o);
136 Py_DECREF(fast_seq);
137 return -1;
140 /* returns NULL and sets the Python exception if an error occurred */
141 static PyObject *
142 set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
144 int i, j, count=0;
145 PyObject *list, *o;
146 SOCKET fd;
148 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
149 if (FD_ISSET(fd2obj[j].fd, set))
150 count++;
152 list = PyList_New(count);
153 if (!list)
154 return NULL;
156 i = 0;
157 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
158 fd = fd2obj[j].fd;
159 if (FD_ISSET(fd, set)) {
160 #ifndef _MSC_VER
161 if (fd > FD_SETSIZE) {
162 PyErr_SetString(PyExc_SystemError,
163 "filedescriptor out of range returned in select()");
164 goto finally;
166 #endif
167 o = fd2obj[j].obj;
168 fd2obj[j].obj = NULL;
169 /* transfer ownership */
170 if (PyList_SetItem(list, i, o) < 0)
171 goto finally;
173 i++;
176 return list;
177 finally:
178 Py_DECREF(list);
179 return NULL;
182 #undef SELECT_USES_HEAP
183 #if FD_SETSIZE > 1024
184 #define SELECT_USES_HEAP
185 #endif /* FD_SETSIZE > 1024 */
187 static PyObject *
188 select_select(PyObject *self, PyObject *args)
190 #ifdef SELECT_USES_HEAP
191 pylist *rfd2obj, *wfd2obj, *efd2obj;
192 #else /* !SELECT_USES_HEAP */
193 /* XXX: All this should probably be implemented as follows:
194 * - find the highest descriptor we're interested in
195 * - add one
196 * - that's the size
197 * See: Stevens, APitUE, $12.5.1
199 pylist rfd2obj[FD_SETSIZE + 1];
200 pylist wfd2obj[FD_SETSIZE + 1];
201 pylist efd2obj[FD_SETSIZE + 1];
202 #endif /* SELECT_USES_HEAP */
203 PyObject *ifdlist, *ofdlist, *efdlist;
204 PyObject *ret = NULL;
205 PyObject *tout = Py_None;
206 fd_set ifdset, ofdset, efdset;
207 double timeout;
208 struct timeval tv, *tvp;
209 long seconds;
210 int imax, omax, emax, max;
211 int n;
213 /* convert arguments */
214 if (!PyArg_ParseTuple(args, "OOO|O:select",
215 &ifdlist, &ofdlist, &efdlist, &tout))
216 return NULL;
218 if (tout == Py_None)
219 tvp = (struct timeval *)0;
220 else if (!PyNumber_Check(tout)) {
221 PyErr_SetString(PyExc_TypeError,
222 "timeout must be a float or None");
223 return NULL;
225 else {
226 timeout = PyFloat_AsDouble(tout);
227 if (timeout == -1 && PyErr_Occurred())
228 return NULL;
229 if (timeout > (double)LONG_MAX) {
230 PyErr_SetString(PyExc_OverflowError,
231 "timeout period too long");
232 return NULL;
234 seconds = (long)timeout;
235 timeout = timeout - (double)seconds;
236 tv.tv_sec = seconds;
237 tv.tv_usec = (long)(timeout*1000000.0);
238 tvp = &tv;
242 #ifdef SELECT_USES_HEAP
243 /* Allocate memory for the lists */
244 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
245 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
246 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
247 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
248 if (rfd2obj) PyMem_DEL(rfd2obj);
249 if (wfd2obj) PyMem_DEL(wfd2obj);
250 if (efd2obj) PyMem_DEL(efd2obj);
251 return PyErr_NoMemory();
253 #endif /* SELECT_USES_HEAP */
254 /* Convert sequences to fd_sets, and get maximum fd number
255 * propagates the Python exception set in seq2set()
257 rfd2obj[0].sentinel = -1;
258 wfd2obj[0].sentinel = -1;
259 efd2obj[0].sentinel = -1;
260 if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
261 goto finally;
262 if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
263 goto finally;
264 if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
265 goto finally;
266 max = imax;
267 if (omax > max) max = omax;
268 if (emax > max) max = emax;
270 Py_BEGIN_ALLOW_THREADS
271 n = select(max, &ifdset, &ofdset, &efdset, tvp);
272 Py_END_ALLOW_THREADS
274 #ifdef MS_WINDOWS
275 if (n == SOCKET_ERROR) {
276 PyErr_SetExcFromWindowsErr(SelectError, WSAGetLastError());
278 #else
279 if (n < 0) {
280 PyErr_SetFromErrno(SelectError);
282 #endif
283 else if (n == 0) {
284 /* optimization */
285 ifdlist = PyList_New(0);
286 if (ifdlist) {
287 ret = PyTuple_Pack(3, ifdlist, ifdlist, ifdlist);
288 Py_DECREF(ifdlist);
291 else {
292 /* any of these three calls can raise an exception. it's more
293 convenient to test for this after all three calls... but
294 is that acceptable?
296 ifdlist = set2list(&ifdset, rfd2obj);
297 ofdlist = set2list(&ofdset, wfd2obj);
298 efdlist = set2list(&efdset, efd2obj);
299 if (PyErr_Occurred())
300 ret = NULL;
301 else
302 ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
304 Py_DECREF(ifdlist);
305 Py_DECREF(ofdlist);
306 Py_DECREF(efdlist);
309 finally:
310 reap_obj(rfd2obj);
311 reap_obj(wfd2obj);
312 reap_obj(efd2obj);
313 #ifdef SELECT_USES_HEAP
314 PyMem_DEL(rfd2obj);
315 PyMem_DEL(wfd2obj);
316 PyMem_DEL(efd2obj);
317 #endif /* SELECT_USES_HEAP */
318 return ret;
321 #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
323 * poll() support
326 typedef struct {
327 PyObject_HEAD
328 PyObject *dict;
329 int ufd_uptodate;
330 int ufd_len;
331 struct pollfd *ufds;
332 } pollObject;
334 static PyTypeObject poll_Type;
336 /* Update the malloc'ed array of pollfds to match the dictionary
337 contained within a pollObject. Return 1 on success, 0 on an error.
340 static int
341 update_ufd_array(pollObject *self)
343 int i, pos;
344 PyObject *key, *value;
346 self->ufd_len = PyDict_Size(self->dict);
347 PyMem_Resize(self->ufds, struct pollfd, self->ufd_len);
348 if (self->ufds == NULL) {
349 PyErr_NoMemory();
350 return 0;
353 i = pos = 0;
354 while (PyDict_Next(self->dict, &pos, &key, &value)) {
355 self->ufds[i].fd = PyInt_AsLong(key);
356 self->ufds[i].events = (short)PyInt_AsLong(value);
357 i++;
359 self->ufd_uptodate = 1;
360 return 1;
363 PyDoc_STRVAR(poll_register_doc,
364 "register(fd [, eventmask] ) -> None\n\n\
365 Register a file descriptor with the polling object.\n\
366 fd -- either an integer, or an object with a fileno() method returning an\n\
367 int.\n\
368 events -- an optional bitmask describing the type of events to check for");
370 static PyObject *
371 poll_register(pollObject *self, PyObject *args)
373 PyObject *o, *key, *value;
374 int fd, events = POLLIN | POLLPRI | POLLOUT;
375 int err;
377 if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
378 return NULL;
381 fd = PyObject_AsFileDescriptor(o);
382 if (fd == -1) return NULL;
384 /* Add entry to the internal dictionary: the key is the
385 file descriptor, and the value is the event mask. */
386 key = PyInt_FromLong(fd);
387 if (key == NULL)
388 return NULL;
389 value = PyInt_FromLong(events);
390 if (value == NULL) {
391 Py_DECREF(key);
392 return NULL;
394 err = PyDict_SetItem(self->dict, key, value);
395 Py_DECREF(key);
396 Py_DECREF(value);
397 if (err < 0)
398 return NULL;
400 self->ufd_uptodate = 0;
402 Py_INCREF(Py_None);
403 return Py_None;
406 PyDoc_STRVAR(poll_unregister_doc,
407 "unregister(fd) -> None\n\n\
408 Remove a file descriptor being tracked by the polling object.");
410 static PyObject *
411 poll_unregister(pollObject *self, PyObject *args)
413 PyObject *o, *key;
414 int fd;
416 if (!PyArg_ParseTuple(args, "O:unregister", &o)) {
417 return NULL;
420 fd = PyObject_AsFileDescriptor( o );
421 if (fd == -1)
422 return NULL;
424 /* Check whether the fd is already in the array */
425 key = PyInt_FromLong(fd);
426 if (key == NULL)
427 return NULL;
429 if (PyDict_DelItem(self->dict, key) == -1) {
430 Py_DECREF(key);
431 /* This will simply raise the KeyError set by PyDict_DelItem
432 if the file descriptor isn't registered. */
433 return NULL;
436 Py_DECREF(key);
437 self->ufd_uptodate = 0;
439 Py_INCREF(Py_None);
440 return Py_None;
443 PyDoc_STRVAR(poll_poll_doc,
444 "poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
445 Polls the set of registered file descriptors, returning a list containing \n\
446 any descriptors that have events or errors to report.");
448 static PyObject *
449 poll_poll(pollObject *self, PyObject *args)
451 PyObject *result_list = NULL, *tout = NULL;
452 int timeout = 0, poll_result, i, j;
453 PyObject *value = NULL, *num = NULL;
455 if (!PyArg_ParseTuple(args, "|O:poll", &tout)) {
456 return NULL;
459 /* Check values for timeout */
460 if (tout == NULL || tout == Py_None)
461 timeout = -1;
462 else if (!PyNumber_Check(tout)) {
463 PyErr_SetString(PyExc_TypeError,
464 "timeout must be an integer or None");
465 return NULL;
467 else {
468 tout = PyNumber_Int(tout);
469 if (!tout)
470 return NULL;
471 timeout = PyInt_AsLong(tout);
472 Py_DECREF(tout);
473 if (timeout == -1 && PyErr_Occurred())
474 return NULL;
477 /* Ensure the ufd array is up to date */
478 if (!self->ufd_uptodate)
479 if (update_ufd_array(self) == 0)
480 return NULL;
482 /* call poll() */
483 Py_BEGIN_ALLOW_THREADS;
484 poll_result = poll(self->ufds, self->ufd_len, timeout);
485 Py_END_ALLOW_THREADS;
487 if (poll_result < 0) {
488 PyErr_SetFromErrno(SelectError);
489 return NULL;
492 /* build the result list */
494 result_list = PyList_New(poll_result);
495 if (!result_list)
496 return NULL;
497 else {
498 for (i = 0, j = 0; j < poll_result; j++) {
499 /* skip to the next fired descriptor */
500 while (!self->ufds[i].revents) {
501 i++;
503 /* if we hit a NULL return, set value to NULL
504 and break out of loop; code at end will
505 clean up result_list */
506 value = PyTuple_New(2);
507 if (value == NULL)
508 goto error;
509 num = PyInt_FromLong(self->ufds[i].fd);
510 if (num == NULL) {
511 Py_DECREF(value);
512 goto error;
514 PyTuple_SET_ITEM(value, 0, num);
516 /* The &0xffff is a workaround for AIX. 'revents'
517 is a 16-bit short, and IBM assigned POLLNVAL
518 to be 0x8000, so the conversion to int results
519 in a negative number. See SF bug #923315. */
520 num = PyInt_FromLong(self->ufds[i].revents & 0xffff);
521 if (num == NULL) {
522 Py_DECREF(value);
523 goto error;
525 PyTuple_SET_ITEM(value, 1, num);
526 if ((PyList_SetItem(result_list, j, value)) == -1) {
527 Py_DECREF(value);
528 goto error;
530 i++;
533 return result_list;
535 error:
536 Py_DECREF(result_list);
537 return NULL;
540 static PyMethodDef poll_methods[] = {
541 {"register", (PyCFunction)poll_register,
542 METH_VARARGS, poll_register_doc},
543 {"unregister", (PyCFunction)poll_unregister,
544 METH_VARARGS, poll_unregister_doc},
545 {"poll", (PyCFunction)poll_poll,
546 METH_VARARGS, poll_poll_doc},
547 {NULL, NULL} /* sentinel */
550 static pollObject *
551 newPollObject(void)
553 pollObject *self;
554 self = PyObject_New(pollObject, &poll_Type);
555 if (self == NULL)
556 return NULL;
557 /* ufd_uptodate is a Boolean, denoting whether the
558 array pointed to by ufds matches the contents of the dictionary. */
559 self->ufd_uptodate = 0;
560 self->ufds = NULL;
561 self->dict = PyDict_New();
562 if (self->dict == NULL) {
563 Py_DECREF(self);
564 return NULL;
566 return self;
569 static void
570 poll_dealloc(pollObject *self)
572 if (self->ufds != NULL)
573 PyMem_DEL(self->ufds);
574 Py_XDECREF(self->dict);
575 PyObject_Del(self);
578 static PyObject *
579 poll_getattr(pollObject *self, char *name)
581 return Py_FindMethod(poll_methods, (PyObject *)self, name);
584 static PyTypeObject poll_Type = {
585 /* The ob_type field must be initialized in the module init function
586 * to be portable to Windows without using C++. */
587 PyObject_HEAD_INIT(NULL)
588 0, /*ob_size*/
589 "select.poll", /*tp_name*/
590 sizeof(pollObject), /*tp_basicsize*/
591 0, /*tp_itemsize*/
592 /* methods */
593 (destructor)poll_dealloc, /*tp_dealloc*/
594 0, /*tp_print*/
595 (getattrfunc)poll_getattr, /*tp_getattr*/
596 0, /*tp_setattr*/
597 0, /*tp_compare*/
598 0, /*tp_repr*/
599 0, /*tp_as_number*/
600 0, /*tp_as_sequence*/
601 0, /*tp_as_mapping*/
602 0, /*tp_hash*/
605 PyDoc_STRVAR(poll_doc,
606 "Returns a polling object, which supports registering and\n\
607 unregistering file descriptors, and then polling them for I/O events.");
609 static PyObject *
610 select_poll(PyObject *self, PyObject *args)
612 pollObject *rv;
614 if (!PyArg_ParseTuple(args, ":poll"))
615 return NULL;
616 rv = newPollObject();
617 if ( rv == NULL )
618 return NULL;
619 return (PyObject *)rv;
621 #endif /* HAVE_POLL && !HAVE_BROKEN_POLL */
623 PyDoc_STRVAR(select_doc,
624 "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
626 Wait until one or more file descriptors are ready for some kind of I/O.\n\
627 The first three arguments are sequences of file descriptors to be waited for:\n\
628 rlist -- wait until ready for reading\n\
629 wlist -- wait until ready for writing\n\
630 xlist -- wait for an ``exceptional condition''\n\
631 If only one kind of condition is required, pass [] for the other lists.\n\
632 A file descriptor is either a socket or file object, or a small integer\n\
633 gotten from a fileno() method call on one of those.\n\
635 The optional 4th argument specifies a timeout in seconds; it may be\n\
636 a floating point number to specify fractions of seconds. If it is absent\n\
637 or None, the call will never time out.\n\
639 The return value is a tuple of three lists corresponding to the first three\n\
640 arguments; each contains the subset of the corresponding file descriptors\n\
641 that are ready.\n\
643 *** IMPORTANT NOTICE ***\n\
644 On Windows, only sockets are supported; on Unix, all file descriptors.");
646 static PyMethodDef select_methods[] = {
647 {"select", select_select, METH_VARARGS, select_doc},
648 #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
649 {"poll", select_poll, METH_VARARGS, poll_doc},
650 #endif /* HAVE_POLL && !HAVE_BROKEN_POLL */
651 {0, 0}, /* sentinel */
654 PyDoc_STRVAR(module_doc,
655 "This module supports asynchronous I/O on multiple file descriptors.\n\
657 *** IMPORTANT NOTICE ***\n\
658 On Windows, only sockets are supported; on Unix, all file descriptors.");
660 PyMODINIT_FUNC
661 initselect(void)
663 PyObject *m;
664 m = Py_InitModule3("select", select_methods, module_doc);
665 if (m == NULL)
666 return;
668 SelectError = PyErr_NewException("select.error", NULL, NULL);
669 Py_INCREF(SelectError);
670 PyModule_AddObject(m, "error", SelectError);
671 #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
672 poll_Type.ob_type = &PyType_Type;
673 PyModule_AddIntConstant(m, "POLLIN", POLLIN);
674 PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
675 PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
676 PyModule_AddIntConstant(m, "POLLERR", POLLERR);
677 PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
678 PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
680 #ifdef POLLRDNORM
681 PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
682 #endif
683 #ifdef POLLRDBAND
684 PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
685 #endif
686 #ifdef POLLWRNORM
687 PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
688 #endif
689 #ifdef POLLWRBAND
690 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
691 #endif
692 #ifdef POLLMSG
693 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
694 #endif
695 #endif /* HAVE_POLL && !HAVE_BROKEN_POLL */