blob: de910c6077e1643b27de5e7e1ea088fa696b8875 [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
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000053static PyObject *SelectError;
Guido van Rossumed233a51992-06-23 09:07:03 +000054
Barry Warsawc1cb3601996-12-12 22:16:21 +000055/* list of Python objects and their file descriptor */
56typedef struct {
57 PyObject *obj; /* owned reference */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000058 SOCKET fd;
Barry Warsawc1cb3601996-12-12 22:16:21 +000059 int sentinel; /* -1 == sentinel */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000060} pylist;
61
Barry Warsawc1cb3601996-12-12 22:16:21 +000062static void
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +000063reap_obj(pylist fd2obj[FD_SETSIZE + 3])
Barry Warsawc1cb3601996-12-12 22:16:21 +000064{
65 int i;
66 for (i = 0; i < FD_SETSIZE + 3 && fd2obj[i].sentinel >= 0; i++) {
67 Py_XDECREF(fd2obj[i].obj);
68 fd2obj[i].obj = NULL;
69 }
70 fd2obj[0].sentinel = -1;
71}
72
73
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000074/* returns -1 and sets the Python exception if an error occurred, otherwise
75 returns a number >= 0
76*/
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000077static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +000078list2set(PyObject *list, fd_set *set, pylist fd2obj[FD_SETSIZE + 3])
Guido van Rossumed233a51992-06-23 09:07:03 +000079{
Barry Warsawc1cb3601996-12-12 22:16:21 +000080 int i;
81 int max = -1;
82 int index = 0;
83 int len = PyList_Size(list);
84 PyObject* o = NULL;
Guido van Rossum07432c01995-03-29 16:47:45 +000085
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000086 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000087 FD_ZERO(set);
Barry Warsawc1cb3601996-12-12 22:16:21 +000088
89 for (i = 0; i < len; i++) {
Barry Warsawc1cb3601996-12-12 22:16:21 +000090 SOCKET v;
91
92 /* any intervening fileno() calls could decr this refcnt */
Barry Warsaw24c4b3d1996-12-13 23:22:42 +000093 if (!(o = PyList_GetItem(list, i)))
Barry Warsaw529fcfe1996-12-16 18:15:34 +000094 return -1;
Barry Warsaw24c4b3d1996-12-13 23:22:42 +000095
Barry Warsawc1cb3601996-12-12 22:16:21 +000096 Py_INCREF(o);
Andrew M. Kuchling9f28a032000-07-13 23:59:35 +000097 v = PyObject_AsFileDescriptor( o );
98 if (v == -1) goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +000099
Guido van Rossum947a0fa2000-01-14 16:33:09 +0000100#if defined(_MSC_VER)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000101 max = 0; /* not used for Win32 */
102#else /* !_MSC_VER */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000103 if (v < 0 || v >= FD_SETSIZE) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000104 PyErr_SetString(PyExc_ValueError,
105 "filedescriptor out of range in select()");
106 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000107 }
108 if (v > max)
109 max = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000110#endif /* _MSC_VER */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000111 FD_SET(v, set);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000112
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000113 /* add object and its file descriptor to the list */
114 if (index >= FD_SETSIZE) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000115 PyErr_SetString(PyExc_ValueError,
116 "too many file descriptors in select()");
117 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000118 }
119 fd2obj[index].obj = o;
120 fd2obj[index].fd = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000121 fd2obj[index].sentinel = 0;
122 fd2obj[++index].sentinel = -1;
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000123 }
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000124 return max+1;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000125
126 finally:
127 Py_XDECREF(o);
128 return -1;
Guido van Rossumed233a51992-06-23 09:07:03 +0000129}
130
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000131/* returns NULL and sets the Python exception if an error occurred */
132static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000133set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 3])
Guido van Rossumed233a51992-06-23 09:07:03 +0000134{
Barry Warsawc1cb3601996-12-12 22:16:21 +0000135 int i, j, count=0;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000136 PyObject *list, *o;
137 SOCKET fd;
Guido van Rossumed233a51992-06-23 09:07:03 +0000138
Barry Warsawc1cb3601996-12-12 22:16:21 +0000139 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000140 if (FD_ISSET(fd2obj[j].fd, set))
Barry Warsawc1cb3601996-12-12 22:16:21 +0000141 count++;
142 }
143 list = PyList_New(count);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000144 if (!list)
145 return NULL;
146
Barry Warsawc1cb3601996-12-12 22:16:21 +0000147 i = 0;
148 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000149 fd = fd2obj[j].fd;
150 if (FD_ISSET(fd, set)) {
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000151#ifndef _MSC_VER
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000152 if (fd > FD_SETSIZE) {
153 PyErr_SetString(PyExc_SystemError,
154 "filedescriptor out of range returned in select()");
Barry Warsawc1cb3601996-12-12 22:16:21 +0000155 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000156 }
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000157#endif
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000158 o = fd2obj[j].obj;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000159 fd2obj[j].obj = NULL;
160 /* transfer ownership */
161 if (PyList_SetItem(list, i, o) < 0)
162 goto finally;
163
164 i++;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000165 }
166 }
167 return list;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000168 finally:
169 Py_DECREF(list);
170 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000171}
Barry Warsawc1cb3601996-12-12 22:16:21 +0000172
Guido van Rossumed233a51992-06-23 09:07:03 +0000173
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000174static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000175select_select(PyObject *self, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000176{
Guido van Rossumd20781b1998-07-02 02:53:36 +0000177#ifdef MS_WINDOWS
178 /* This would be an awful lot of stack space on Windows! */
179 pylist *rfd2obj, *wfd2obj, *efd2obj;
180#else
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000181 pylist rfd2obj[FD_SETSIZE + 3];
182 pylist wfd2obj[FD_SETSIZE + 3];
183 pylist efd2obj[FD_SETSIZE + 3];
Guido van Rossumd20781b1998-07-02 02:53:36 +0000184#endif
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000185 PyObject *ifdlist, *ofdlist, *efdlist;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000186 PyObject *ret = NULL;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000187 PyObject *tout = Py_None;
188 fd_set ifdset, ofdset, efdset;
189 double timeout;
190 struct timeval tv, *tvp;
Guido van Rossum3262e162000-06-28 21:18:13 +0000191 long seconds;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000192 int imax, omax, emax, max;
193 int n;
Guido van Rossumed233a51992-06-23 09:07:03 +0000194
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000195 /* convert arguments */
Guido van Rossum43713e52000-02-29 13:59:29 +0000196 if (!PyArg_ParseTuple(args, "OOO|O:select",
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000197 &ifdlist, &ofdlist, &efdlist, &tout))
198 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000199
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000200 if (tout == Py_None)
201 tvp = (struct timeval *)0;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000202 else if (!PyArg_Parse(tout, "d", &timeout)) {
203 PyErr_SetString(PyExc_TypeError,
204 "timeout must be a float or None");
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000205 return NULL;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000206 }
Guido van Rossumc7a22701993-11-01 16:27:16 +0000207 else {
Guido van Rossum3262e162000-06-28 21:18:13 +0000208 if (timeout > (double)LONG_MAX) {
209 PyErr_SetString(PyExc_OverflowError, "timeout period too long");
210 return NULL;
211 }
212 seconds = (long)timeout;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000213 timeout = timeout - (double)seconds;
214 tv.tv_sec = seconds;
Guido van Rossum3262e162000-06-28 21:18:13 +0000215 tv.tv_usec = (long)(timeout*1000000.0);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000216 tvp = &tv;
Guido van Rossumc7a22701993-11-01 16:27:16 +0000217 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000218
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000219 /* sanity check first three arguments */
220 if (!PyList_Check(ifdlist) ||
221 !PyList_Check(ofdlist) ||
222 !PyList_Check(efdlist))
223 {
224 PyErr_SetString(PyExc_TypeError,
225 "arguments 1-3 must be lists");
226 return NULL;
227 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000228
Guido van Rossumd20781b1998-07-02 02:53:36 +0000229#ifdef MS_WINDOWS
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) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000235 if (rfd2obj) PyMem_DEL(rfd2obj);
236 if (wfd2obj) PyMem_DEL(wfd2obj);
237 if (efd2obj) PyMem_DEL(efd2obj);
Guido van Rossumd20781b1998-07-02 02:53:36 +0000238 return NULL;
239 }
240#endif
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000241 /* Convert lists to fd_sets, and get maximum fd number
242 * propagates the Python exception set in list2set()
243 */
Barry Warsawc1cb3601996-12-12 22:16:21 +0000244 rfd2obj[0].sentinel = -1;
245 wfd2obj[0].sentinel = -1;
246 efd2obj[0].sentinel = -1;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000247 if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000248 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000249 if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000250 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000251 if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000252 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000253 max = imax;
254 if (omax > max) max = omax;
255 if (emax > max) max = emax;
Guido van Rossumed233a51992-06-23 09:07:03 +0000256
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000257 Py_BEGIN_ALLOW_THREADS
258 n = select(max, &ifdset, &ofdset, &efdset, tvp);
259 Py_END_ALLOW_THREADS
Guido van Rossumed233a51992-06-23 09:07:03 +0000260
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000261 if (n < 0) {
262 PyErr_SetFromErrno(SelectError);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000263 }
Barry Warsawc1cb3601996-12-12 22:16:21 +0000264 else if (n == 0) {
265 /* optimization */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000266 ifdlist = PyList_New(0);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000267 if (ifdlist) {
268 ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist);
269 Py_DECREF(ifdlist);
270 }
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000271 }
Barry Warsawc1cb3601996-12-12 22:16:21 +0000272 else {
273 /* any of these three calls can raise an exception. it's more
274 convenient to test for this after all three calls... but
275 is that acceptable?
276 */
277 ifdlist = set2list(&ifdset, rfd2obj);
278 ofdlist = set2list(&ofdset, wfd2obj);
279 efdlist = set2list(&efdset, efd2obj);
280 if (PyErr_Occurred())
281 ret = NULL;
282 else
283 ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000284
Barry Warsawc1cb3601996-12-12 22:16:21 +0000285 Py_DECREF(ifdlist);
286 Py_DECREF(ofdlist);
287 Py_DECREF(efdlist);
288 }
289
290 finally:
291 reap_obj(rfd2obj);
292 reap_obj(wfd2obj);
293 reap_obj(efd2obj);
Guido van Rossumd20781b1998-07-02 02:53:36 +0000294#ifdef MS_WINDOWS
295 PyMem_DEL(rfd2obj);
296 PyMem_DEL(wfd2obj);
297 PyMem_DEL(efd2obj);
298#endif
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000299 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000300}
301
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000302#ifdef HAVE_POLL
303/*
304 * poll() support
305 */
306
307typedef struct {
308 PyObject_HEAD
309 PyObject *dict;
310 int ufd_uptodate;
311 int ufd_len;
312 struct pollfd *ufds;
313} pollObject;
314
315staticforward PyTypeObject poll_Type;
316
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.
319*/
320
321static int
322update_ufd_array(pollObject *self)
323{
324 int i, j, pos;
325 PyObject *key, *value;
326
327 self->ufd_len = PyDict_Size(self->dict);
328 PyMem_Resize(self->ufds, struct pollfd, self->ufd_len);
329 if (self->ufds == NULL) {
330 PyErr_NoMemory();
331 return 0;
332 }
333
334 i = pos = 0;
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);
338 i++;
339 }
340 self->ufd_uptodate = 1;
341 return 1;
342}
343
344static char poll_register_doc[] =
345"register(fd [, eventmask] ) -> None\n\n\
346Register a file descriptor with the polling object.\n\
347fd -- either an integer, or an object with a fileno() method returning an int.\n\
348events -- an optional bitmask describing the type of events to check for";
349
350static PyObject *
351poll_register(pollObject *self, PyObject *args)
352{
353 PyObject *o, *key, *value;
354 int fd, events = POLLIN | POLLPRI | POLLOUT;
355
356 if (!PyArg_ParseTuple(args, "O|i", &o, &events)) {
357 return NULL;
358 }
359
360 fd = PyObject_AsFileDescriptor(o);
361 if (fd == -1) return NULL;
362
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) {
368 return NULL;
369 }
370 self->ufd_uptodate = 0;
371
372 Py_INCREF(Py_None);
373 return Py_None;
374}
375
376static char poll_unregister_doc[] =
377"unregister(fd) -> None\n\n\
378Remove a file descriptor being tracked by the polling object.";
379
380static PyObject *
381poll_unregister(pollObject *self, PyObject *args)
382{
383 PyObject *o, *key;
384 int fd;
385
386 if (!PyArg_ParseTuple(args, "O", &o)) {
387 return NULL;
388 }
389
390 fd = PyObject_AsFileDescriptor( o );
391 if (fd == -1)
392 return NULL;
393
394 /* Check whether the fd is already in the array */
395 key = PyInt_FromLong(fd);
396 if (key == NULL)
397 return NULL;
398
399 if (PyDict_DelItem(self->dict, key) == -1) {
400 Py_DECREF(key);
401 /* This will simply raise the KeyError set by PyDict_DelItem
402 if the file descriptor isn't registered. */
403 return NULL;
404 }
405
406 Py_DECREF(key);
407 self->ufd_uptodate = 0;
408
409 Py_INCREF(Py_None);
410 return Py_None;
411}
412
413static char poll_poll_doc[] =
414"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
415Polls the set of registered file descriptors, returning a list containing \n\
416any descriptors that have events or errors to report.";
417
418static PyObject *
419poll_poll(pollObject *self, PyObject *args)
420{
421 PyObject *result_list = NULL, *tout = NULL;
422 int timeout = 0, poll_result, i, j;
423 PyObject *value = NULL, *num = NULL;
424
425 if (!PyArg_ParseTuple(args, "|O", &tout)) {
426 return NULL;
427 }
428
429 /* Check values for timeout */
430 if (tout == NULL || tout == Py_None)
431 timeout = -1;
432 else if (!PyArg_Parse(tout, "i", &timeout)) {
433 PyErr_SetString(PyExc_TypeError,
434 "timeout must be an integer or None");
435 return NULL;
436 }
437
438 /* Ensure the ufd array is up to date */
439 if (!self->ufd_uptodate)
440 if (update_ufd_array(self) == 0)
441 return NULL;
442
443 /* call poll() */
444 Py_BEGIN_ALLOW_THREADS;
445 poll_result = poll(self->ufds, self->ufd_len, timeout);
446 Py_END_ALLOW_THREADS;
447
448 if (poll_result < 0) {
449 PyErr_SetFromErrno(SelectError);
450 return NULL;
451 }
452
453 /* build the result list */
454
455 result_list = PyList_New(poll_result);
456 if (!result_list)
457 return NULL;
458 else {
459 for (i = 0, j = 0; j < poll_result; j++) {
460 /* skip to the next fired descriptor */
461 while (!self->ufds[i].revents) {
462 i++;
463 }
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);
468 if (value == NULL)
469 goto error;
470 num = PyInt_FromLong(self->ufds[i].fd);
471 if (num == NULL) {
472 Py_DECREF(value);
473 goto error;
474 }
475 PyTuple_SET_ITEM(value, 0, num);
476
477 num = PyInt_FromLong(self->ufds[i].revents);
478 if (num == NULL) {
479 Py_DECREF(value);
480 goto error;
481 }
482 PyTuple_SET_ITEM(value, 1, num);
483 if ((PyList_SetItem(result_list, j, value)) == -1) {
484 Py_DECREF(value);
485 goto error;
486 }
487 i++;
488 }
489 }
490 return result_list;
491
492 error:
493 Py_DECREF(result_list);
494 return NULL;
495}
496
497static 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 */
505};
506
507static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000508newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000509{
510 pollObject *self;
511 self = PyObject_New(pollObject, &poll_Type);
512 if (self == NULL)
513 return NULL;
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;
517 self->ufds = NULL;
518 self->dict = PyDict_New();
519 if (self->dict == NULL) {
520 Py_DECREF(self);
521 return NULL;
522 }
523 return self;
524}
525
526static void
527poll_dealloc(pollObject *self)
528{
529 if (self->ufds != NULL)
530 PyMem_DEL(self->ufds);
531 Py_XDECREF(self->dict);
532 PyObject_Del(self);
533}
534
535static PyObject *
536poll_getattr(pollObject *self, char *name)
537{
538 return Py_FindMethod(poll_methods, (PyObject *)self, name);
539}
540
541statichere 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)
545 0, /*ob_size*/
546 "poll", /*tp_name*/
547 sizeof(pollObject), /*tp_basicsize*/
548 0, /*tp_itemsize*/
549 /* methods */
550 (destructor)poll_dealloc, /*tp_dealloc*/
551 0, /*tp_print*/
552 (getattrfunc)poll_getattr, /*tp_getattr*/
553 0, /*tp_setattr*/
554 0, /*tp_compare*/
555 0, /*tp_repr*/
556 0, /*tp_as_number*/
557 0, /*tp_as_sequence*/
558 0, /*tp_as_mapping*/
559 0, /*tp_hash*/
560};
561
562static char poll_doc[] =
563"Returns a polling object, which supports registering and\n\
564unregistering file descriptors, and then polling them for I/O events.";
565
566static PyObject *
567select_poll(PyObject *self, PyObject *args)
568{
569 pollObject *rv;
570
571 if (!PyArg_ParseTuple(args, ":poll"))
572 return NULL;
573 rv = newPollObject();
574 if ( rv == NULL )
575 return NULL;
576 return (PyObject *)rv;
577}
578#endif /* HAVE_POLL */
579
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000580static char select_doc[] =
581"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
582\n\
583Wait until one or more file descriptors are ready for some kind of I/O.\n\
584The first three arguments are lists of file descriptors to be waited for:\n\
585rlist -- wait until ready for reading\n\
586wlist -- wait until ready for writing\n\
587xlist -- wait for an ``exceptional condition''\n\
588If only one kind of condition is required, pass [] for the other lists.\n\
589A file descriptor is either a socket or file object, or a small integer\n\
590gotten from a fileno() method call on one of those.\n\
591\n\
592The optional 4th argument specifies a timeout in seconds; it may be\n\
593a floating point number to specify fractions of seconds. If it is absent\n\
594or None, the call will never time out.\n\
595\n\
596The return value is a tuple of three lists corresponding to the first three\n\
597arguments; each contains the subset of the corresponding file descriptors\n\
598that are ready.\n\
599\n\
600*** IMPORTANT NOTICE ***\n\
601On Windows, only sockets are supported; on Unix, all file descriptors.";
602
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000603static PyMethodDef select_methods[] = {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000604 {"select", select_select, METH_VARARGS, select_doc},
605#ifdef HAVE_POLL
606 {"poll", select_poll, METH_VARARGS, poll_doc},
607#endif /* HAVE_POLL */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000608 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +0000609};
610
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000611static char module_doc[] =
612"This module supports asynchronous I/O on multiple file descriptors.\n\
613\n\
614*** IMPORTANT NOTICE ***\n\
615On Windows, only sockets are supported; on Unix, all file descriptors.";
Guido van Rossumed233a51992-06-23 09:07:03 +0000616
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000617/*
618 * Convenience routine to export an integer value.
619 * For simplicity, errors (which are unlikely anyway) are ignored.
620 */
621
622static void
623insint(PyObject *d, char *name, int value)
624{
625 PyObject *v = PyInt_FromLong((long) value);
626 if (v == NULL) {
627 /* Don't bother reporting this error */
628 PyErr_Clear();
629 }
630 else {
631 PyDict_SetItemString(d, name, v);
632 Py_DECREF(v);
633 }
634}
635
Guido van Rossum3886bb61998-12-04 18:50:17 +0000636DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000637initselect(void)
Guido van Rossumed233a51992-06-23 09:07:03 +0000638{
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000639 PyObject *m, *d;
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000640 m = Py_InitModule3("select", select_methods, module_doc);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000641 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000642 SelectError = PyErr_NewException("select.error", NULL, NULL);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000643 PyDict_SetItemString(d, "error", SelectError);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000644#ifdef HAVE_POLL
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);
652
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000653#ifdef POLLRDNORM
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000654 insint(d, "POLLRDNORM", POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000655#endif
656#ifdef POLLRDBAND
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000657 insint(d, "POLLRDBAND", POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000658#endif
659#ifdef POLLWRNORM
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000660 insint(d, "POLLWRNORM", POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000661#endif
662#ifdef POLLWRBAND
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000663 insint(d, "POLLWRBAND", POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000664#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +0000665#ifdef POLLMSG
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000666 insint(d, "POLLMSG", POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +0000667#endif
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000668#endif /* HAVE_POLL */
Guido van Rossumed233a51992-06-23 09:07:03 +0000669}