blob: b0139e8bce09f32d8134eccec96ea007384b138b [file] [log] [blame]
Guido van Rossum4f0fbf81996-06-12 04:22:53 +00001/* select - Module containing unix select(2) call.
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002 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.
Guido van Rossumbcc20741998-08-04 22:53:56 +00005 Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything
6 >= 0.
Guido van Rossum4f0fbf81996-06-12 04:22:53 +00007*/
Guido van Rossumed233a51992-06-23 09:07:03 +00008
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00009#include "Python.h"
Guido van Rossumed233a51992-06-23 09:07:03 +000010
Tim Petersd92dfe02000-12-12 01:18:41 +000011/* 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.
16*/
17#if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
18#define FD_SETSIZE 512
19#endif
20
Guido van Rossuma376cc51996-12-05 23:43:35 +000021#ifdef HAVE_UNISTD_H
22#include <unistd.h>
23#endif
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +000024#ifdef HAVE_POLL_H
25#include <poll.h>
26#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000027
Guido van Rossum37273171996-12-09 18:47:43 +000028#ifdef __sgi
29/* This is missing from unistd.h */
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000030extern void bzero(void *, int);
Guido van Rossum37273171996-12-09 18:47:43 +000031#endif
32
Guido van Rossumff7e83d1999-08-27 20:39:37 +000033#ifndef DONT_HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000034#include <sys/types.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +000035#endif
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000036
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000037#if defined(PYOS_OS2)
38#include <sys/time.h>
39#include <utils.h>
40#endif
41
Guido van Rossum6f489d91996-06-28 20:15:15 +000042#ifdef MS_WINDOWS
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000043#include <winsock.h>
44#else
Guido van Rossumbcc20741998-08-04 22:53:56 +000045#ifdef __BEOS__
46#include <net/socket.h>
47#define SOCKET int
48#else
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000049#define SOCKET int
50#endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000051#endif
Guido van Rossumed233a51992-06-23 09:07:03 +000052
Guido van Rossum1ca8bb32001-03-02 06:28:17 +000053#ifdef RISCOS
54#define NO_DUP
55#undef off_t
56#undef uid_t
57#undef gid_t
58#undef errno
59#include "socklib.h"
60#endif /* RISCOS */
61
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000062static PyObject *SelectError;
Guido van Rossumed233a51992-06-23 09:07:03 +000063
Barry Warsawc1cb3601996-12-12 22:16:21 +000064/* list of Python objects and their file descriptor */
65typedef struct {
66 PyObject *obj; /* owned reference */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000067 SOCKET fd;
Barry Warsawc1cb3601996-12-12 22:16:21 +000068 int sentinel; /* -1 == sentinel */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000069} pylist;
70
Barry Warsawc1cb3601996-12-12 22:16:21 +000071static void
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +000072reap_obj(pylist fd2obj[FD_SETSIZE + 3])
Barry Warsawc1cb3601996-12-12 22:16:21 +000073{
74 int i;
75 for (i = 0; i < FD_SETSIZE + 3 && fd2obj[i].sentinel >= 0; i++) {
76 Py_XDECREF(fd2obj[i].obj);
77 fd2obj[i].obj = NULL;
78 }
79 fd2obj[0].sentinel = -1;
80}
81
82
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000083/* returns -1 and sets the Python exception if an error occurred, otherwise
84 returns a number >= 0
85*/
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000086static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +000087list2set(PyObject *list, fd_set *set, pylist fd2obj[FD_SETSIZE + 3])
Guido van Rossumed233a51992-06-23 09:07:03 +000088{
Barry Warsawc1cb3601996-12-12 22:16:21 +000089 int i;
90 int max = -1;
91 int index = 0;
92 int len = PyList_Size(list);
93 PyObject* o = NULL;
Guido van Rossum07432c01995-03-29 16:47:45 +000094
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000095 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000096 FD_ZERO(set);
Barry Warsawc1cb3601996-12-12 22:16:21 +000097
98 for (i = 0; i < len; i++) {
Barry Warsawc1cb3601996-12-12 22:16:21 +000099 SOCKET v;
100
101 /* any intervening fileno() calls could decr this refcnt */
Barry Warsaw24c4b3d1996-12-13 23:22:42 +0000102 if (!(o = PyList_GetItem(list, i)))
Barry Warsaw529fcfe1996-12-16 18:15:34 +0000103 return -1;
Barry Warsaw24c4b3d1996-12-13 23:22:42 +0000104
Barry Warsawc1cb3601996-12-12 22:16:21 +0000105 Py_INCREF(o);
Andrew M. Kuchling9f28a032000-07-13 23:59:35 +0000106 v = PyObject_AsFileDescriptor( o );
107 if (v == -1) goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000108
Guido van Rossum947a0fa2000-01-14 16:33:09 +0000109#if defined(_MSC_VER)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000110 max = 0; /* not used for Win32 */
111#else /* !_MSC_VER */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000112 if (v < 0 || v >= FD_SETSIZE) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000113 PyErr_SetString(PyExc_ValueError,
114 "filedescriptor out of range in select()");
115 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000116 }
117 if (v > max)
118 max = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000119#endif /* _MSC_VER */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000120 FD_SET(v, set);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000121
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000122 /* add object and its file descriptor to the list */
123 if (index >= FD_SETSIZE) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000124 PyErr_SetString(PyExc_ValueError,
125 "too many file descriptors in select()");
126 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000127 }
128 fd2obj[index].obj = o;
129 fd2obj[index].fd = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000130 fd2obj[index].sentinel = 0;
131 fd2obj[++index].sentinel = -1;
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000132 }
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000133 return max+1;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000134
135 finally:
136 Py_XDECREF(o);
137 return -1;
Guido van Rossumed233a51992-06-23 09:07:03 +0000138}
139
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000140/* returns NULL and sets the Python exception if an error occurred */
141static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000142set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 3])
Guido van Rossumed233a51992-06-23 09:07:03 +0000143{
Barry Warsawc1cb3601996-12-12 22:16:21 +0000144 int i, j, count=0;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000145 PyObject *list, *o;
146 SOCKET fd;
Guido van Rossumed233a51992-06-23 09:07:03 +0000147
Barry Warsawc1cb3601996-12-12 22:16:21 +0000148 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000149 if (FD_ISSET(fd2obj[j].fd, set))
Barry Warsawc1cb3601996-12-12 22:16:21 +0000150 count++;
151 }
152 list = PyList_New(count);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000153 if (!list)
154 return NULL;
155
Barry Warsawc1cb3601996-12-12 22:16:21 +0000156 i = 0;
157 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000158 fd = fd2obj[j].fd;
159 if (FD_ISSET(fd, set)) {
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000160#ifndef _MSC_VER
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000161 if (fd > FD_SETSIZE) {
162 PyErr_SetString(PyExc_SystemError,
163 "filedescriptor out of range returned in select()");
Barry Warsawc1cb3601996-12-12 22:16:21 +0000164 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000165 }
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000166#endif
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000167 o = fd2obj[j].obj;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000168 fd2obj[j].obj = NULL;
169 /* transfer ownership */
170 if (PyList_SetItem(list, i, o) < 0)
171 goto finally;
172
173 i++;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000174 }
175 }
176 return list;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000177 finally:
178 Py_DECREF(list);
179 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000180}
Barry Warsawc1cb3601996-12-12 22:16:21 +0000181
Guido van Rossumed233a51992-06-23 09:07:03 +0000182
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000183static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000184select_select(PyObject *self, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000185{
Guido van Rossumd20781b1998-07-02 02:53:36 +0000186#ifdef MS_WINDOWS
187 /* This would be an awful lot of stack space on Windows! */
188 pylist *rfd2obj, *wfd2obj, *efd2obj;
189#else
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000190 pylist rfd2obj[FD_SETSIZE + 3];
191 pylist wfd2obj[FD_SETSIZE + 3];
192 pylist efd2obj[FD_SETSIZE + 3];
Guido van Rossumd20781b1998-07-02 02:53:36 +0000193#endif
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000194 PyObject *ifdlist, *ofdlist, *efdlist;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000195 PyObject *ret = NULL;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000196 PyObject *tout = Py_None;
197 fd_set ifdset, ofdset, efdset;
198 double timeout;
199 struct timeval tv, *tvp;
Guido van Rossum3262e162000-06-28 21:18:13 +0000200 long seconds;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000201 int imax, omax, emax, max;
202 int n;
Guido van Rossumed233a51992-06-23 09:07:03 +0000203
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000204 /* convert arguments */
Guido van Rossum43713e52000-02-29 13:59:29 +0000205 if (!PyArg_ParseTuple(args, "OOO|O:select",
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000206 &ifdlist, &ofdlist, &efdlist, &tout))
207 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000208
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000209 if (tout == Py_None)
210 tvp = (struct timeval *)0;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000211 else if (!PyArg_Parse(tout, "d", &timeout)) {
212 PyErr_SetString(PyExc_TypeError,
213 "timeout must be a float or None");
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000214 return NULL;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000215 }
Guido van Rossumc7a22701993-11-01 16:27:16 +0000216 else {
Guido van Rossum3262e162000-06-28 21:18:13 +0000217 if (timeout > (double)LONG_MAX) {
218 PyErr_SetString(PyExc_OverflowError, "timeout period too long");
219 return NULL;
220 }
221 seconds = (long)timeout;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000222 timeout = timeout - (double)seconds;
223 tv.tv_sec = seconds;
Guido van Rossum3262e162000-06-28 21:18:13 +0000224 tv.tv_usec = (long)(timeout*1000000.0);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000225 tvp = &tv;
Guido van Rossumc7a22701993-11-01 16:27:16 +0000226 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000227
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000228 /* sanity check first three arguments */
229 if (!PyList_Check(ifdlist) ||
230 !PyList_Check(ofdlist) ||
231 !PyList_Check(efdlist))
232 {
233 PyErr_SetString(PyExc_TypeError,
234 "arguments 1-3 must be lists");
235 return NULL;
236 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000237
Guido van Rossumd20781b1998-07-02 02:53:36 +0000238#ifdef MS_WINDOWS
239 /* Allocate memory for the lists */
240 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
241 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
242 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
243 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000244 if (rfd2obj) PyMem_DEL(rfd2obj);
245 if (wfd2obj) PyMem_DEL(wfd2obj);
246 if (efd2obj) PyMem_DEL(efd2obj);
Guido van Rossumd20781b1998-07-02 02:53:36 +0000247 return NULL;
248 }
249#endif
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000250 /* Convert lists to fd_sets, and get maximum fd number
251 * propagates the Python exception set in list2set()
252 */
Barry Warsawc1cb3601996-12-12 22:16:21 +0000253 rfd2obj[0].sentinel = -1;
254 wfd2obj[0].sentinel = -1;
255 efd2obj[0].sentinel = -1;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000256 if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000257 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000258 if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000259 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000260 if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000261 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000262 max = imax;
263 if (omax > max) max = omax;
264 if (emax > max) max = emax;
Guido van Rossumed233a51992-06-23 09:07:03 +0000265
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000266 Py_BEGIN_ALLOW_THREADS
267 n = select(max, &ifdset, &ofdset, &efdset, tvp);
268 Py_END_ALLOW_THREADS
Guido van Rossumed233a51992-06-23 09:07:03 +0000269
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000270 if (n < 0) {
271 PyErr_SetFromErrno(SelectError);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000272 }
Barry Warsawc1cb3601996-12-12 22:16:21 +0000273 else if (n == 0) {
274 /* optimization */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000275 ifdlist = PyList_New(0);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000276 if (ifdlist) {
277 ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist);
278 Py_DECREF(ifdlist);
279 }
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000280 }
Barry Warsawc1cb3601996-12-12 22:16:21 +0000281 else {
282 /* any of these three calls can raise an exception. it's more
283 convenient to test for this after all three calls... but
284 is that acceptable?
285 */
286 ifdlist = set2list(&ifdset, rfd2obj);
287 ofdlist = set2list(&ofdset, wfd2obj);
288 efdlist = set2list(&efdset, efd2obj);
289 if (PyErr_Occurred())
290 ret = NULL;
291 else
292 ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000293
Barry Warsawc1cb3601996-12-12 22:16:21 +0000294 Py_DECREF(ifdlist);
295 Py_DECREF(ofdlist);
296 Py_DECREF(efdlist);
297 }
298
299 finally:
300 reap_obj(rfd2obj);
301 reap_obj(wfd2obj);
302 reap_obj(efd2obj);
Guido van Rossumd20781b1998-07-02 02:53:36 +0000303#ifdef MS_WINDOWS
304 PyMem_DEL(rfd2obj);
305 PyMem_DEL(wfd2obj);
306 PyMem_DEL(efd2obj);
307#endif
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000308 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000309}
310
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000311#ifdef HAVE_POLL
312/*
313 * poll() support
314 */
315
316typedef struct {
317 PyObject_HEAD
318 PyObject *dict;
319 int ufd_uptodate;
320 int ufd_len;
321 struct pollfd *ufds;
322} pollObject;
323
324staticforward PyTypeObject poll_Type;
325
326/* Update the malloc'ed array of pollfds to match the dictionary
327 contained within a pollObject. Return 1 on success, 0 on an error.
328*/
329
330static int
331update_ufd_array(pollObject *self)
332{
333 int i, j, pos;
334 PyObject *key, *value;
335
336 self->ufd_len = PyDict_Size(self->dict);
337 PyMem_Resize(self->ufds, struct pollfd, self->ufd_len);
338 if (self->ufds == NULL) {
339 PyErr_NoMemory();
340 return 0;
341 }
342
343 i = pos = 0;
344 while ((j = PyDict_Next(self->dict, &pos, &key, &value))) {
345 self->ufds[i].fd = PyInt_AsLong(key);
346 self->ufds[i].events = PyInt_AsLong(value);
347 i++;
348 }
349 self->ufd_uptodate = 1;
350 return 1;
351}
352
353static char poll_register_doc[] =
354"register(fd [, eventmask] ) -> None\n\n\
355Register a file descriptor with the polling object.\n\
356fd -- either an integer, or an object with a fileno() method returning an int.\n\
357events -- an optional bitmask describing the type of events to check for";
358
359static PyObject *
360poll_register(pollObject *self, PyObject *args)
361{
362 PyObject *o, *key, *value;
363 int fd, events = POLLIN | POLLPRI | POLLOUT;
364
365 if (!PyArg_ParseTuple(args, "O|i", &o, &events)) {
366 return NULL;
367 }
368
369 fd = PyObject_AsFileDescriptor(o);
370 if (fd == -1) return NULL;
371
372 /* Add entry to the internal dictionary: the key is the
373 file descriptor, and the value is the event mask. */
374 if ( (NULL == (key = PyInt_FromLong(fd))) ||
375 (NULL == (value = PyInt_FromLong(events))) ||
376 (PyDict_SetItem(self->dict, key, value)) == -1) {
377 return NULL;
378 }
379 self->ufd_uptodate = 0;
380
381 Py_INCREF(Py_None);
382 return Py_None;
383}
384
385static char poll_unregister_doc[] =
386"unregister(fd) -> None\n\n\
387Remove a file descriptor being tracked by the polling object.";
388
389static PyObject *
390poll_unregister(pollObject *self, PyObject *args)
391{
392 PyObject *o, *key;
393 int fd;
394
395 if (!PyArg_ParseTuple(args, "O", &o)) {
396 return NULL;
397 }
398
399 fd = PyObject_AsFileDescriptor( o );
400 if (fd == -1)
401 return NULL;
402
403 /* Check whether the fd is already in the array */
404 key = PyInt_FromLong(fd);
405 if (key == NULL)
406 return NULL;
407
408 if (PyDict_DelItem(self->dict, key) == -1) {
409 Py_DECREF(key);
410 /* This will simply raise the KeyError set by PyDict_DelItem
411 if the file descriptor isn't registered. */
412 return NULL;
413 }
414
415 Py_DECREF(key);
416 self->ufd_uptodate = 0;
417
418 Py_INCREF(Py_None);
419 return Py_None;
420}
421
422static char poll_poll_doc[] =
423"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
424Polls the set of registered file descriptors, returning a list containing \n\
425any descriptors that have events or errors to report.";
426
427static PyObject *
428poll_poll(pollObject *self, PyObject *args)
429{
430 PyObject *result_list = NULL, *tout = NULL;
431 int timeout = 0, poll_result, i, j;
432 PyObject *value = NULL, *num = NULL;
433
434 if (!PyArg_ParseTuple(args, "|O", &tout)) {
435 return NULL;
436 }
437
438 /* Check values for timeout */
439 if (tout == NULL || tout == Py_None)
440 timeout = -1;
441 else if (!PyArg_Parse(tout, "i", &timeout)) {
442 PyErr_SetString(PyExc_TypeError,
443 "timeout must be an integer or None");
444 return NULL;
445 }
446
447 /* Ensure the ufd array is up to date */
448 if (!self->ufd_uptodate)
449 if (update_ufd_array(self) == 0)
450 return NULL;
451
452 /* call poll() */
453 Py_BEGIN_ALLOW_THREADS;
454 poll_result = poll(self->ufds, self->ufd_len, timeout);
455 Py_END_ALLOW_THREADS;
456
457 if (poll_result < 0) {
458 PyErr_SetFromErrno(SelectError);
459 return NULL;
460 }
461
462 /* build the result list */
463
464 result_list = PyList_New(poll_result);
465 if (!result_list)
466 return NULL;
467 else {
468 for (i = 0, j = 0; j < poll_result; j++) {
469 /* skip to the next fired descriptor */
470 while (!self->ufds[i].revents) {
471 i++;
472 }
473 /* if we hit a NULL return, set value to NULL
474 and break out of loop; code at end will
475 clean up result_list */
476 value = PyTuple_New(2);
477 if (value == NULL)
478 goto error;
479 num = PyInt_FromLong(self->ufds[i].fd);
480 if (num == NULL) {
481 Py_DECREF(value);
482 goto error;
483 }
484 PyTuple_SET_ITEM(value, 0, num);
485
486 num = PyInt_FromLong(self->ufds[i].revents);
487 if (num == NULL) {
488 Py_DECREF(value);
489 goto error;
490 }
491 PyTuple_SET_ITEM(value, 1, num);
492 if ((PyList_SetItem(result_list, j, value)) == -1) {
493 Py_DECREF(value);
494 goto error;
495 }
496 i++;
497 }
498 }
499 return result_list;
500
501 error:
502 Py_DECREF(result_list);
503 return NULL;
504}
505
506static PyMethodDef poll_methods[] = {
507 {"register", (PyCFunction)poll_register,
508 METH_VARARGS, poll_register_doc},
509 {"unregister", (PyCFunction)poll_unregister,
510 METH_VARARGS, poll_unregister_doc},
511 {"poll", (PyCFunction)poll_poll,
512 METH_VARARGS, poll_poll_doc},
513 {NULL, NULL} /* sentinel */
514};
515
516static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000517newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000518{
519 pollObject *self;
520 self = PyObject_New(pollObject, &poll_Type);
521 if (self == NULL)
522 return NULL;
523 /* ufd_uptodate is a Boolean, denoting whether the
524 array pointed to by ufds matches the contents of the dictionary. */
525 self->ufd_uptodate = 0;
526 self->ufds = NULL;
527 self->dict = PyDict_New();
528 if (self->dict == NULL) {
529 Py_DECREF(self);
530 return NULL;
531 }
532 return self;
533}
534
535static void
536poll_dealloc(pollObject *self)
537{
538 if (self->ufds != NULL)
539 PyMem_DEL(self->ufds);
540 Py_XDECREF(self->dict);
541 PyObject_Del(self);
542}
543
544static PyObject *
545poll_getattr(pollObject *self, char *name)
546{
547 return Py_FindMethod(poll_methods, (PyObject *)self, name);
548}
549
550statichere PyTypeObject poll_Type = {
551 /* The ob_type field must be initialized in the module init function
552 * to be portable to Windows without using C++. */
553 PyObject_HEAD_INIT(NULL)
554 0, /*ob_size*/
555 "poll", /*tp_name*/
556 sizeof(pollObject), /*tp_basicsize*/
557 0, /*tp_itemsize*/
558 /* methods */
559 (destructor)poll_dealloc, /*tp_dealloc*/
560 0, /*tp_print*/
561 (getattrfunc)poll_getattr, /*tp_getattr*/
562 0, /*tp_setattr*/
563 0, /*tp_compare*/
564 0, /*tp_repr*/
565 0, /*tp_as_number*/
566 0, /*tp_as_sequence*/
567 0, /*tp_as_mapping*/
568 0, /*tp_hash*/
569};
570
571static char poll_doc[] =
572"Returns a polling object, which supports registering and\n\
573unregistering file descriptors, and then polling them for I/O events.";
574
575static PyObject *
576select_poll(PyObject *self, PyObject *args)
577{
578 pollObject *rv;
579
580 if (!PyArg_ParseTuple(args, ":poll"))
581 return NULL;
582 rv = newPollObject();
583 if ( rv == NULL )
584 return NULL;
585 return (PyObject *)rv;
586}
587#endif /* HAVE_POLL */
588
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000589static char select_doc[] =
590"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
591\n\
592Wait until one or more file descriptors are ready for some kind of I/O.\n\
593The first three arguments are lists of file descriptors to be waited for:\n\
594rlist -- wait until ready for reading\n\
595wlist -- wait until ready for writing\n\
596xlist -- wait for an ``exceptional condition''\n\
597If only one kind of condition is required, pass [] for the other lists.\n\
598A file descriptor is either a socket or file object, or a small integer\n\
599gotten from a fileno() method call on one of those.\n\
600\n\
601The optional 4th argument specifies a timeout in seconds; it may be\n\
602a floating point number to specify fractions of seconds. If it is absent\n\
603or None, the call will never time out.\n\
604\n\
605The return value is a tuple of three lists corresponding to the first three\n\
606arguments; each contains the subset of the corresponding file descriptors\n\
607that are ready.\n\
608\n\
609*** IMPORTANT NOTICE ***\n\
610On Windows, only sockets are supported; on Unix, all file descriptors.";
611
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000612static PyMethodDef select_methods[] = {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000613 {"select", select_select, METH_VARARGS, select_doc},
614#ifdef HAVE_POLL
615 {"poll", select_poll, METH_VARARGS, poll_doc},
616#endif /* HAVE_POLL */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000617 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +0000618};
619
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000620static char module_doc[] =
621"This module supports asynchronous I/O on multiple file descriptors.\n\
622\n\
623*** IMPORTANT NOTICE ***\n\
624On Windows, only sockets are supported; on Unix, all file descriptors.";
Guido van Rossumed233a51992-06-23 09:07:03 +0000625
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000626/*
627 * Convenience routine to export an integer value.
628 * For simplicity, errors (which are unlikely anyway) are ignored.
629 */
630
631static void
632insint(PyObject *d, char *name, int value)
633{
634 PyObject *v = PyInt_FromLong((long) value);
635 if (v == NULL) {
636 /* Don't bother reporting this error */
637 PyErr_Clear();
638 }
639 else {
640 PyDict_SetItemString(d, name, v);
641 Py_DECREF(v);
642 }
643}
644
Guido van Rossum3886bb61998-12-04 18:50:17 +0000645DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000646initselect(void)
Guido van Rossumed233a51992-06-23 09:07:03 +0000647{
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000648 PyObject *m, *d;
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000649 m = Py_InitModule3("select", select_methods, module_doc);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000650 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000651 SelectError = PyErr_NewException("select.error", NULL, NULL);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000652 PyDict_SetItemString(d, "error", SelectError);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000653#ifdef HAVE_POLL
654 poll_Type.ob_type = &PyType_Type;
655 insint(d, "POLLIN", POLLIN);
656 insint(d, "POLLPRI", POLLPRI);
657 insint(d, "POLLOUT", POLLOUT);
658 insint(d, "POLLERR", POLLERR);
659 insint(d, "POLLHUP", POLLHUP);
660 insint(d, "POLLNVAL", POLLNVAL);
661
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000662#ifdef POLLRDNORM
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000663 insint(d, "POLLRDNORM", POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000664#endif
665#ifdef POLLRDBAND
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000666 insint(d, "POLLRDBAND", POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000667#endif
668#ifdef POLLWRNORM
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000669 insint(d, "POLLWRNORM", POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000670#endif
671#ifdef POLLWRBAND
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000672 insint(d, "POLLWRBAND", POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000673#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +0000674#ifdef POLLMSG
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000675 insint(d, "POLLMSG", POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +0000676#endif
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000677#endif /* HAVE_POLL */
Guido van Rossumed233a51992-06-23 09:07:03 +0000678}