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
24 #if defined(HAVE_POLL_H)
26 #elif defined(HAVE_SYS_POLL_H)
31 /* This is missing from unistd.h */
32 extern void bzero(void *, int);
35 #ifndef DONT_HAVE_SYS_TYPES_H
36 #include <sys/types.h>
48 #include <net/socket.h>
64 static PyObject
*SelectError
;
66 /* list of Python objects and their file descriptor */
68 PyObject
*obj
; /* owned reference */
70 int sentinel
; /* -1 == sentinel */
74 reap_obj(pylist fd2obj
[FD_SETSIZE
+ 1])
77 for (i
= 0; i
< FD_SETSIZE
+ 1 && fd2obj
[i
].sentinel
>= 0; i
++) {
78 Py_XDECREF(fd2obj
[i
].obj
);
81 fd2obj
[0].sentinel
= -1;
85 /* returns -1 and sets the Python exception if an error occurred, otherwise
89 list2set(PyObject
*list
, fd_set
*set
, pylist fd2obj
[FD_SETSIZE
+ 1])
94 int len
= PyList_Size(list
);
97 fd2obj
[0].obj
= (PyObject
*)0; /* set list to zero size */
100 for (i
= 0; i
< len
; i
++) {
103 /* any intervening fileno() calls could decr this refcnt */
104 if (!(o
= PyList_GetItem(list
, i
)))
108 v
= PyObject_AsFileDescriptor( o
);
109 if (v
== -1) goto finally
;
111 #if defined(_MSC_VER)
112 max
= 0; /* not used for Win32 */
113 #else /* !_MSC_VER */
114 if (v
< 0 || v
>= FD_SETSIZE
) {
115 PyErr_SetString(PyExc_ValueError
,
116 "filedescriptor out of range in select()");
121 #endif /* _MSC_VER */
124 /* add object and its file descriptor to the list */
125 if (index
>= FD_SETSIZE
) {
126 PyErr_SetString(PyExc_ValueError
,
127 "too many file descriptors in select()");
130 fd2obj
[index
].obj
= o
;
131 fd2obj
[index
].fd
= v
;
132 fd2obj
[index
].sentinel
= 0;
133 fd2obj
[++index
].sentinel
= -1;
142 /* returns NULL and sets the Python exception if an error occurred */
144 set2list(fd_set
*set
, pylist fd2obj
[FD_SETSIZE
+ 1])
150 for (j
= 0; fd2obj
[j
].sentinel
>= 0; j
++) {
151 if (FD_ISSET(fd2obj
[j
].fd
, set
))
154 list
= PyList_New(count
);
159 for (j
= 0; fd2obj
[j
].sentinel
>= 0; j
++) {
161 if (FD_ISSET(fd
, set
)) {
163 if (fd
> FD_SETSIZE
) {
164 PyErr_SetString(PyExc_SystemError
,
165 "filedescriptor out of range returned in select()");
170 fd2obj
[j
].obj
= NULL
;
171 /* transfer ownership */
172 if (PyList_SetItem(list
, i
, o
) < 0)
184 #undef SELECT_USES_HEAP
185 #if FD_SETSIZE > 1024
186 #define SELECT_USES_HEAP
187 #endif /* FD_SETSIZE > 1024 */
190 select_select(PyObject
*self
, PyObject
*args
)
192 #ifdef SELECT_USES_HEAP
193 pylist
*rfd2obj
, *wfd2obj
, *efd2obj
;
194 #else /* !SELECT_USES_HEAP */
195 /* XXX: All this should probably be implemented as follows:
196 * - find the highest descriptor we're interested in
199 * See: Stevens, APitUE, $12.5.1
201 pylist rfd2obj
[FD_SETSIZE
+ 1];
202 pylist wfd2obj
[FD_SETSIZE
+ 1];
203 pylist efd2obj
[FD_SETSIZE
+ 1];
204 #endif /* SELECT_USES_HEAP */
205 PyObject
*ifdlist
, *ofdlist
, *efdlist
;
206 PyObject
*ret
= NULL
;
207 PyObject
*tout
= Py_None
;
208 fd_set ifdset
, ofdset
, efdset
;
210 struct timeval tv
, *tvp
;
212 int imax
, omax
, emax
, max
;
215 /* convert arguments */
216 if (!PyArg_ParseTuple(args
, "OOO|O:select",
217 &ifdlist
, &ofdlist
, &efdlist
, &tout
))
221 tvp
= (struct timeval
*)0;
222 else if (!PyArg_Parse(tout
, "d", &timeout
)) {
223 PyErr_SetString(PyExc_TypeError
,
224 "timeout must be a float or None");
228 if (timeout
> (double)LONG_MAX
) {
229 PyErr_SetString(PyExc_OverflowError
,
230 "timeout period too long");
233 seconds
= (long)timeout
;
234 timeout
= timeout
- (double)seconds
;
236 tv
.tv_usec
= (long)(timeout
*1000000.0);
240 /* sanity check first three arguments */
241 if (!PyList_Check(ifdlist
) ||
242 !PyList_Check(ofdlist
) ||
243 !PyList_Check(efdlist
))
245 PyErr_SetString(PyExc_TypeError
,
246 "arguments 1-3 must be lists");
250 #ifdef SELECT_USES_HEAP
251 /* Allocate memory for the lists */
252 rfd2obj
= PyMem_NEW(pylist
, FD_SETSIZE
+ 1);
253 wfd2obj
= PyMem_NEW(pylist
, FD_SETSIZE
+ 1);
254 efd2obj
= PyMem_NEW(pylist
, FD_SETSIZE
+ 1);
255 if (rfd2obj
== NULL
|| wfd2obj
== NULL
|| efd2obj
== NULL
) {
256 if (rfd2obj
) PyMem_DEL(rfd2obj
);
257 if (wfd2obj
) PyMem_DEL(wfd2obj
);
258 if (efd2obj
) PyMem_DEL(efd2obj
);
261 #endif /* SELECT_USES_HEAP */
262 /* Convert lists to fd_sets, and get maximum fd number
263 * propagates the Python exception set in list2set()
265 rfd2obj
[0].sentinel
= -1;
266 wfd2obj
[0].sentinel
= -1;
267 efd2obj
[0].sentinel
= -1;
268 if ((imax
=list2set(ifdlist
, &ifdset
, rfd2obj
)) < 0)
270 if ((omax
=list2set(ofdlist
, &ofdset
, wfd2obj
)) < 0)
272 if ((emax
=list2set(efdlist
, &efdset
, efd2obj
)) < 0)
275 if (omax
> max
) max
= omax
;
276 if (emax
> max
) max
= emax
;
278 Py_BEGIN_ALLOW_THREADS
279 n
= select(max
, &ifdset
, &ofdset
, &efdset
, tvp
);
283 PyErr_SetFromErrno(SelectError
);
287 ifdlist
= PyList_New(0);
289 ret
= Py_BuildValue("OOO", ifdlist
, ifdlist
, ifdlist
);
294 /* any of these three calls can raise an exception. it's more
295 convenient to test for this after all three calls... but
298 ifdlist
= set2list(&ifdset
, rfd2obj
);
299 ofdlist
= set2list(&ofdset
, wfd2obj
);
300 efdlist
= set2list(&efdset
, efd2obj
);
301 if (PyErr_Occurred())
304 ret
= Py_BuildValue("OOO", ifdlist
, ofdlist
, efdlist
);
315 #ifdef SELECT_USES_HEAP
319 #endif /* SELECT_USES_HEAP */
336 staticforward PyTypeObject poll_Type
;
338 /* Update the malloc'ed array of pollfds to match the dictionary
339 contained within a pollObject. Return 1 on success, 0 on an error.
343 update_ufd_array(pollObject
*self
)
346 PyObject
*key
, *value
;
348 self
->ufd_len
= PyDict_Size(self
->dict
);
349 PyMem_Resize(self
->ufds
, struct pollfd
, self
->ufd_len
);
350 if (self
->ufds
== NULL
) {
356 while (PyDict_Next(self
->dict
, &pos
, &key
, &value
)) {
357 self
->ufds
[i
].fd
= PyInt_AsLong(key
);
358 self
->ufds
[i
].events
= (short)PyInt_AsLong(value
);
361 self
->ufd_uptodate
= 1;
365 static char poll_register_doc
[] =
366 "register(fd [, eventmask] ) -> None\n\n\
367 Register a file descriptor with the polling object.\n\
368 fd -- either an integer, or an object with a fileno() method returning an\n\
370 events -- an optional bitmask describing the type of events to check for";
373 poll_register(pollObject
*self
, PyObject
*args
)
375 PyObject
*o
, *key
, *value
;
376 int fd
, events
= POLLIN
| POLLPRI
| POLLOUT
;
378 if (!PyArg_ParseTuple(args
, "O|i:register", &o
, &events
)) {
382 fd
= PyObject_AsFileDescriptor(o
);
383 if (fd
== -1) return NULL
;
385 /* Add entry to the internal dictionary: the key is the
386 file descriptor, and the value is the event mask. */
387 if ( (NULL
== (key
= PyInt_FromLong(fd
))) ||
388 (NULL
== (value
= PyInt_FromLong(events
))) ||
389 (PyDict_SetItem(self
->dict
, key
, value
)) == -1) {
392 self
->ufd_uptodate
= 0;
398 static char poll_unregister_doc
[] =
399 "unregister(fd) -> None\n\n\
400 Remove a file descriptor being tracked by the polling object.";
403 poll_unregister(pollObject
*self
, PyObject
*args
)
408 if (!PyArg_ParseTuple(args
, "O:unregister", &o
)) {
412 fd
= PyObject_AsFileDescriptor( o
);
416 /* Check whether the fd is already in the array */
417 key
= PyInt_FromLong(fd
);
421 if (PyDict_DelItem(self
->dict
, key
) == -1) {
423 /* This will simply raise the KeyError set by PyDict_DelItem
424 if the file descriptor isn't registered. */
429 self
->ufd_uptodate
= 0;
435 static char poll_poll_doc
[] =
436 "poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
437 Polls the set of registered file descriptors, returning a list containing \n\
438 any descriptors that have events or errors to report.";
441 poll_poll(pollObject
*self
, PyObject
*args
)
443 PyObject
*result_list
= NULL
, *tout
= NULL
;
444 int timeout
= 0, poll_result
, i
, j
;
445 PyObject
*value
= NULL
, *num
= NULL
;
447 if (!PyArg_ParseTuple(args
, "|O:poll", &tout
)) {
451 /* Check values for timeout */
452 if (tout
== NULL
|| tout
== Py_None
)
454 else if (!PyArg_Parse(tout
, "i", &timeout
)) {
455 PyErr_SetString(PyExc_TypeError
,
456 "timeout must be an integer or None");
460 /* Ensure the ufd array is up to date */
461 if (!self
->ufd_uptodate
)
462 if (update_ufd_array(self
) == 0)
466 Py_BEGIN_ALLOW_THREADS
;
467 poll_result
= poll(self
->ufds
, self
->ufd_len
, timeout
);
468 Py_END_ALLOW_THREADS
;
470 if (poll_result
< 0) {
471 PyErr_SetFromErrno(SelectError
);
475 /* build the result list */
477 result_list
= PyList_New(poll_result
);
481 for (i
= 0, j
= 0; j
< poll_result
; j
++) {
482 /* skip to the next fired descriptor */
483 while (!self
->ufds
[i
].revents
) {
486 /* if we hit a NULL return, set value to NULL
487 and break out of loop; code at end will
488 clean up result_list */
489 value
= PyTuple_New(2);
492 num
= PyInt_FromLong(self
->ufds
[i
].fd
);
497 PyTuple_SET_ITEM(value
, 0, num
);
499 num
= PyInt_FromLong(self
->ufds
[i
].revents
);
504 PyTuple_SET_ITEM(value
, 1, num
);
505 if ((PyList_SetItem(result_list
, j
, value
)) == -1) {
515 Py_DECREF(result_list
);
519 static PyMethodDef poll_methods
[] = {
520 {"register", (PyCFunction
)poll_register
,
521 METH_VARARGS
, poll_register_doc
},
522 {"unregister", (PyCFunction
)poll_unregister
,
523 METH_VARARGS
, poll_unregister_doc
},
524 {"poll", (PyCFunction
)poll_poll
,
525 METH_VARARGS
, poll_poll_doc
},
526 {NULL
, NULL
} /* sentinel */
533 self
= PyObject_New(pollObject
, &poll_Type
);
536 /* ufd_uptodate is a Boolean, denoting whether the
537 array pointed to by ufds matches the contents of the dictionary. */
538 self
->ufd_uptodate
= 0;
540 self
->dict
= PyDict_New();
541 if (self
->dict
== NULL
) {
549 poll_dealloc(pollObject
*self
)
551 if (self
->ufds
!= NULL
)
552 PyMem_DEL(self
->ufds
);
553 Py_XDECREF(self
->dict
);
558 poll_getattr(pollObject
*self
, char *name
)
560 return Py_FindMethod(poll_methods
, (PyObject
*)self
, name
);
563 statichere PyTypeObject poll_Type
= {
564 /* The ob_type field must be initialized in the module init function
565 * to be portable to Windows without using C++. */
566 PyObject_HEAD_INIT(NULL
)
569 sizeof(pollObject
), /*tp_basicsize*/
572 (destructor
)poll_dealloc
, /*tp_dealloc*/
574 (getattrfunc
)poll_getattr
, /*tp_getattr*/
579 0, /*tp_as_sequence*/
584 static char poll_doc
[] =
585 "Returns a polling object, which supports registering and\n\
586 unregistering file descriptors, and then polling them for I/O events.";
589 select_poll(PyObject
*self
, PyObject
*args
)
593 if (!PyArg_ParseTuple(args
, ":poll"))
595 rv
= newPollObject();
598 return (PyObject
*)rv
;
600 #endif /* HAVE_POLL */
602 static char select_doc
[] =
603 "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
605 Wait until one or more file descriptors are ready for some kind of I/O.\n\
606 The first three arguments are lists of file descriptors to be waited for:\n\
607 rlist -- wait until ready for reading\n\
608 wlist -- wait until ready for writing\n\
609 xlist -- wait for an ``exceptional condition''\n\
610 If only one kind of condition is required, pass [] for the other lists.\n\
611 A file descriptor is either a socket or file object, or a small integer\n\
612 gotten from a fileno() method call on one of those.\n\
614 The optional 4th argument specifies a timeout in seconds; it may be\n\
615 a floating point number to specify fractions of seconds. If it is absent\n\
616 or None, the call will never time out.\n\
618 The return value is a tuple of three lists corresponding to the first three\n\
619 arguments; each contains the subset of the corresponding file descriptors\n\
622 *** IMPORTANT NOTICE ***\n\
623 On Windows, only sockets are supported; on Unix, all file descriptors.";
625 static PyMethodDef select_methods
[] = {
626 {"select", select_select
, METH_VARARGS
, select_doc
},
628 {"poll", select_poll
, METH_VARARGS
, poll_doc
},
629 #endif /* HAVE_POLL */
630 {0, 0}, /* sentinel */
633 static char module_doc
[] =
634 "This module supports asynchronous I/O on multiple file descriptors.\n\
636 *** IMPORTANT NOTICE ***\n\
637 On Windows, only sockets are supported; on Unix, all file descriptors.";
640 * Convenience routine to export an integer value.
641 * For simplicity, errors (which are unlikely anyway) are ignored.
645 insint(PyObject
*d
, char *name
, int value
)
647 PyObject
*v
= PyInt_FromLong((long) value
);
649 /* Don't bother reporting this error */
653 PyDict_SetItemString(d
, name
, v
);
662 m
= Py_InitModule3("select", select_methods
, module_doc
);
663 d
= PyModule_GetDict(m
);
664 SelectError
= PyErr_NewException("select.error", NULL
, NULL
);
665 PyDict_SetItemString(d
, "error", SelectError
);
667 poll_Type
.ob_type
= &PyType_Type
;
668 insint(d
, "POLLIN", POLLIN
);
669 insint(d
, "POLLPRI", POLLPRI
);
670 insint(d
, "POLLOUT", POLLOUT
);
671 insint(d
, "POLLERR", POLLERR
);
672 insint(d
, "POLLHUP", POLLHUP
);
673 insint(d
, "POLLNVAL", POLLNVAL
);
676 insint(d
, "POLLRDNORM", POLLRDNORM
);
679 insint(d
, "POLLRDBAND", POLLRDBAND
);
682 insint(d
, "POLLWRNORM", POLLWRNORM
);
685 insint(d
, "POLLWRBAND", POLLWRBAND
);
688 insint(d
, "POLLMSG", POLLMSG
);
690 #endif /* HAVE_POLL */