]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.10/PyMod-2.7.10/Modules/selectmodule.c
AppPkg/.../Python-2.7.10: AppPkg.dsc, pyconfig.h, PyMod-2.7.10
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / PyMod-2.7.10 / Modules / selectmodule.c
CommitLineData
d11973f1
DM
1/* @file\r
2 select - Module containing unix select(2) call.\r
3 Under Unix, the file descriptors are small integers.\r
4 Under Win32, select only exists for sockets, and sockets may\r
5 have any value except INVALID_SOCKET.\r
6 Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything\r
7 >= 0.\r
8\r
9 Copyright (c) 2015, Daryl McDaniel. All rights reserved.<BR>\r
10 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>\r
11 This program and the accompanying materials are licensed and made available under\r
12 the terms and conditions of the BSD License that accompanies this distribution.\r
13 The full text of the license may be found at\r
14 http://opensource.org/licenses/bsd-license.\r
15\r
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
3ec97ca4
DM
18*/\r
19\r
20#include "Python.h"\r
21#include <structmember.h>\r
22\r
23#ifdef __APPLE__\r
24 /* Perform runtime testing for a broken poll on OSX to make it easier\r
25 * to use the same binary on multiple releases of the OS.\r
26 */\r
27#undef HAVE_BROKEN_POLL\r
28#endif\r
29\r
30/* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.\r
31 64 is too small (too many people have bumped into that limit).\r
32 Here we boost it.\r
33 Users who want even more than the boosted limit should #define\r
34 FD_SETSIZE higher before this; e.g., via compiler /D switch.\r
35*/\r
36#if defined(MS_WINDOWS) && !defined(FD_SETSIZE)\r
37#define FD_SETSIZE 512\r
38#endif\r
39\r
40#if defined(HAVE_POLL_H)\r
41#include <poll.h>\r
42#elif defined(HAVE_SYS_POLL_H)\r
43#include <sys/poll.h>\r
44#endif\r
45\r
46#ifdef __sgi\r
47/* This is missing from unistd.h */\r
48extern void bzero(void *, int);\r
49#endif\r
50\r
51#ifdef HAVE_SYS_TYPES_H\r
52#include <sys/types.h>\r
53#endif\r
54\r
55#if defined(PYOS_OS2) && !defined(PYCC_GCC)\r
56#include <sys/time.h>\r
57#include <utils.h>\r
58#endif\r
59\r
60#ifdef MS_WINDOWS\r
61# include <winsock2.h>\r
62#else\r
63# define SOCKET int\r
64# ifdef __BEOS__\r
65# include <net/socket.h>\r
66# elif defined(__VMS)\r
67# include <socket.h>\r
68# endif\r
69#endif\r
70\r
71static PyObject *SelectError;\r
72\r
73/* list of Python objects and their file descriptor */\r
74typedef struct {\r
75 PyObject *obj; /* owned reference */\r
76 SOCKET fd;\r
77 int sentinel; /* -1 == sentinel */\r
78} pylist;\r
79\r
80static void\r
81reap_obj(pylist fd2obj[FD_SETSIZE + 1])\r
82{\r
83 int i;\r
84 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {\r
85 Py_CLEAR(fd2obj[i].obj);\r
86 }\r
87 fd2obj[0].sentinel = -1;\r
88}\r
89\r
90\r
91/* returns -1 and sets the Python exception if an error occurred, otherwise\r
92 returns a number >= 0\r
93*/\r
94static int\r
95seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])\r
96{\r
97 int i;\r
98 int max = -1;\r
99 int index = 0;\r
100 PyObject* fast_seq = NULL;\r
101 PyObject* o = NULL;\r
102\r
103 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */\r
104 FD_ZERO(set);\r
105\r
106 fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences");\r
107 if (!fast_seq)\r
108 return -1;\r
109\r
110 for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++) {\r
111 SOCKET v;\r
112\r
113 /* any intervening fileno() calls could decr this refcnt */\r
114 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))\r
115 return -1;\r
116\r
117 Py_INCREF(o);\r
118 v = PyObject_AsFileDescriptor( o );\r
119 if (v == -1) goto finally;\r
120\r
d11973f1 121#if defined(_MSC_VER) && !defined(UEFI_C_SOURCE)\r
3ec97ca4
DM
122 max = 0; /* not used for Win32 */\r
123#else /* !_MSC_VER */\r
124 if (!_PyIsSelectable_fd(v)) {\r
125 PyErr_SetString(PyExc_ValueError,\r
126 "filedescriptor out of range in select()");\r
127 goto finally;\r
128 }\r
129 if (v > max)\r
130 max = v;\r
131#endif /* _MSC_VER */\r
132 FD_SET(v, set);\r
133\r
134 /* add object and its file descriptor to the list */\r
135 if (index >= FD_SETSIZE) {\r
136 PyErr_SetString(PyExc_ValueError,\r
137 "too many file descriptors in select()");\r
138 goto finally;\r
139 }\r
140 fd2obj[index].obj = o;\r
141 fd2obj[index].fd = v;\r
142 fd2obj[index].sentinel = 0;\r
143 fd2obj[++index].sentinel = -1;\r
144 }\r
145 Py_DECREF(fast_seq);\r
146 return max+1;\r
147\r
148 finally:\r
149 Py_XDECREF(o);\r
150 Py_DECREF(fast_seq);\r
151 return -1;\r
152}\r
153\r
154/* returns NULL and sets the Python exception if an error occurred */\r
155static PyObject *\r
156set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])\r
157{\r
158 int i, j, count=0;\r
159 PyObject *list, *o;\r
160 SOCKET fd;\r
161\r
162 for (j = 0; fd2obj[j].sentinel >= 0; j++) {\r
163 if (FD_ISSET(fd2obj[j].fd, set))\r
164 count++;\r
165 }\r
166 list = PyList_New(count);\r
167 if (!list)\r
168 return NULL;\r
169\r
170 i = 0;\r
171 for (j = 0; fd2obj[j].sentinel >= 0; j++) {\r
172 fd = fd2obj[j].fd;\r
173 if (FD_ISSET(fd, set)) {\r
174 o = fd2obj[j].obj;\r
175 fd2obj[j].obj = NULL;\r
176 /* transfer ownership */\r
177 if (PyList_SetItem(list, i, o) < 0)\r
178 goto finally;\r
179\r
180 i++;\r
181 }\r
182 }\r
183 return list;\r
184 finally:\r
185 Py_DECREF(list);\r
186 return NULL;\r
187}\r
188\r
189#undef SELECT_USES_HEAP\r
190#if FD_SETSIZE > 1024\r
191#define SELECT_USES_HEAP\r
192#endif /* FD_SETSIZE > 1024 */\r
193\r
194static PyObject *\r
195select_select(PyObject *self, PyObject *args)\r
196{\r
197#ifdef SELECT_USES_HEAP\r
198 pylist *rfd2obj, *wfd2obj, *efd2obj;\r
199#else /* !SELECT_USES_HEAP */\r
200 /* XXX: All this should probably be implemented as follows:\r
201 * - find the highest descriptor we're interested in\r
202 * - add one\r
203 * - that's the size\r
204 * See: Stevens, APitUE, $12.5.1\r
205 */\r
206 pylist rfd2obj[FD_SETSIZE + 1];\r
207 pylist wfd2obj[FD_SETSIZE + 1];\r
208 pylist efd2obj[FD_SETSIZE + 1];\r
209#endif /* SELECT_USES_HEAP */\r
210 PyObject *ifdlist, *ofdlist, *efdlist;\r
211 PyObject *ret = NULL;\r
212 PyObject *tout = Py_None;\r
213 fd_set ifdset, ofdset, efdset;\r
214 double timeout;\r
215 struct timeval tv, *tvp;\r
216 long seconds;\r
217 int imax, omax, emax, max;\r
218 int n;\r
219\r
220 /* convert arguments */\r
221 if (!PyArg_UnpackTuple(args, "select", 3, 4,\r
222 &ifdlist, &ofdlist, &efdlist, &tout))\r
223 return NULL;\r
224\r
225 if (tout == Py_None)\r
226 tvp = (struct timeval *)0;\r
227 else if (!PyNumber_Check(tout)) {\r
228 PyErr_SetString(PyExc_TypeError,\r
229 "timeout must be a float or None");\r
230 return NULL;\r
231 }\r
232 else {\r
233 timeout = PyFloat_AsDouble(tout);\r
234 if (timeout == -1 && PyErr_Occurred())\r
235 return NULL;\r
236 if (timeout > (double)LONG_MAX) {\r
237 PyErr_SetString(PyExc_OverflowError,\r
238 "timeout period too long");\r
239 return NULL;\r
240 }\r
241 seconds = (long)timeout;\r
242 timeout = timeout - (double)seconds;\r
243 tv.tv_sec = seconds;\r
244 tv.tv_usec = (long)(timeout * 1E6);\r
245 tvp = &tv;\r
246 }\r
247\r
248\r
249#ifdef SELECT_USES_HEAP\r
250 /* Allocate memory for the lists */\r
251 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);\r
252 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);\r
253 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);\r
254 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {\r
255 if (rfd2obj) PyMem_DEL(rfd2obj);\r
256 if (wfd2obj) PyMem_DEL(wfd2obj);\r
257 if (efd2obj) PyMem_DEL(efd2obj);\r
258 return PyErr_NoMemory();\r
259 }\r
260#endif /* SELECT_USES_HEAP */\r
261 /* Convert sequences to fd_sets, and get maximum fd number\r
262 * propagates the Python exception set in seq2set()\r
263 */\r
264 rfd2obj[0].sentinel = -1;\r
265 wfd2obj[0].sentinel = -1;\r
266 efd2obj[0].sentinel = -1;\r
267 if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)\r
268 goto finally;\r
269 if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)\r
270 goto finally;\r
271 if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)\r
272 goto finally;\r
273 max = imax;\r
274 if (omax > max) max = omax;\r
275 if (emax > max) max = emax;\r
276\r
277 Py_BEGIN_ALLOW_THREADS\r
278 n = select(max, &ifdset, &ofdset, &efdset, tvp);\r
279 Py_END_ALLOW_THREADS\r
280\r
281#ifdef MS_WINDOWS\r
282 if (n == SOCKET_ERROR) {\r
283 PyErr_SetExcFromWindowsErr(SelectError, WSAGetLastError());\r
284 }\r
285#else\r
286 if (n < 0) {\r
287 PyErr_SetFromErrno(SelectError);\r
288 }\r
289#endif\r
290 else {\r
291 /* any of these three calls can raise an exception. it's more\r
292 convenient to test for this after all three calls... but\r
293 is that acceptable?\r
294 */\r
295 ifdlist = set2list(&ifdset, rfd2obj);\r
296 ofdlist = set2list(&ofdset, wfd2obj);\r
297 efdlist = set2list(&efdset, efd2obj);\r
298 if (PyErr_Occurred())\r
299 ret = NULL;\r
300 else\r
301 ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);\r
302\r
303 Py_DECREF(ifdlist);\r
304 Py_DECREF(ofdlist);\r
305 Py_DECREF(efdlist);\r
306 }\r
307\r
308 finally:\r
309 reap_obj(rfd2obj);\r
310 reap_obj(wfd2obj);\r
311 reap_obj(efd2obj);\r
312#ifdef SELECT_USES_HEAP\r
313 PyMem_DEL(rfd2obj);\r
314 PyMem_DEL(wfd2obj);\r
315 PyMem_DEL(efd2obj);\r
316#endif /* SELECT_USES_HEAP */\r
317 return ret;\r
318}\r
319\r
320#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)\r
321/*\r
322 * poll() support\r
323 */\r
324\r
325typedef struct {\r
326 PyObject_HEAD\r
327 PyObject *dict;\r
328 int ufd_uptodate;\r
329 int ufd_len;\r
330 struct pollfd *ufds;\r
331 int poll_running;\r
332} pollObject;\r
333\r
334static PyTypeObject poll_Type;\r
335\r
336/* Update the malloc'ed array of pollfds to match the dictionary\r
337 contained within a pollObject. Return 1 on success, 0 on an error.\r
338*/\r
339\r
340static int\r
341update_ufd_array(pollObject *self)\r
342{\r
343 Py_ssize_t i, pos;\r
344 PyObject *key, *value;\r
345 struct pollfd *old_ufds = self->ufds;\r
346\r
347 self->ufd_len = PyDict_Size(self->dict);\r
348 PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);\r
349 if (self->ufds == NULL) {\r
350 self->ufds = old_ufds;\r
351 PyErr_NoMemory();\r
352 return 0;\r
353 }\r
354\r
355 i = pos = 0;\r
356 while (PyDict_Next(self->dict, &pos, &key, &value)) {\r
357 assert(i < self->ufd_len);\r
358 /* Never overflow */\r
359 self->ufds[i].fd = (int)PyInt_AsLong(key);\r
360 self->ufds[i].events = (short)(unsigned short)PyInt_AsLong(value);\r
361 i++;\r
362 }\r
363 assert(i == self->ufd_len);\r
364 self->ufd_uptodate = 1;\r
365 return 1;\r
366}\r
367\r
368static int\r
369ushort_converter(PyObject *obj, void *ptr)\r
370{\r
371 unsigned long uval;\r
372\r
373 uval = PyLong_AsUnsignedLong(obj);\r
374 if (uval == (unsigned long)-1 && PyErr_Occurred())\r
375 return 0;\r
376 if (uval > USHRT_MAX) {\r
377 PyErr_SetString(PyExc_OverflowError,\r
378 "Python int too large for C unsigned short");\r
379 return 0;\r
380 }\r
381\r
382 *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short);\r
383 return 1;\r
384}\r
385\r
386PyDoc_STRVAR(poll_register_doc,\r
387"register(fd [, eventmask] ) -> None\n\n\\r
388Register a file descriptor with the polling object.\n\\r
389fd -- either an integer, or an object with a fileno() method returning an\n\\r
390 int.\n\\r
391events -- an optional bitmask describing the type of events to check for");\r
392\r
393static PyObject *\r
394poll_register(pollObject *self, PyObject *args)\r
395{\r
396 PyObject *o, *key, *value;\r
397 int fd;\r
398 unsigned short events = POLLIN | POLLPRI | POLLOUT;\r
399 int err;\r
400\r
401 if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events))\r
402 return NULL;\r
403\r
404 fd = PyObject_AsFileDescriptor(o);\r
405 if (fd == -1) return NULL;\r
406\r
407 /* Add entry to the internal dictionary: the key is the\r
408 file descriptor, and the value is the event mask. */\r
409 key = PyInt_FromLong(fd);\r
410 if (key == NULL)\r
411 return NULL;\r
412 value = PyInt_FromLong(events);\r
413 if (value == NULL) {\r
414 Py_DECREF(key);\r
415 return NULL;\r
416 }\r
417 err = PyDict_SetItem(self->dict, key, value);\r
418 Py_DECREF(key);\r
419 Py_DECREF(value);\r
420 if (err < 0)\r
421 return NULL;\r
422\r
423 self->ufd_uptodate = 0;\r
424\r
425 Py_INCREF(Py_None);\r
426 return Py_None;\r
427}\r
428\r
429PyDoc_STRVAR(poll_modify_doc,\r
430"modify(fd, eventmask) -> None\n\n\\r
431Modify an already registered file descriptor.\n\\r
432fd -- either an integer, or an object with a fileno() method returning an\n\\r
433 int.\n\\r
434events -- an optional bitmask describing the type of events to check for");\r
435\r
436static PyObject *\r
437poll_modify(pollObject *self, PyObject *args)\r
438{\r
439 PyObject *o, *key, *value;\r
440 int fd;\r
441 unsigned short events;\r
442 int err;\r
443\r
444 if (!PyArg_ParseTuple(args, "OO&:modify", &o, ushort_converter, &events))\r
445 return NULL;\r
446\r
447 fd = PyObject_AsFileDescriptor(o);\r
448 if (fd == -1) return NULL;\r
449\r
450 /* Modify registered fd */\r
451 key = PyInt_FromLong(fd);\r
452 if (key == NULL)\r
453 return NULL;\r
454 if (PyDict_GetItem(self->dict, key) == NULL) {\r
455 errno = ENOENT;\r
456 PyErr_SetFromErrno(PyExc_IOError);\r
457 return NULL;\r
458 }\r
459 value = PyInt_FromLong(events);\r
460 if (value == NULL) {\r
461 Py_DECREF(key);\r
462 return NULL;\r
463 }\r
464 err = PyDict_SetItem(self->dict, key, value);\r
465 Py_DECREF(key);\r
466 Py_DECREF(value);\r
467 if (err < 0)\r
468 return NULL;\r
469\r
470 self->ufd_uptodate = 0;\r
471\r
472 Py_INCREF(Py_None);\r
473 return Py_None;\r
474}\r
475\r
476\r
477PyDoc_STRVAR(poll_unregister_doc,\r
478"unregister(fd) -> None\n\n\\r
479Remove a file descriptor being tracked by the polling object.");\r
480\r
481static PyObject *\r
482poll_unregister(pollObject *self, PyObject *o)\r
483{\r
484 PyObject *key;\r
485 int fd;\r
486\r
487 fd = PyObject_AsFileDescriptor( o );\r
488 if (fd == -1)\r
489 return NULL;\r
490\r
491 /* Check whether the fd is already in the array */\r
492 key = PyInt_FromLong(fd);\r
493 if (key == NULL)\r
494 return NULL;\r
495\r
496 if (PyDict_DelItem(self->dict, key) == -1) {\r
497 Py_DECREF(key);\r
498 /* This will simply raise the KeyError set by PyDict_DelItem\r
499 if the file descriptor isn't registered. */\r
500 return NULL;\r
501 }\r
502\r
503 Py_DECREF(key);\r
504 self->ufd_uptodate = 0;\r
505\r
506 Py_INCREF(Py_None);\r
507 return Py_None;\r
508}\r
509\r
510PyDoc_STRVAR(poll_poll_doc,\r
511"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\\r
512Polls the set of registered file descriptors, returning a list containing \n\\r
513any descriptors that have events or errors to report.");\r
514\r
515static PyObject *\r
516poll_poll(pollObject *self, PyObject *args)\r
517{\r
518 PyObject *result_list = NULL, *tout = NULL;\r
519 int timeout = 0, poll_result, i, j;\r
520 PyObject *value = NULL, *num = NULL;\r
521\r
522 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {\r
523 return NULL;\r
524 }\r
525\r
526 /* Check values for timeout */\r
527 if (tout == NULL || tout == Py_None)\r
528 timeout = -1;\r
529 else if (!PyNumber_Check(tout)) {\r
530 PyErr_SetString(PyExc_TypeError,\r
531 "timeout must be an integer or None");\r
532 return NULL;\r
533 }\r
534 else {\r
535 tout = PyNumber_Int(tout);\r
536 if (!tout)\r
537 return NULL;\r
538 timeout = _PyInt_AsInt(tout);\r
539 Py_DECREF(tout);\r
540 if (timeout == -1 && PyErr_Occurred())\r
541 return NULL;\r
542 }\r
543\r
544 /* Avoid concurrent poll() invocation, issue 8865 */\r
545 if (self->poll_running) {\r
546 PyErr_SetString(PyExc_RuntimeError,\r
547 "concurrent poll() invocation");\r
548 return NULL;\r
549 }\r
550\r
551 /* Ensure the ufd array is up to date */\r
552 if (!self->ufd_uptodate)\r
553 if (update_ufd_array(self) == 0)\r
554 return NULL;\r
555\r
556 self->poll_running = 1;\r
557\r
558 /* call poll() */\r
559 Py_BEGIN_ALLOW_THREADS\r
560 poll_result = poll(self->ufds, self->ufd_len, timeout);\r
561 Py_END_ALLOW_THREADS\r
562\r
563 self->poll_running = 0;\r
564\r
565 if (poll_result < 0) {\r
566 PyErr_SetFromErrno(SelectError);\r
567 return NULL;\r
568 }\r
569\r
570 /* build the result list */\r
571\r
572 result_list = PyList_New(poll_result);\r
573 if (!result_list)\r
574 return NULL;\r
575 else {\r
576 for (i = 0, j = 0; j < poll_result; j++) {\r
577 /* skip to the next fired descriptor */\r
578 while (!self->ufds[i].revents) {\r
579 i++;\r
580 }\r
581 /* if we hit a NULL return, set value to NULL\r
582 and break out of loop; code at end will\r
583 clean up result_list */\r
584 value = PyTuple_New(2);\r
585 if (value == NULL)\r
586 goto error;\r
587 num = PyInt_FromLong(self->ufds[i].fd);\r
588 if (num == NULL) {\r
589 Py_DECREF(value);\r
590 goto error;\r
591 }\r
592 PyTuple_SET_ITEM(value, 0, num);\r
593\r
594 /* The &0xffff is a workaround for AIX. 'revents'\r
595 is a 16-bit short, and IBM assigned POLLNVAL\r
596 to be 0x8000, so the conversion to int results\r
597 in a negative number. See SF bug #923315. */\r
598 num = PyInt_FromLong(self->ufds[i].revents & 0xffff);\r
599 if (num == NULL) {\r
600 Py_DECREF(value);\r
601 goto error;\r
602 }\r
603 PyTuple_SET_ITEM(value, 1, num);\r
604 if ((PyList_SetItem(result_list, j, value)) == -1) {\r
605 Py_DECREF(value);\r
606 goto error;\r
607 }\r
608 i++;\r
609 }\r
610 }\r
611 return result_list;\r
612\r
613 error:\r
614 Py_DECREF(result_list);\r
615 return NULL;\r
616}\r
617\r
618static PyMethodDef poll_methods[] = {\r
619 {"register", (PyCFunction)poll_register,\r
620 METH_VARARGS, poll_register_doc},\r
621 {"modify", (PyCFunction)poll_modify,\r
622 METH_VARARGS, poll_modify_doc},\r
623 {"unregister", (PyCFunction)poll_unregister,\r
624 METH_O, poll_unregister_doc},\r
625 {"poll", (PyCFunction)poll_poll,\r
626 METH_VARARGS, poll_poll_doc},\r
627 {NULL, NULL} /* sentinel */\r
628};\r
629\r
630static pollObject *\r
631newPollObject(void)\r
632{\r
633 pollObject *self;\r
634 self = PyObject_New(pollObject, &poll_Type);\r
635 if (self == NULL)\r
636 return NULL;\r
637 /* ufd_uptodate is a Boolean, denoting whether the\r
638 array pointed to by ufds matches the contents of the dictionary. */\r
639 self->ufd_uptodate = 0;\r
640 self->ufds = NULL;\r
641 self->poll_running = 0;\r
642 self->dict = PyDict_New();\r
643 if (self->dict == NULL) {\r
644 Py_DECREF(self);\r
645 return NULL;\r
646 }\r
647 return self;\r
648}\r
649\r
650static void\r
651poll_dealloc(pollObject *self)\r
652{\r
653 if (self->ufds != NULL)\r
654 PyMem_DEL(self->ufds);\r
655 Py_XDECREF(self->dict);\r
656 PyObject_Del(self);\r
657}\r
658\r
659static PyObject *\r
660poll_getattr(pollObject *self, char *name)\r
661{\r
662 return Py_FindMethod(poll_methods, (PyObject *)self, name);\r
663}\r
664\r
665static PyTypeObject poll_Type = {\r
666 /* The ob_type field must be initialized in the module init function\r
667 * to be portable to Windows without using C++. */\r
668 PyVarObject_HEAD_INIT(NULL, 0)\r
669 "select.poll", /*tp_name*/\r
670 sizeof(pollObject), /*tp_basicsize*/\r
671 0, /*tp_itemsize*/\r
672 /* methods */\r
673 (destructor)poll_dealloc, /*tp_dealloc*/\r
674 0, /*tp_print*/\r
675 (getattrfunc)poll_getattr, /*tp_getattr*/\r
676 0, /*tp_setattr*/\r
677 0, /*tp_compare*/\r
678 0, /*tp_repr*/\r
679 0, /*tp_as_number*/\r
680 0, /*tp_as_sequence*/\r
681 0, /*tp_as_mapping*/\r
682 0, /*tp_hash*/\r
683};\r
684\r
685PyDoc_STRVAR(poll_doc,\r
686"Returns a polling object, which supports registering and\n\\r
687unregistering file descriptors, and then polling them for I/O events.");\r
688\r
689static PyObject *\r
690select_poll(PyObject *self, PyObject *unused)\r
691{\r
692 return (PyObject *)newPollObject();\r
693}\r
694\r
695#ifdef __APPLE__\r
696/*\r
697 * On some systems poll() sets errno on invalid file descriptors. We test\r
698 * for this at runtime because this bug may be fixed or introduced between\r
699 * OS releases.\r
700 */\r
701static int select_have_broken_poll(void)\r
702{\r
703 int poll_test;\r
704 int filedes[2];\r
705\r
706 struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };\r
707\r
708 /* Create a file descriptor to make invalid */\r
709 if (pipe(filedes) < 0) {\r
710 return 1;\r
711 }\r
712 poll_struct.fd = filedes[0];\r
713 close(filedes[0]);\r
714 close(filedes[1]);\r
715 poll_test = poll(&poll_struct, 1, 0);\r
716 if (poll_test < 0) {\r
717 return 1;\r
718 } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {\r
719 return 1;\r
720 }\r
721 return 0;\r
722}\r
723#endif /* __APPLE__ */\r
724\r
725#endif /* HAVE_POLL */\r
726\r
727#ifdef HAVE_EPOLL\r
728/* **************************************************************************\r
729 * epoll interface for Linux 2.6\r
730 *\r
731 * Written by Christian Heimes\r
732 * Inspired by Twisted's _epoll.pyx and select.poll()\r
733 */\r
734\r
735#ifdef HAVE_SYS_EPOLL_H\r
736#include <sys/epoll.h>\r
737#endif\r
738\r
739typedef struct {\r
740 PyObject_HEAD\r
741 SOCKET epfd; /* epoll control file descriptor */\r
742} pyEpoll_Object;\r
743\r
744static PyTypeObject pyEpoll_Type;\r
745#define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type))\r
746\r
747static PyObject *\r
748pyepoll_err_closed(void)\r
749{\r
750 PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll fd");\r
751 return NULL;\r
752}\r
753\r
754static int\r
755pyepoll_internal_close(pyEpoll_Object *self)\r
756{\r
757 int save_errno = 0;\r
758 if (self->epfd >= 0) {\r
759 int epfd = self->epfd;\r
760 self->epfd = -1;\r
761 Py_BEGIN_ALLOW_THREADS\r
762 if (close(epfd) < 0)\r
763 save_errno = errno;\r
764 Py_END_ALLOW_THREADS\r
765 }\r
766 return save_errno;\r
767}\r
768\r
769static PyObject *\r
770newPyEpoll_Object(PyTypeObject *type, int sizehint, SOCKET fd)\r
771{\r
772 pyEpoll_Object *self;\r
773\r
774 if (sizehint == -1) {\r
775 sizehint = FD_SETSIZE-1;\r
776 }\r
777 else if (sizehint < 1) {\r
778 PyErr_Format(PyExc_ValueError,\r
779 "sizehint must be greater zero, got %d",\r
780 sizehint);\r
781 return NULL;\r
782 }\r
783\r
784 assert(type != NULL && type->tp_alloc != NULL);\r
785 self = (pyEpoll_Object *) type->tp_alloc(type, 0);\r
786 if (self == NULL)\r
787 return NULL;\r
788\r
789 if (fd == -1) {\r
790 Py_BEGIN_ALLOW_THREADS\r
791 self->epfd = epoll_create(sizehint);\r
792 Py_END_ALLOW_THREADS\r
793 }\r
794 else {\r
795 self->epfd = fd;\r
796 }\r
797 if (self->epfd < 0) {\r
798 Py_DECREF(self);\r
799 PyErr_SetFromErrno(PyExc_IOError);\r
800 return NULL;\r
801 }\r
802 return (PyObject *)self;\r
803}\r
804\r
805\r
806static PyObject *\r
807pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
808{\r
809 int sizehint = -1;\r
810 static char *kwlist[] = {"sizehint", NULL};\r
811\r
812 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:epoll", kwlist,\r
813 &sizehint))\r
814 return NULL;\r
815\r
816 return newPyEpoll_Object(type, sizehint, -1);\r
817}\r
818\r
819\r
820static void\r
821pyepoll_dealloc(pyEpoll_Object *self)\r
822{\r
823 (void)pyepoll_internal_close(self);\r
824 Py_TYPE(self)->tp_free(self);\r
825}\r
826\r
827static PyObject*\r
828pyepoll_close(pyEpoll_Object *self)\r
829{\r
830 errno = pyepoll_internal_close(self);\r
831 if (errno < 0) {\r
832 PyErr_SetFromErrno(PyExc_IOError);\r
833 return NULL;\r
834 }\r
835 Py_RETURN_NONE;\r
836}\r
837\r
838PyDoc_STRVAR(pyepoll_close_doc,\r
839"close() -> None\n\\r
840\n\\r
841Close the epoll control file descriptor. Further operations on the epoll\n\\r
842object will raise an exception.");\r
843\r
844static PyObject*\r
845pyepoll_get_closed(pyEpoll_Object *self)\r
846{\r
847 if (self->epfd < 0)\r
848 Py_RETURN_TRUE;\r
849 else\r
850 Py_RETURN_FALSE;\r
851}\r
852\r
853static PyObject*\r
854pyepoll_fileno(pyEpoll_Object *self)\r
855{\r
856 if (self->epfd < 0)\r
857 return pyepoll_err_closed();\r
858 return PyInt_FromLong(self->epfd);\r
859}\r
860\r
861PyDoc_STRVAR(pyepoll_fileno_doc,\r
862"fileno() -> int\n\\r
863\n\\r
864Return the epoll control file descriptor.");\r
865\r
866static PyObject*\r
867pyepoll_fromfd(PyObject *cls, PyObject *args)\r
868{\r
869 SOCKET fd;\r
870\r
871 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))\r
872 return NULL;\r
873\r
874 return newPyEpoll_Object((PyTypeObject*)cls, -1, fd);\r
875}\r
876\r
877PyDoc_STRVAR(pyepoll_fromfd_doc,\r
878"fromfd(fd) -> epoll\n\\r
879\n\\r
880Create an epoll object from a given control fd.");\r
881\r
882static PyObject *\r
883pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)\r
884{\r
885 struct epoll_event ev;\r
886 int result;\r
887 int fd;\r
888\r
889 if (epfd < 0)\r
890 return pyepoll_err_closed();\r
891\r
892 fd = PyObject_AsFileDescriptor(pfd);\r
893 if (fd == -1) {\r
894 return NULL;\r
895 }\r
896\r
897 switch(op) {\r
898 case EPOLL_CTL_ADD:\r
899 case EPOLL_CTL_MOD:\r
900 ev.events = events;\r
901 ev.data.fd = fd;\r
902 Py_BEGIN_ALLOW_THREADS\r
903 result = epoll_ctl(epfd, op, fd, &ev);\r
904 Py_END_ALLOW_THREADS\r
905 break;\r
906 case EPOLL_CTL_DEL:\r
907 /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL\r
908 * operation required a non-NULL pointer in event, even\r
909 * though this argument is ignored. */\r
910 Py_BEGIN_ALLOW_THREADS\r
911 result = epoll_ctl(epfd, op, fd, &ev);\r
912 if (errno == EBADF) {\r
913 /* fd already closed */\r
914 result = 0;\r
915 errno = 0;\r
916 }\r
917 Py_END_ALLOW_THREADS\r
918 break;\r
919 default:\r
920 result = -1;\r
921 errno = EINVAL;\r
922 }\r
923\r
924 if (result < 0) {\r
925 PyErr_SetFromErrno(PyExc_IOError);\r
926 return NULL;\r
927 }\r
928 Py_RETURN_NONE;\r
929}\r
930\r
931static PyObject *\r
932pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)\r
933{\r
934 PyObject *pfd;\r
935 unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;\r
936 static char *kwlist[] = {"fd", "eventmask", NULL};\r
937\r
938 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,\r
939 &pfd, &events)) {\r
940 return NULL;\r
941 }\r
942\r
943 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);\r
944}\r
945\r
946PyDoc_STRVAR(pyepoll_register_doc,\r
947"register(fd[, eventmask]) -> None\n\\r
948\n\\r
949Registers a new fd or raises an IOError if the fd is already registered.\n\\r
950fd is the target file descriptor of the operation.\n\\r
951events is a bit set composed of the various EPOLL constants; the default\n\\r
952is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\\r
953\n\\r
954The epoll interface supports all file descriptors that support poll.");\r
955\r
956static PyObject *\r
957pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)\r
958{\r
959 PyObject *pfd;\r
960 unsigned int events;\r
961 static char *kwlist[] = {"fd", "eventmask", NULL};\r
962\r
963 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,\r
964 &pfd, &events)) {\r
965 return NULL;\r
966 }\r
967\r
968 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);\r
969}\r
970\r
971PyDoc_STRVAR(pyepoll_modify_doc,\r
972"modify(fd, eventmask) -> None\n\\r
973\n\\r
974fd is the target file descriptor of the operation\n\\r
975events is a bit set composed of the various EPOLL constants");\r
976\r
977static PyObject *\r
978pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds)\r
979{\r
980 PyObject *pfd;\r
981 static char *kwlist[] = {"fd", NULL};\r
982\r
983 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,\r
984 &pfd)) {\r
985 return NULL;\r
986 }\r
987\r
988 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);\r
989}\r
990\r
991PyDoc_STRVAR(pyepoll_unregister_doc,\r
992"unregister(fd) -> None\n\\r
993\n\\r
994fd is the target file descriptor of the operation.");\r
995\r
996static PyObject *\r
997pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)\r
998{\r
999 double dtimeout = -1.;\r
1000 int timeout;\r
1001 int maxevents = -1;\r
1002 int nfds, i;\r
1003 PyObject *elist = NULL, *etuple = NULL;\r
1004 struct epoll_event *evs = NULL;\r
1005 static char *kwlist[] = {"timeout", "maxevents", NULL};\r
1006\r
1007 if (self->epfd < 0)\r
1008 return pyepoll_err_closed();\r
1009\r
1010 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,\r
1011 &dtimeout, &maxevents)) {\r
1012 return NULL;\r
1013 }\r
1014\r
1015 if (dtimeout < 0) {\r
1016 timeout = -1;\r
1017 }\r
1018 else if (dtimeout * 1000.0 > INT_MAX) {\r
1019 PyErr_SetString(PyExc_OverflowError,\r
1020 "timeout is too large");\r
1021 return NULL;\r
1022 }\r
1023 else {\r
1024 timeout = (int)(dtimeout * 1000.0);\r
1025 }\r
1026\r
1027 if (maxevents == -1) {\r
1028 maxevents = FD_SETSIZE-1;\r
1029 }\r
1030 else if (maxevents < 1) {\r
1031 PyErr_Format(PyExc_ValueError,\r
1032 "maxevents must be greater than 0, got %d",\r
1033 maxevents);\r
1034 return NULL;\r
1035 }\r
1036\r
1037 evs = PyMem_New(struct epoll_event, maxevents);\r
1038 if (evs == NULL) {\r
1039 Py_DECREF(self);\r
1040 PyErr_NoMemory();\r
1041 return NULL;\r
1042 }\r
1043\r
1044 Py_BEGIN_ALLOW_THREADS\r
1045 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);\r
1046 Py_END_ALLOW_THREADS\r
1047 if (nfds < 0) {\r
1048 PyErr_SetFromErrno(PyExc_IOError);\r
1049 goto error;\r
1050 }\r
1051\r
1052 elist = PyList_New(nfds);\r
1053 if (elist == NULL) {\r
1054 goto error;\r
1055 }\r
1056\r
1057 for (i = 0; i < nfds; i++) {\r
1058 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);\r
1059 if (etuple == NULL) {\r
1060 Py_CLEAR(elist);\r
1061 goto error;\r
1062 }\r
1063 PyList_SET_ITEM(elist, i, etuple);\r
1064 }\r
1065\r
1066 error:\r
1067 PyMem_Free(evs);\r
1068 return elist;\r
1069}\r
1070\r
1071PyDoc_STRVAR(pyepoll_poll_doc,\r
1072"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\\r
1073\n\\r
1074Wait for events on the epoll file descriptor for a maximum time of timeout\n\\r
1075in seconds (as float). -1 makes poll wait indefinitely.\n\\r
1076Up to maxevents are returned to the caller.");\r
1077\r
1078static PyMethodDef pyepoll_methods[] = {\r
1079 {"fromfd", (PyCFunction)pyepoll_fromfd,\r
1080 METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},\r
1081 {"close", (PyCFunction)pyepoll_close, METH_NOARGS,\r
1082 pyepoll_close_doc},\r
1083 {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,\r
1084 pyepoll_fileno_doc},\r
1085 {"modify", (PyCFunction)pyepoll_modify,\r
1086 METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},\r
1087 {"register", (PyCFunction)pyepoll_register,\r
1088 METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},\r
1089 {"unregister", (PyCFunction)pyepoll_unregister,\r
1090 METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},\r
1091 {"poll", (PyCFunction)pyepoll_poll,\r
1092 METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},\r
1093 {NULL, NULL},\r
1094};\r
1095\r
1096static PyGetSetDef pyepoll_getsetlist[] = {\r
1097 {"closed", (getter)pyepoll_get_closed, NULL,\r
1098 "True if the epoll handler is closed"},\r
1099 {0},\r
1100};\r
1101\r
1102PyDoc_STRVAR(pyepoll_doc,\r
1103"select.epoll([sizehint=-1])\n\\r
1104\n\\r
1105Returns an epolling object\n\\r
1106\n\\r
1107sizehint must be a positive integer or -1 for the default size. The\n\\r
1108sizehint is used to optimize internal data structures. It doesn't limit\n\\r
1109the maximum number of monitored events.");\r
1110\r
1111static PyTypeObject pyEpoll_Type = {\r
1112 PyVarObject_HEAD_INIT(NULL, 0)\r
1113 "select.epoll", /* tp_name */\r
1114 sizeof(pyEpoll_Object), /* tp_basicsize */\r
1115 0, /* tp_itemsize */\r
1116 (destructor)pyepoll_dealloc, /* tp_dealloc */\r
1117 0, /* tp_print */\r
1118 0, /* tp_getattr */\r
1119 0, /* tp_setattr */\r
1120 0, /* tp_compare */\r
1121 0, /* tp_repr */\r
1122 0, /* tp_as_number */\r
1123 0, /* tp_as_sequence */\r
1124 0, /* tp_as_mapping */\r
1125 0, /* tp_hash */\r
1126 0, /* tp_call */\r
1127 0, /* tp_str */\r
1128 PyObject_GenericGetAttr, /* tp_getattro */\r
1129 0, /* tp_setattro */\r
1130 0, /* tp_as_buffer */\r
1131 Py_TPFLAGS_DEFAULT, /* tp_flags */\r
1132 pyepoll_doc, /* tp_doc */\r
1133 0, /* tp_traverse */\r
1134 0, /* tp_clear */\r
1135 0, /* tp_richcompare */\r
1136 0, /* tp_weaklistoffset */\r
1137 0, /* tp_iter */\r
1138 0, /* tp_iternext */\r
1139 pyepoll_methods, /* tp_methods */\r
1140 0, /* tp_members */\r
1141 pyepoll_getsetlist, /* tp_getset */\r
1142 0, /* tp_base */\r
1143 0, /* tp_dict */\r
1144 0, /* tp_descr_get */\r
1145 0, /* tp_descr_set */\r
1146 0, /* tp_dictoffset */\r
1147 0, /* tp_init */\r
1148 0, /* tp_alloc */\r
1149 pyepoll_new, /* tp_new */\r
1150 0, /* tp_free */\r
1151};\r
1152\r
1153#endif /* HAVE_EPOLL */\r
1154\r
1155#ifdef HAVE_KQUEUE\r
1156/* **************************************************************************\r
1157 * kqueue interface for BSD\r
1158 *\r
1159 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes\r
1160 * All rights reserved.\r
1161 *\r
1162 * Redistribution and use in source and binary forms, with or without\r
1163 * modification, are permitted provided that the following conditions\r
1164 * are met:\r
1165 * 1. Redistributions of source code must retain the above copyright\r
1166 * notice, this list of conditions and the following disclaimer.\r
1167 * 2. Redistributions in binary form must reproduce the above copyright\r
1168 * notice, this list of conditions and the following disclaimer in the\r
1169 * documentation and/or other materials provided with the distribution.\r
1170 *\r
1171 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND\r
1172 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
1173 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
1174 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\r
1175 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
1176 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
1177 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
1178 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
1179 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
1180 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
1181 * SUCH DAMAGE.\r
1182 */\r
1183\r
1184#ifdef HAVE_SYS_EVENT_H\r
1185#include <sys/event.h>\r
1186#endif\r
1187\r
1188PyDoc_STRVAR(kqueue_event_doc,\r
1189"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\\r
1190\n\\r
1191This object is the equivalent of the struct kevent for the C API.\n\\r
1192\n\\r
1193See the kqueue manpage for more detailed information about the meaning\n\\r
1194of the arguments.\n\\r
1195\n\\r
1196One minor note: while you might hope that udata could store a\n\\r
1197reference to a python object, it cannot, because it is impossible to\n\\r
1198keep a proper reference count of the object once it's passed into the\n\\r
1199kernel. Therefore, I have restricted it to only storing an integer. I\n\\r
1200recommend ignoring it and simply using the 'ident' field to key off\n\\r
1201of. You could also set up a dictionary on the python side to store a\n\\r
1202udata->object mapping.");\r
1203\r
1204typedef struct {\r
1205 PyObject_HEAD\r
1206 struct kevent e;\r
1207} kqueue_event_Object;\r
1208\r
1209static PyTypeObject kqueue_event_Type;\r
1210\r
1211#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))\r
1212\r
1213typedef struct {\r
1214 PyObject_HEAD\r
1215 SOCKET kqfd; /* kqueue control fd */\r
1216} kqueue_queue_Object;\r
1217\r
1218static PyTypeObject kqueue_queue_Type;\r
1219\r
1220#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))\r
1221\r
1222#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)\r
1223# error uintptr_t does not match void *!\r
1224#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)\r
1225# define T_UINTPTRT T_ULONGLONG\r
1226# define T_INTPTRT T_LONGLONG\r
1227# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong\r
1228# define UINTPTRT_FMT_UNIT "K"\r
1229# define INTPTRT_FMT_UNIT "L"\r
1230#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)\r
1231# define T_UINTPTRT T_ULONG\r
1232# define T_INTPTRT T_LONG\r
1233# define PyLong_AsUintptr_t PyLong_AsUnsignedLong\r
1234# define UINTPTRT_FMT_UNIT "k"\r
1235# define INTPTRT_FMT_UNIT "l"\r
1236#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)\r
1237# define T_UINTPTRT T_UINT\r
1238# define T_INTPTRT T_INT\r
1239# define PyLong_AsUintptr_t PyLong_AsUnsignedLong\r
1240# define UINTPTRT_FMT_UNIT "I"\r
1241# define INTPTRT_FMT_UNIT "i"\r
1242#else\r
1243# error uintptr_t does not match int, long, or long long!\r
1244#endif\r
1245\r
1246/*\r
1247 * kevent is not standard and its members vary across BSDs.\r
1248 */\r
1249#if !defined(__OpenBSD__)\r
d11973f1
DM
1250# define IDENT_TYPE T_UINTPTRT\r
1251# define IDENT_CAST Py_intptr_t\r
1252# define DATA_TYPE T_INTPTRT\r
3ec97ca4 1253# define DATA_FMT_UNIT INTPTRT_FMT_UNIT\r
d11973f1 1254# define IDENT_AsType PyLong_AsUintptr_t\r
3ec97ca4 1255#else\r
d11973f1
DM
1256# define IDENT_TYPE T_UINT\r
1257# define IDENT_CAST int\r
1258# define DATA_TYPE T_INT\r
3ec97ca4 1259# define DATA_FMT_UNIT "i"\r
d11973f1 1260# define IDENT_AsType PyLong_AsUnsignedLong\r
3ec97ca4
DM
1261#endif\r
1262\r
1263/* Unfortunately, we can't store python objects in udata, because\r
1264 * kevents in the kernel can be removed without warning, which would\r
1265 * forever lose the refcount on the object stored with it.\r
1266 */\r
1267\r
1268#define KQ_OFF(x) offsetof(kqueue_event_Object, x)\r
1269static struct PyMemberDef kqueue_event_members[] = {\r
1270 {"ident", IDENT_TYPE, KQ_OFF(e.ident)},\r
1271 {"filter", T_SHORT, KQ_OFF(e.filter)},\r
1272 {"flags", T_USHORT, KQ_OFF(e.flags)},\r
1273 {"fflags", T_UINT, KQ_OFF(e.fflags)},\r
1274 {"data", DATA_TYPE, KQ_OFF(e.data)},\r
1275 {"udata", T_UINTPTRT, KQ_OFF(e.udata)},\r
1276 {NULL} /* Sentinel */\r
1277};\r
1278#undef KQ_OFF\r
1279\r
1280static PyObject *\r
1281\r
1282kqueue_event_repr(kqueue_event_Object *s)\r
1283{\r
1284 char buf[1024];\r
1285 PyOS_snprintf(\r
1286 buf, sizeof(buf),\r
1287 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "\r
1288 "data=0x%zd udata=%p>",\r
1289 (size_t)(s->e.ident), s->e.filter, s->e.flags,\r
1290 s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);\r
1291 return PyString_FromString(buf);\r
1292}\r
1293\r
1294static int\r
1295kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)\r
1296{\r
1297 PyObject *pfd;\r
1298 static char *kwlist[] = {"ident", "filter", "flags", "fflags",\r
1299 "data", "udata", NULL};\r
1300 static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";\r
1301\r
1302 EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */\r
1303\r
1304 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,\r
1305 &pfd, &(self->e.filter), &(self->e.flags),\r
1306 &(self->e.fflags), &(self->e.data), &(self->e.udata))) {\r
1307 return -1;\r
1308 }\r
1309\r
1310 if (PyLong_Check(pfd)\r
1311#if IDENT_TYPE == T_UINT\r
d11973f1 1312 && PyLong_AsUnsignedLong(pfd) <= UINT_MAX\r
3ec97ca4
DM
1313#endif\r
1314 ) {\r
1315 self->e.ident = IDENT_AsType(pfd);\r
1316 }\r
1317 else {\r
1318 self->e.ident = PyObject_AsFileDescriptor(pfd);\r
1319 }\r
1320 if (PyErr_Occurred()) {\r
1321 return -1;\r
1322 }\r
1323 return 0;\r
1324}\r
1325\r
1326static PyObject *\r
1327kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,\r
1328 int op)\r
1329{\r
1330 Py_intptr_t result = 0;\r
1331\r
1332 if (!kqueue_event_Check(o)) {\r
1333 if (op == Py_EQ || op == Py_NE) {\r
1334 PyObject *res = op == Py_EQ ? Py_False : Py_True;\r
1335 Py_INCREF(res);\r
1336 return res;\r
1337 }\r
1338 PyErr_Format(PyExc_TypeError,\r
1339 "can't compare %.200s to %.200s",\r
1340 Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);\r
1341 return NULL;\r
1342 }\r
1343 if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&\r
1344 ((result = s->e.filter - o->e.filter) == 0) &&\r
1345 ((result = s->e.flags - o->e.flags) == 0) &&\r
1346 ((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&\r
1347 ((result = s->e.data - o->e.data) == 0) &&\r
1348 ((result = s->e.udata - o->e.udata) == 0)\r
1349 ) {\r
1350 result = 0;\r
1351 }\r
1352\r
1353 switch (op) {\r
1354 case Py_EQ:\r
1355 result = (result == 0);\r
1356 break;\r
1357 case Py_NE:\r
1358 result = (result != 0);\r
1359 break;\r
1360 case Py_LE:\r
1361 result = (result <= 0);\r
1362 break;\r
1363 case Py_GE:\r
1364 result = (result >= 0);\r
1365 break;\r
1366 case Py_LT:\r
1367 result = (result < 0);\r
1368 break;\r
1369 case Py_GT:\r
1370 result = (result > 0);\r
1371 break;\r
1372 }\r
1373 return PyBool_FromLong((long)result);\r
1374}\r
1375\r
1376static PyTypeObject kqueue_event_Type = {\r
1377 PyVarObject_HEAD_INIT(NULL, 0)\r
1378 "select.kevent", /* tp_name */\r
1379 sizeof(kqueue_event_Object), /* tp_basicsize */\r
1380 0, /* tp_itemsize */\r
1381 0, /* tp_dealloc */\r
1382 0, /* tp_print */\r
1383 0, /* tp_getattr */\r
1384 0, /* tp_setattr */\r
1385 0, /* tp_compare */\r
1386 (reprfunc)kqueue_event_repr, /* tp_repr */\r
1387 0, /* tp_as_number */\r
1388 0, /* tp_as_sequence */\r
1389 0, /* tp_as_mapping */\r
1390 0, /* tp_hash */\r
1391 0, /* tp_call */\r
1392 0, /* tp_str */\r
1393 0, /* tp_getattro */\r
1394 0, /* tp_setattro */\r
1395 0, /* tp_as_buffer */\r
1396 Py_TPFLAGS_DEFAULT, /* tp_flags */\r
1397 kqueue_event_doc, /* tp_doc */\r
1398 0, /* tp_traverse */\r
1399 0, /* tp_clear */\r
1400 (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */\r
1401 0, /* tp_weaklistoffset */\r
1402 0, /* tp_iter */\r
1403 0, /* tp_iternext */\r
1404 0, /* tp_methods */\r
1405 kqueue_event_members, /* tp_members */\r
1406 0, /* tp_getset */\r
1407 0, /* tp_base */\r
1408 0, /* tp_dict */\r
1409 0, /* tp_descr_get */\r
1410 0, /* tp_descr_set */\r
1411 0, /* tp_dictoffset */\r
1412 (initproc)kqueue_event_init, /* tp_init */\r
1413 0, /* tp_alloc */\r
1414 0, /* tp_new */\r
1415 0, /* tp_free */\r
1416};\r
1417\r
1418static PyObject *\r
1419kqueue_queue_err_closed(void)\r
1420{\r
1421 PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue fd");\r
1422 return NULL;\r
1423}\r
1424\r
1425static int\r
1426kqueue_queue_internal_close(kqueue_queue_Object *self)\r
1427{\r
1428 int save_errno = 0;\r
1429 if (self->kqfd >= 0) {\r
1430 int kqfd = self->kqfd;\r
1431 self->kqfd = -1;\r
1432 Py_BEGIN_ALLOW_THREADS\r
1433 if (close(kqfd) < 0)\r
1434 save_errno = errno;\r
1435 Py_END_ALLOW_THREADS\r
1436 }\r
1437 return save_errno;\r
1438}\r
1439\r
1440static PyObject *\r
1441newKqueue_Object(PyTypeObject *type, SOCKET fd)\r
1442{\r
1443 kqueue_queue_Object *self;\r
1444 assert(type != NULL && type->tp_alloc != NULL);\r
1445 self = (kqueue_queue_Object *) type->tp_alloc(type, 0);\r
1446 if (self == NULL) {\r
1447 return NULL;\r
1448 }\r
1449\r
1450 if (fd == -1) {\r
1451 Py_BEGIN_ALLOW_THREADS\r
1452 self->kqfd = kqueue();\r
1453 Py_END_ALLOW_THREADS\r
1454 }\r
1455 else {\r
1456 self->kqfd = fd;\r
1457 }\r
1458 if (self->kqfd < 0) {\r
1459 Py_DECREF(self);\r
1460 PyErr_SetFromErrno(PyExc_IOError);\r
1461 return NULL;\r
1462 }\r
1463 return (PyObject *)self;\r
1464}\r
1465\r
1466static PyObject *\r
1467kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
1468{\r
1469\r
1470 if ((args != NULL && PyObject_Size(args)) ||\r
1471 (kwds != NULL && PyObject_Size(kwds))) {\r
1472 PyErr_SetString(PyExc_ValueError,\r
1473 "select.kqueue doesn't accept arguments");\r
1474 return NULL;\r
1475 }\r
1476\r
1477 return newKqueue_Object(type, -1);\r
1478}\r
1479\r
1480static void\r
1481kqueue_queue_dealloc(kqueue_queue_Object *self)\r
1482{\r
1483 kqueue_queue_internal_close(self);\r
1484 Py_TYPE(self)->tp_free(self);\r
1485}\r
1486\r
1487static PyObject*\r
1488kqueue_queue_close(kqueue_queue_Object *self)\r
1489{\r
1490 errno = kqueue_queue_internal_close(self);\r
1491 if (errno < 0) {\r
1492 PyErr_SetFromErrno(PyExc_IOError);\r
1493 return NULL;\r
1494 }\r
1495 Py_RETURN_NONE;\r
1496}\r
1497\r
1498PyDoc_STRVAR(kqueue_queue_close_doc,\r
1499"close() -> None\n\\r
1500\n\\r
1501Close the kqueue control file descriptor. Further operations on the kqueue\n\\r
1502object will raise an exception.");\r
1503\r
1504static PyObject*\r
1505kqueue_queue_get_closed(kqueue_queue_Object *self)\r
1506{\r
1507 if (self->kqfd < 0)\r
1508 Py_RETURN_TRUE;\r
1509 else\r
1510 Py_RETURN_FALSE;\r
1511}\r
1512\r
1513static PyObject*\r
1514kqueue_queue_fileno(kqueue_queue_Object *self)\r
1515{\r
1516 if (self->kqfd < 0)\r
1517 return kqueue_queue_err_closed();\r
1518 return PyInt_FromLong(self->kqfd);\r
1519}\r
1520\r
1521PyDoc_STRVAR(kqueue_queue_fileno_doc,\r
1522"fileno() -> int\n\\r
1523\n\\r
1524Return the kqueue control file descriptor.");\r
1525\r
1526static PyObject*\r
1527kqueue_queue_fromfd(PyObject *cls, PyObject *args)\r
1528{\r
1529 SOCKET fd;\r
1530\r
1531 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))\r
1532 return NULL;\r
1533\r
1534 return newKqueue_Object((PyTypeObject*)cls, fd);\r
1535}\r
1536\r
1537PyDoc_STRVAR(kqueue_queue_fromfd_doc,\r
1538"fromfd(fd) -> kqueue\n\\r
1539\n\\r
1540Create a kqueue object from a given control fd.");\r
1541\r
1542static PyObject *\r
1543kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)\r
1544{\r
1545 int nevents = 0;\r
1546 int gotevents = 0;\r
1547 int nchanges = 0;\r
1548 int i = 0;\r
1549 PyObject *otimeout = NULL;\r
1550 PyObject *ch = NULL;\r
1551 PyObject *it = NULL, *ei = NULL;\r
1552 PyObject *result = NULL;\r
1553 struct kevent *evl = NULL;\r
1554 struct kevent *chl = NULL;\r
1555 struct timespec timeoutspec;\r
1556 struct timespec *ptimeoutspec;\r
1557\r
1558 if (self->kqfd < 0)\r
1559 return kqueue_queue_err_closed();\r
1560\r
1561 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))\r
1562 return NULL;\r
1563\r
1564 if (nevents < 0) {\r
1565 PyErr_Format(PyExc_ValueError,\r
1566 "Length of eventlist must be 0 or positive, got %d",\r
1567 nevents);\r
1568 return NULL;\r
1569 }\r
1570\r
1571 if (otimeout == Py_None || otimeout == NULL) {\r
1572 ptimeoutspec = NULL;\r
1573 }\r
1574 else if (PyNumber_Check(otimeout)) {\r
1575 double timeout;\r
1576 long seconds;\r
1577\r
1578 timeout = PyFloat_AsDouble(otimeout);\r
1579 if (timeout == -1 && PyErr_Occurred())\r
1580 return NULL;\r
1581 if (timeout > (double)LONG_MAX) {\r
1582 PyErr_SetString(PyExc_OverflowError,\r
1583 "timeout period too long");\r
1584 return NULL;\r
1585 }\r
1586 if (timeout < 0) {\r
1587 PyErr_SetString(PyExc_ValueError,\r
1588 "timeout must be positive or None");\r
1589 return NULL;\r
1590 }\r
1591\r
1592 seconds = (long)timeout;\r
1593 timeout = timeout - (double)seconds;\r
1594 timeoutspec.tv_sec = seconds;\r
1595 timeoutspec.tv_nsec = (long)(timeout * 1E9);\r
1596 ptimeoutspec = &timeoutspec;\r
1597 }\r
1598 else {\r
1599 PyErr_Format(PyExc_TypeError,\r
1600 "timeout argument must be an number "\r
1601 "or None, got %.200s",\r
1602 Py_TYPE(otimeout)->tp_name);\r
1603 return NULL;\r
1604 }\r
1605\r
1606 if (ch != NULL && ch != Py_None) {\r
1607 it = PyObject_GetIter(ch);\r
1608 if (it == NULL) {\r
1609 PyErr_SetString(PyExc_TypeError,\r
1610 "changelist is not iterable");\r
1611 return NULL;\r
1612 }\r
1613 nchanges = PyObject_Size(ch);\r
1614 if (nchanges < 0) {\r
1615 goto error;\r
1616 }\r
1617\r
1618 chl = PyMem_New(struct kevent, nchanges);\r
1619 if (chl == NULL) {\r
1620 PyErr_NoMemory();\r
1621 goto error;\r
1622 }\r
1623 i = 0;\r
1624 while ((ei = PyIter_Next(it)) != NULL) {\r
1625 if (!kqueue_event_Check(ei)) {\r
1626 Py_DECREF(ei);\r
1627 PyErr_SetString(PyExc_TypeError,\r
1628 "changelist must be an iterable of "\r
1629 "select.kevent objects");\r
1630 goto error;\r
1631 } else {\r
1632 chl[i++] = ((kqueue_event_Object *)ei)->e;\r
1633 }\r
1634 Py_DECREF(ei);\r
1635 }\r
1636 }\r
1637 Py_CLEAR(it);\r
1638\r
1639 /* event list */\r
1640 if (nevents) {\r
1641 evl = PyMem_New(struct kevent, nevents);\r
1642 if (evl == NULL) {\r
1643 PyErr_NoMemory();\r
1644 goto error;\r
1645 }\r
1646 }\r
1647\r
1648 Py_BEGIN_ALLOW_THREADS\r
1649 gotevents = kevent(self->kqfd, chl, nchanges,\r
1650 evl, nevents, ptimeoutspec);\r
1651 Py_END_ALLOW_THREADS\r
1652\r
1653 if (gotevents == -1) {\r
1654 PyErr_SetFromErrno(PyExc_OSError);\r
1655 goto error;\r
1656 }\r
1657\r
1658 result = PyList_New(gotevents);\r
1659 if (result == NULL) {\r
1660 goto error;\r
1661 }\r
1662\r
1663 for (i = 0; i < gotevents; i++) {\r
1664 kqueue_event_Object *ch;\r
1665\r
1666 ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);\r
1667 if (ch == NULL) {\r
1668 goto error;\r
1669 }\r
1670 ch->e = evl[i];\r
1671 PyList_SET_ITEM(result, i, (PyObject *)ch);\r
1672 }\r
1673 PyMem_Free(chl);\r
1674 PyMem_Free(evl);\r
1675 return result;\r
1676\r
1677 error:\r
1678 PyMem_Free(chl);\r
1679 PyMem_Free(evl);\r
1680 Py_XDECREF(result);\r
1681 Py_XDECREF(it);\r
1682 return NULL;\r
1683}\r
1684\r
1685PyDoc_STRVAR(kqueue_queue_control_doc,\r
1686"control(changelist, max_events[, timeout=None]) -> eventlist\n\\r
1687\n\\r
1688Calls the kernel kevent function.\n\\r
1689- changelist must be a list of kevent objects describing the changes\n\\r
1690 to be made to the kernel's watch list or None.\n\\r
1691- max_events lets you specify the maximum number of events that the\n\\r
1692 kernel will return.\n\\r
1693- timeout is the maximum time to wait in seconds, or else None,\n\\r
1694 to wait forever. timeout accepts floats for smaller timeouts, too.");\r
1695\r
1696\r
1697static PyMethodDef kqueue_queue_methods[] = {\r
1698 {"fromfd", (PyCFunction)kqueue_queue_fromfd,\r
1699 METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},\r
1700 {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,\r
1701 kqueue_queue_close_doc},\r
1702 {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,\r
1703 kqueue_queue_fileno_doc},\r
1704 {"control", (PyCFunction)kqueue_queue_control,\r
1705 METH_VARARGS , kqueue_queue_control_doc},\r
1706 {NULL, NULL},\r
1707};\r
1708\r
1709static PyGetSetDef kqueue_queue_getsetlist[] = {\r
1710 {"closed", (getter)kqueue_queue_get_closed, NULL,\r
1711 "True if the kqueue handler is closed"},\r
1712 {0},\r
1713};\r
1714\r
1715PyDoc_STRVAR(kqueue_queue_doc,\r
1716"Kqueue syscall wrapper.\n\\r
1717\n\\r
1718For example, to start watching a socket for input:\n\\r
1719>>> kq = kqueue()\n\\r
1720>>> sock = socket()\n\\r
1721>>> sock.connect((host, port))\n\\r
1722>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\\r
1723\n\\r
1724To wait one second for it to become writeable:\n\\r
1725>>> kq.control(None, 1, 1000)\n\\r
1726\n\\r
1727To stop listening:\n\\r
1728>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");\r
1729\r
1730static PyTypeObject kqueue_queue_Type = {\r
1731 PyVarObject_HEAD_INIT(NULL, 0)\r
1732 "select.kqueue", /* tp_name */\r
1733 sizeof(kqueue_queue_Object), /* tp_basicsize */\r
1734 0, /* tp_itemsize */\r
1735 (destructor)kqueue_queue_dealloc, /* tp_dealloc */\r
1736 0, /* tp_print */\r
1737 0, /* tp_getattr */\r
1738 0, /* tp_setattr */\r
1739 0, /* tp_compare */\r
1740 0, /* tp_repr */\r
1741 0, /* tp_as_number */\r
1742 0, /* tp_as_sequence */\r
1743 0, /* tp_as_mapping */\r
1744 0, /* tp_hash */\r
1745 0, /* tp_call */\r
1746 0, /* tp_str */\r
1747 0, /* tp_getattro */\r
1748 0, /* tp_setattro */\r
1749 0, /* tp_as_buffer */\r
1750 Py_TPFLAGS_DEFAULT, /* tp_flags */\r
1751 kqueue_queue_doc, /* tp_doc */\r
1752 0, /* tp_traverse */\r
1753 0, /* tp_clear */\r
1754 0, /* tp_richcompare */\r
1755 0, /* tp_weaklistoffset */\r
1756 0, /* tp_iter */\r
1757 0, /* tp_iternext */\r
1758 kqueue_queue_methods, /* tp_methods */\r
1759 0, /* tp_members */\r
1760 kqueue_queue_getsetlist, /* tp_getset */\r
1761 0, /* tp_base */\r
1762 0, /* tp_dict */\r
1763 0, /* tp_descr_get */\r
1764 0, /* tp_descr_set */\r
1765 0, /* tp_dictoffset */\r
1766 0, /* tp_init */\r
1767 0, /* tp_alloc */\r
1768 kqueue_queue_new, /* tp_new */\r
1769 0, /* tp_free */\r
1770};\r
1771\r
1772#endif /* HAVE_KQUEUE */\r
1773/* ************************************************************************ */\r
1774\r
1775PyDoc_STRVAR(select_doc,\r
1776"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\\r
1777\n\\r
1778Wait until one or more file descriptors are ready for some kind of I/O.\n\\r
1779The first three arguments are sequences of file descriptors to be waited for:\n\\r
1780rlist -- wait until ready for reading\n\\r
1781wlist -- wait until ready for writing\n\\r
1782xlist -- wait for an ``exceptional condition''\n\\r
1783If only one kind of condition is required, pass [] for the other lists.\n\\r
1784A file descriptor is either a socket or file object, or a small integer\n\\r
1785gotten from a fileno() method call on one of those.\n\\r
1786\n\\r
1787The optional 4th argument specifies a timeout in seconds; it may be\n\\r
1788a floating point number to specify fractions of seconds. If it is absent\n\\r
1789or None, the call will never time out.\n\\r
1790\n\\r
1791The return value is a tuple of three lists corresponding to the first three\n\\r
1792arguments; each contains the subset of the corresponding file descriptors\n\\r
1793that are ready.\n\\r
1794\n\\r
1795*** IMPORTANT NOTICE ***\n\\r
1796On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\\r
1797descriptors can be used.");\r
1798\r
1799static PyMethodDef select_methods[] = {\r
1800 {"select", select_select, METH_VARARGS, select_doc},\r
1801#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)\r
1802 {"poll", select_poll, METH_NOARGS, poll_doc},\r
1803#endif /* HAVE_POLL */\r
1804 {0, 0}, /* sentinel */\r
1805};\r
1806\r
1807PyDoc_STRVAR(module_doc,\r
1808"This module supports asynchronous I/O on multiple file descriptors.\n\\r
1809\n\\r
1810*** IMPORTANT NOTICE ***\n\\r
1811On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");\r
1812\r
1813PyMODINIT_FUNC\r
1814initselect(void)\r
1815{\r
1816 PyObject *m;\r
1817 m = Py_InitModule3("select", select_methods, module_doc);\r
1818 if (m == NULL)\r
1819 return;\r
1820\r
1821 SelectError = PyErr_NewException("select.error", NULL, NULL);\r
1822 Py_INCREF(SelectError);\r
1823 PyModule_AddObject(m, "error", SelectError);\r
1824\r
1825#ifdef PIPE_BUF\r
1826#ifdef HAVE_BROKEN_PIPE_BUF\r
1827#undef PIPE_BUF\r
1828#define PIPE_BUF 512\r
1829#endif\r
1830 PyModule_AddIntConstant(m, "PIPE_BUF", PIPE_BUF);\r
1831#endif\r
1832\r
1833#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)\r
1834#ifdef __APPLE__\r
1835 if (select_have_broken_poll()) {\r
1836 if (PyObject_DelAttrString(m, "poll") == -1) {\r
1837 PyErr_Clear();\r
1838 }\r
1839 } else {\r
1840#else\r
1841 {\r
1842#endif\r
1843 Py_TYPE(&poll_Type) = &PyType_Type;\r
1844 PyModule_AddIntConstant(m, "POLLIN", POLLIN);\r
1845 PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);\r
1846 PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);\r
1847 PyModule_AddIntConstant(m, "POLLERR", POLLERR);\r
1848 PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);\r
1849 PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);\r
1850\r
1851#ifdef POLLRDNORM\r
1852 PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);\r
1853#endif\r
1854#ifdef POLLRDBAND\r
1855 PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);\r
1856#endif\r
1857#ifdef POLLWRNORM\r
1858 PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);\r
1859#endif\r
1860#ifdef POLLWRBAND\r
1861 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);\r
1862#endif\r
1863#ifdef POLLMSG\r
1864 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);\r
1865#endif\r
1866 }\r
1867#endif /* HAVE_POLL */\r
1868\r
1869#ifdef HAVE_EPOLL\r
1870 Py_TYPE(&pyEpoll_Type) = &PyType_Type;\r
1871 if (PyType_Ready(&pyEpoll_Type) < 0)\r
1872 return;\r
1873\r
1874 Py_INCREF(&pyEpoll_Type);\r
1875 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);\r
1876\r
1877 PyModule_AddIntConstant(m, "EPOLLIN", EPOLLIN);\r
1878 PyModule_AddIntConstant(m, "EPOLLOUT", EPOLLOUT);\r
1879 PyModule_AddIntConstant(m, "EPOLLPRI", EPOLLPRI);\r
1880 PyModule_AddIntConstant(m, "EPOLLERR", EPOLLERR);\r
1881 PyModule_AddIntConstant(m, "EPOLLHUP", EPOLLHUP);\r
1882 PyModule_AddIntConstant(m, "EPOLLET", EPOLLET);\r
1883#ifdef EPOLLONESHOT\r
1884 /* Kernel 2.6.2+ */\r
1885 PyModule_AddIntConstant(m, "EPOLLONESHOT", EPOLLONESHOT);\r
1886#endif\r
1887 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */\r
1888 PyModule_AddIntConstant(m, "EPOLLRDNORM", EPOLLRDNORM);\r
1889 PyModule_AddIntConstant(m, "EPOLLRDBAND", EPOLLRDBAND);\r
1890 PyModule_AddIntConstant(m, "EPOLLWRNORM", EPOLLWRNORM);\r
1891 PyModule_AddIntConstant(m, "EPOLLWRBAND", EPOLLWRBAND);\r
1892 PyModule_AddIntConstant(m, "EPOLLMSG", EPOLLMSG);\r
1893#endif /* HAVE_EPOLL */\r
1894\r
1895#ifdef HAVE_KQUEUE\r
1896 kqueue_event_Type.tp_new = PyType_GenericNew;\r
1897 Py_TYPE(&kqueue_event_Type) = &PyType_Type;\r
1898 if(PyType_Ready(&kqueue_event_Type) < 0)\r
1899 return;\r
1900\r
1901 Py_INCREF(&kqueue_event_Type);\r
1902 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);\r
1903\r
1904 Py_TYPE(&kqueue_queue_Type) = &PyType_Type;\r
1905 if(PyType_Ready(&kqueue_queue_Type) < 0)\r
1906 return;\r
1907 Py_INCREF(&kqueue_queue_Type);\r
1908 PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);\r
1909\r
1910 /* event filters */\r
1911 PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);\r
1912 PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);\r
1913 PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);\r
1914 PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);\r
1915 PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);\r
1916#ifdef EVFILT_NETDEV\r
1917 PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);\r
1918#endif\r
1919 PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);\r
1920 PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);\r
1921\r
1922 /* event flags */\r
1923 PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);\r
1924 PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);\r
1925 PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);\r
1926 PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);\r
1927 PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);\r
1928 PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);\r
1929\r
1930 PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);\r
1931 PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);\r
1932\r
1933 PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);\r
1934 PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);\r
1935\r
1936 /* READ WRITE filter flag */\r
1937 PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);\r
1938\r
1939 /* VNODE filter flags */\r
1940 PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);\r
1941 PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);\r
1942 PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);\r
1943 PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);\r
1944 PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);\r
1945 PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);\r
1946 PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);\r
1947\r
1948 /* PROC filter flags */\r
1949 PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);\r
1950 PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);\r
1951 PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);\r
1952 PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);\r
1953 PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);\r
1954\r
1955 PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);\r
1956 PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);\r
1957 PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);\r
1958\r
1959 /* NETDEV filter flags */\r
1960#ifdef EVFILT_NETDEV\r
1961 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);\r
1962 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);\r
1963 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);\r
1964#endif\r
1965\r
1966#endif /* HAVE_KQUEUE */\r
1967}\r