1 /* select - Module containing unix select(2) call.
2 Under Unix, the file descriptors are small integers.
3 Under Win32, select only exists for sockets, and sockets may
4 have any value except INVALID_SOCKET.
5 Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything
10 #include <structmember.h>
13 /* Perform runtime testing for a broken poll on OSX to make it easier
14 * to use the same binary on multiple releases of the OS.
16 #undef HAVE_BROKEN_POLL
19 /* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.
20 64 is too small (too many people have bumped into that limit).
22 Users who want even more than the boosted limit should #define
23 FD_SETSIZE higher before this; e.g., via compiler /D switch.
25 #if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
26 #define FD_SETSIZE 512
29 #if defined(HAVE_POLL_H)
31 #elif defined(HAVE_SYS_POLL_H)
36 /* This is missing from unistd.h */
37 extern void bzero(void *, int);
40 #ifdef HAVE_SYS_TYPES_H
41 #include <sys/types.h>
44 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
50 # include <winsock2.h>
54 # include <net/socket.h>
60 static PyObject
*SelectError
;
62 /* list of Python objects and their file descriptor */
64 PyObject
*obj
; /* owned reference */
66 int sentinel
; /* -1 == sentinel */
70 reap_obj(pylist fd2obj
[FD_SETSIZE
+ 1])
73 for (i
= 0; i
< FD_SETSIZE
+ 1 && fd2obj
[i
].sentinel
>= 0; i
++) {
74 Py_CLEAR(fd2obj
[i
].obj
);
76 fd2obj
[0].sentinel
= -1;
80 /* returns -1 and sets the Python exception if an error occurred, otherwise
84 seq2set(PyObject
*seq
, fd_set
*set
, pylist fd2obj
[FD_SETSIZE
+ 1])
89 PyObject
* fast_seq
= NULL
;
92 fd2obj
[0].obj
= (PyObject
*)0; /* set list to zero size */
95 fast_seq
= PySequence_Fast(seq
, "arguments 1-3 must be sequences");
99 for (i
= 0; i
< PySequence_Fast_GET_SIZE(fast_seq
); i
++) {
102 /* any intervening fileno() calls could decr this refcnt */
103 if (!(o
= PySequence_Fast_GET_ITEM(fast_seq
, i
)))
107 v
= PyObject_AsFileDescriptor( o
);
108 if (v
== -1) goto finally
;
110 #if defined(_MSC_VER)
111 max
= 0; /* not used for Win32 */
112 #else /* !_MSC_VER */
113 if (!_PyIsSelectable_fd(v
)) {
114 PyErr_SetString(PyExc_ValueError
,
115 "filedescriptor out of range in select()");
120 #endif /* _MSC_VER */
123 /* add object and its file descriptor to the list */
124 if (index
>= FD_SETSIZE
) {
125 PyErr_SetString(PyExc_ValueError
,
126 "too many file descriptors in select()");
129 fd2obj
[index
].obj
= o
;
130 fd2obj
[index
].fd
= v
;
131 fd2obj
[index
].sentinel
= 0;
132 fd2obj
[++index
].sentinel
= -1;
143 /* returns NULL and sets the Python exception if an error occurred */
145 set2list(fd_set
*set
, pylist fd2obj
[FD_SETSIZE
+ 1])
151 for (j
= 0; fd2obj
[j
].sentinel
>= 0; j
++) {
152 if (FD_ISSET(fd2obj
[j
].fd
, set
))
155 list
= PyList_New(count
);
160 for (j
= 0; fd2obj
[j
].sentinel
>= 0; j
++) {
162 if (FD_ISSET(fd
, set
)) {
164 fd2obj
[j
].obj
= NULL
;
165 /* transfer ownership */
166 if (PyList_SetItem(list
, i
, o
) < 0)
178 #undef SELECT_USES_HEAP
179 #if FD_SETSIZE > 1024
180 #define SELECT_USES_HEAP
181 #endif /* FD_SETSIZE > 1024 */
184 select_select(PyObject
*self
, PyObject
*args
)
186 #ifdef SELECT_USES_HEAP
187 pylist
*rfd2obj
, *wfd2obj
, *efd2obj
;
188 #else /* !SELECT_USES_HEAP */
189 /* XXX: All this should probably be implemented as follows:
190 * - find the highest descriptor we're interested in
193 * See: Stevens, APitUE, $12.5.1
195 pylist rfd2obj
[FD_SETSIZE
+ 1];
196 pylist wfd2obj
[FD_SETSIZE
+ 1];
197 pylist efd2obj
[FD_SETSIZE
+ 1];
198 #endif /* SELECT_USES_HEAP */
199 PyObject
*ifdlist
, *ofdlist
, *efdlist
;
200 PyObject
*ret
= NULL
;
201 PyObject
*tout
= Py_None
;
202 fd_set ifdset
, ofdset
, efdset
;
204 struct timeval tv
, *tvp
;
206 int imax
, omax
, emax
, max
;
209 /* convert arguments */
210 if (!PyArg_UnpackTuple(args
, "select", 3, 4,
211 &ifdlist
, &ofdlist
, &efdlist
, &tout
))
215 tvp
= (struct timeval
*)0;
216 else if (!PyNumber_Check(tout
)) {
217 PyErr_SetString(PyExc_TypeError
,
218 "timeout must be a float or None");
222 timeout
= PyFloat_AsDouble(tout
);
223 if (timeout
== -1 && PyErr_Occurred())
225 if (timeout
> (double)LONG_MAX
) {
226 PyErr_SetString(PyExc_OverflowError
,
227 "timeout period too long");
230 seconds
= (long)timeout
;
231 timeout
= timeout
- (double)seconds
;
233 tv
.tv_usec
= (long)(timeout
* 1E6
);
238 #ifdef SELECT_USES_HEAP
239 /* Allocate memory for the lists */
240 rfd2obj
= PyMem_NEW(pylist
, FD_SETSIZE
+ 1);
241 wfd2obj
= PyMem_NEW(pylist
, FD_SETSIZE
+ 1);
242 efd2obj
= PyMem_NEW(pylist
, FD_SETSIZE
+ 1);
243 if (rfd2obj
== NULL
|| wfd2obj
== NULL
|| efd2obj
== NULL
) {
244 if (rfd2obj
) PyMem_DEL(rfd2obj
);
245 if (wfd2obj
) PyMem_DEL(wfd2obj
);
246 if (efd2obj
) PyMem_DEL(efd2obj
);
247 return PyErr_NoMemory();
249 #endif /* SELECT_USES_HEAP */
250 /* Convert sequences to fd_sets, and get maximum fd number
251 * propagates the Python exception set in seq2set()
253 rfd2obj
[0].sentinel
= -1;
254 wfd2obj
[0].sentinel
= -1;
255 efd2obj
[0].sentinel
= -1;
256 if ((imax
=seq2set(ifdlist
, &ifdset
, rfd2obj
)) < 0)
258 if ((omax
=seq2set(ofdlist
, &ofdset
, wfd2obj
)) < 0)
260 if ((emax
=seq2set(efdlist
, &efdset
, efd2obj
)) < 0)
263 if (omax
> max
) max
= omax
;
264 if (emax
> max
) max
= emax
;
266 Py_BEGIN_ALLOW_THREADS
267 n
= select(max
, &ifdset
, &ofdset
, &efdset
, tvp
);
271 if (n
== SOCKET_ERROR
) {
272 PyErr_SetExcFromWindowsErr(SelectError
, WSAGetLastError());
276 PyErr_SetFromErrno(SelectError
);
280 /* any of these three calls can raise an exception. it's more
281 convenient to test for this after all three calls... but
284 ifdlist
= set2list(&ifdset
, rfd2obj
);
285 ofdlist
= set2list(&ofdset
, wfd2obj
);
286 efdlist
= set2list(&efdset
, efd2obj
);
287 if (PyErr_Occurred())
290 ret
= PyTuple_Pack(3, ifdlist
, ofdlist
, efdlist
);
301 #ifdef SELECT_USES_HEAP
305 #endif /* SELECT_USES_HEAP */
309 #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
323 static PyTypeObject poll_Type
;
325 /* Update the malloc'ed array of pollfds to match the dictionary
326 contained within a pollObject. Return 1 on success, 0 on an error.
330 update_ufd_array(pollObject
*self
)
333 PyObject
*key
, *value
;
334 struct pollfd
*old_ufds
= self
->ufds
;
336 self
->ufd_len
= PyDict_Size(self
->dict
);
337 PyMem_RESIZE(self
->ufds
, struct pollfd
, self
->ufd_len
);
338 if (self
->ufds
== NULL
) {
339 self
->ufds
= old_ufds
;
345 while (PyDict_Next(self
->dict
, &pos
, &key
, &value
)) {
346 assert(i
< self
->ufd_len
);
348 self
->ufds
[i
].fd
= (int)PyInt_AsLong(key
);
349 self
->ufds
[i
].events
= (short)(unsigned short)PyInt_AsLong(value
);
352 assert(i
== self
->ufd_len
);
353 self
->ufd_uptodate
= 1;
358 ushort_converter(PyObject
*obj
, void *ptr
)
362 uval
= PyLong_AsUnsignedLong(obj
);
363 if (uval
== (unsigned long)-1 && PyErr_Occurred())
365 if (uval
> USHRT_MAX
) {
366 PyErr_SetString(PyExc_OverflowError
,
367 "Python int too large for C unsigned short");
371 *(unsigned short *)ptr
= Py_SAFE_DOWNCAST(uval
, unsigned long, unsigned short);
375 PyDoc_STRVAR(poll_register_doc
,
376 "register(fd [, eventmask] ) -> None\n\n\
377 Register a file descriptor with the polling object.\n\
378 fd -- either an integer, or an object with a fileno() method returning an\n\
380 events -- an optional bitmask describing the type of events to check for");
383 poll_register(pollObject
*self
, PyObject
*args
)
385 PyObject
*o
, *key
, *value
;
387 unsigned short events
= POLLIN
| POLLPRI
| POLLOUT
;
390 if (!PyArg_ParseTuple(args
, "O|O&:register", &o
, ushort_converter
, &events
))
393 fd
= PyObject_AsFileDescriptor(o
);
394 if (fd
== -1) return NULL
;
396 /* Add entry to the internal dictionary: the key is the
397 file descriptor, and the value is the event mask. */
398 key
= PyInt_FromLong(fd
);
401 value
= PyInt_FromLong(events
);
406 err
= PyDict_SetItem(self
->dict
, key
, value
);
412 self
->ufd_uptodate
= 0;
418 PyDoc_STRVAR(poll_modify_doc
,
419 "modify(fd, eventmask) -> None\n\n\
420 Modify an already registered file descriptor.\n\
421 fd -- either an integer, or an object with a fileno() method returning an\n\
423 events -- an optional bitmask describing the type of events to check for");
426 poll_modify(pollObject
*self
, PyObject
*args
)
428 PyObject
*o
, *key
, *value
;
430 unsigned short events
;
433 if (!PyArg_ParseTuple(args
, "OO&:modify", &o
, ushort_converter
, &events
))
436 fd
= PyObject_AsFileDescriptor(o
);
437 if (fd
== -1) return NULL
;
439 /* Modify registered fd */
440 key
= PyInt_FromLong(fd
);
443 if (PyDict_GetItem(self
->dict
, key
) == NULL
) {
445 PyErr_SetFromErrno(PyExc_IOError
);
448 value
= PyInt_FromLong(events
);
453 err
= PyDict_SetItem(self
->dict
, key
, value
);
459 self
->ufd_uptodate
= 0;
466 PyDoc_STRVAR(poll_unregister_doc
,
467 "unregister(fd) -> None\n\n\
468 Remove a file descriptor being tracked by the polling object.");
471 poll_unregister(pollObject
*self
, PyObject
*o
)
476 fd
= PyObject_AsFileDescriptor( o
);
480 /* Check whether the fd is already in the array */
481 key
= PyInt_FromLong(fd
);
485 if (PyDict_DelItem(self
->dict
, key
) == -1) {
487 /* This will simply raise the KeyError set by PyDict_DelItem
488 if the file descriptor isn't registered. */
493 self
->ufd_uptodate
= 0;
499 PyDoc_STRVAR(poll_poll_doc
,
500 "poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
501 Polls the set of registered file descriptors, returning a list containing \n\
502 any descriptors that have events or errors to report.");
505 poll_poll(pollObject
*self
, PyObject
*args
)
507 PyObject
*result_list
= NULL
, *tout
= NULL
;
508 int timeout
= 0, poll_result
, i
, j
;
509 PyObject
*value
= NULL
, *num
= NULL
;
511 if (!PyArg_UnpackTuple(args
, "poll", 0, 1, &tout
)) {
515 /* Check values for timeout */
516 if (tout
== NULL
|| tout
== Py_None
)
518 else if (!PyNumber_Check(tout
)) {
519 PyErr_SetString(PyExc_TypeError
,
520 "timeout must be an integer or None");
524 tout
= PyNumber_Int(tout
);
527 timeout
= _PyInt_AsInt(tout
);
529 if (timeout
== -1 && PyErr_Occurred())
533 /* Avoid concurrent poll() invocation, issue 8865 */
534 if (self
->poll_running
) {
535 PyErr_SetString(PyExc_RuntimeError
,
536 "concurrent poll() invocation");
540 /* Ensure the ufd array is up to date */
541 if (!self
->ufd_uptodate
)
542 if (update_ufd_array(self
) == 0)
545 self
->poll_running
= 1;
548 Py_BEGIN_ALLOW_THREADS
549 poll_result
= poll(self
->ufds
, self
->ufd_len
, timeout
);
552 self
->poll_running
= 0;
554 if (poll_result
< 0) {
555 PyErr_SetFromErrno(SelectError
);
559 /* build the result list */
561 result_list
= PyList_New(poll_result
);
565 for (i
= 0, j
= 0; j
< poll_result
; j
++) {
566 /* skip to the next fired descriptor */
567 while (!self
->ufds
[i
].revents
) {
570 /* if we hit a NULL return, set value to NULL
571 and break out of loop; code at end will
572 clean up result_list */
573 value
= PyTuple_New(2);
576 num
= PyInt_FromLong(self
->ufds
[i
].fd
);
581 PyTuple_SET_ITEM(value
, 0, num
);
583 /* The &0xffff is a workaround for AIX. 'revents'
584 is a 16-bit short, and IBM assigned POLLNVAL
585 to be 0x8000, so the conversion to int results
586 in a negative number. See SF bug #923315. */
587 num
= PyInt_FromLong(self
->ufds
[i
].revents
& 0xffff);
592 PyTuple_SET_ITEM(value
, 1, num
);
593 if ((PyList_SetItem(result_list
, j
, value
)) == -1) {
603 Py_DECREF(result_list
);
607 static PyMethodDef poll_methods
[] = {
608 {"register", (PyCFunction
)poll_register
,
609 METH_VARARGS
, poll_register_doc
},
610 {"modify", (PyCFunction
)poll_modify
,
611 METH_VARARGS
, poll_modify_doc
},
612 {"unregister", (PyCFunction
)poll_unregister
,
613 METH_O
, poll_unregister_doc
},
614 {"poll", (PyCFunction
)poll_poll
,
615 METH_VARARGS
, poll_poll_doc
},
616 {NULL
, NULL
} /* sentinel */
623 self
= PyObject_New(pollObject
, &poll_Type
);
626 /* ufd_uptodate is a Boolean, denoting whether the
627 array pointed to by ufds matches the contents of the dictionary. */
628 self
->ufd_uptodate
= 0;
630 self
->poll_running
= 0;
631 self
->dict
= PyDict_New();
632 if (self
->dict
== NULL
) {
640 poll_dealloc(pollObject
*self
)
642 if (self
->ufds
!= NULL
)
643 PyMem_DEL(self
->ufds
);
644 Py_XDECREF(self
->dict
);
649 poll_getattr(pollObject
*self
, char *name
)
651 return Py_FindMethod(poll_methods
, (PyObject
*)self
, name
);
654 static PyTypeObject poll_Type
= {
655 /* The ob_type field must be initialized in the module init function
656 * to be portable to Windows without using C++. */
657 PyVarObject_HEAD_INIT(NULL
, 0)
658 "select.poll", /*tp_name*/
659 sizeof(pollObject
), /*tp_basicsize*/
662 (destructor
)poll_dealloc
, /*tp_dealloc*/
664 (getattrfunc
)poll_getattr
, /*tp_getattr*/
669 0, /*tp_as_sequence*/
674 PyDoc_STRVAR(poll_doc
,
675 "Returns a polling object, which supports registering and\n\
676 unregistering file descriptors, and then polling them for I/O events.");
679 select_poll(PyObject
*self
, PyObject
*unused
)
681 return (PyObject
*)newPollObject();
686 * On some systems poll() sets errno on invalid file descriptors. We test
687 * for this at runtime because this bug may be fixed or introduced between
690 static int select_have_broken_poll(void)
695 struct pollfd poll_struct
= { 0, POLLIN
|POLLPRI
|POLLOUT
, 0 };
697 /* Create a file descriptor to make invalid */
698 if (pipe(filedes
) < 0) {
701 poll_struct
.fd
= filedes
[0];
704 poll_test
= poll(&poll_struct
, 1, 0);
707 } else if (poll_test
== 0 && poll_struct
.revents
!= POLLNVAL
) {
712 #endif /* __APPLE__ */
714 #endif /* HAVE_POLL */
717 /* **************************************************************************
718 * epoll interface for Linux 2.6
720 * Written by Christian Heimes
721 * Inspired by Twisted's _epoll.pyx and select.poll()
724 #ifdef HAVE_SYS_EPOLL_H
725 #include <sys/epoll.h>
730 SOCKET epfd
; /* epoll control file descriptor */
733 static PyTypeObject pyEpoll_Type
;
734 #define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type))
737 pyepoll_err_closed(void)
739 PyErr_SetString(PyExc_ValueError
, "I/O operation on closed epoll fd");
744 pyepoll_internal_close(pyEpoll_Object
*self
)
747 if (self
->epfd
>= 0) {
748 int epfd
= self
->epfd
;
750 Py_BEGIN_ALLOW_THREADS
759 newPyEpoll_Object(PyTypeObject
*type
, int sizehint
, SOCKET fd
)
761 pyEpoll_Object
*self
;
763 if (sizehint
== -1) {
764 sizehint
= FD_SETSIZE
-1;
766 else if (sizehint
< 1) {
767 PyErr_Format(PyExc_ValueError
,
768 "sizehint must be greater zero, got %d",
773 assert(type
!= NULL
&& type
->tp_alloc
!= NULL
);
774 self
= (pyEpoll_Object
*) type
->tp_alloc(type
, 0);
779 Py_BEGIN_ALLOW_THREADS
780 self
->epfd
= epoll_create(sizehint
);
786 if (self
->epfd
< 0) {
788 PyErr_SetFromErrno(PyExc_IOError
);
791 return (PyObject
*)self
;
796 pyepoll_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
799 static char *kwlist
[] = {"sizehint", NULL
};
801 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "|i:epoll", kwlist
,
805 return newPyEpoll_Object(type
, sizehint
, -1);
810 pyepoll_dealloc(pyEpoll_Object
*self
)
812 (void)pyepoll_internal_close(self
);
813 Py_TYPE(self
)->tp_free(self
);
817 pyepoll_close(pyEpoll_Object
*self
)
819 errno
= pyepoll_internal_close(self
);
821 PyErr_SetFromErrno(PyExc_IOError
);
827 PyDoc_STRVAR(pyepoll_close_doc
,
830 Close the epoll control file descriptor. Further operations on the epoll\n\
831 object will raise an exception.");
834 pyepoll_get_closed(pyEpoll_Object
*self
)
843 pyepoll_fileno(pyEpoll_Object
*self
)
846 return pyepoll_err_closed();
847 return PyInt_FromLong(self
->epfd
);
850 PyDoc_STRVAR(pyepoll_fileno_doc
,
853 Return the epoll control file descriptor.");
856 pyepoll_fromfd(PyObject
*cls
, PyObject
*args
)
860 if (!PyArg_ParseTuple(args
, "i:fromfd", &fd
))
863 return newPyEpoll_Object((PyTypeObject
*)cls
, -1, fd
);
866 PyDoc_STRVAR(pyepoll_fromfd_doc
,
867 "fromfd(fd) -> epoll\n\
869 Create an epoll object from a given control fd.");
872 pyepoll_internal_ctl(int epfd
, int op
, PyObject
*pfd
, unsigned int events
)
874 struct epoll_event ev
;
879 return pyepoll_err_closed();
881 fd
= PyObject_AsFileDescriptor(pfd
);
891 Py_BEGIN_ALLOW_THREADS
892 result
= epoll_ctl(epfd
, op
, fd
, &ev
);
896 /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
897 * operation required a non-NULL pointer in event, even
898 * though this argument is ignored. */
899 Py_BEGIN_ALLOW_THREADS
900 result
= epoll_ctl(epfd
, op
, fd
, &ev
);
901 if (errno
== EBADF
) {
902 /* fd already closed */
914 PyErr_SetFromErrno(PyExc_IOError
);
921 pyepoll_register(pyEpoll_Object
*self
, PyObject
*args
, PyObject
*kwds
)
924 unsigned int events
= EPOLLIN
| EPOLLOUT
| EPOLLPRI
;
925 static char *kwlist
[] = {"fd", "eventmask", NULL
};
927 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|I:register", kwlist
,
932 return pyepoll_internal_ctl(self
->epfd
, EPOLL_CTL_ADD
, pfd
, events
);
935 PyDoc_STRVAR(pyepoll_register_doc
,
936 "register(fd[, eventmask]) -> None\n\
938 Registers a new fd or raises an IOError if the fd is already registered.\n\
939 fd is the target file descriptor of the operation.\n\
940 events is a bit set composed of the various EPOLL constants; the default\n\
941 is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\
943 The epoll interface supports all file descriptors that support poll.");
946 pyepoll_modify(pyEpoll_Object
*self
, PyObject
*args
, PyObject
*kwds
)
950 static char *kwlist
[] = {"fd", "eventmask", NULL
};
952 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "OI:modify", kwlist
,
957 return pyepoll_internal_ctl(self
->epfd
, EPOLL_CTL_MOD
, pfd
, events
);
960 PyDoc_STRVAR(pyepoll_modify_doc
,
961 "modify(fd, eventmask) -> None\n\
963 fd is the target file descriptor of the operation\n\
964 events is a bit set composed of the various EPOLL constants");
967 pyepoll_unregister(pyEpoll_Object
*self
, PyObject
*args
, PyObject
*kwds
)
970 static char *kwlist
[] = {"fd", NULL
};
972 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O:unregister", kwlist
,
977 return pyepoll_internal_ctl(self
->epfd
, EPOLL_CTL_DEL
, pfd
, 0);
980 PyDoc_STRVAR(pyepoll_unregister_doc
,
981 "unregister(fd) -> None\n\
983 fd is the target file descriptor of the operation.");
986 pyepoll_poll(pyEpoll_Object
*self
, PyObject
*args
, PyObject
*kwds
)
988 double dtimeout
= -1.;
992 PyObject
*elist
= NULL
, *etuple
= NULL
;
993 struct epoll_event
*evs
= NULL
;
994 static char *kwlist
[] = {"timeout", "maxevents", NULL
};
997 return pyepoll_err_closed();
999 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "|di:poll", kwlist
,
1000 &dtimeout
, &maxevents
)) {
1007 else if (dtimeout
* 1000.0 > INT_MAX
) {
1008 PyErr_SetString(PyExc_OverflowError
,
1009 "timeout is too large");
1013 timeout
= (int)(dtimeout
* 1000.0);
1016 if (maxevents
== -1) {
1017 maxevents
= FD_SETSIZE
-1;
1019 else if (maxevents
< 1) {
1020 PyErr_Format(PyExc_ValueError
,
1021 "maxevents must be greater than 0, got %d",
1026 evs
= PyMem_New(struct epoll_event
, maxevents
);
1033 Py_BEGIN_ALLOW_THREADS
1034 nfds
= epoll_wait(self
->epfd
, evs
, maxevents
, timeout
);
1035 Py_END_ALLOW_THREADS
1037 PyErr_SetFromErrno(PyExc_IOError
);
1041 elist
= PyList_New(nfds
);
1042 if (elist
== NULL
) {
1046 for (i
= 0; i
< nfds
; i
++) {
1047 etuple
= Py_BuildValue("iI", evs
[i
].data
.fd
, evs
[i
].events
);
1048 if (etuple
== NULL
) {
1052 PyList_SET_ITEM(elist
, i
, etuple
);
1060 PyDoc_STRVAR(pyepoll_poll_doc
,
1061 "poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
1063 Wait for events on the epoll file descriptor for a maximum time of timeout\n\
1064 in seconds (as float). -1 makes poll wait indefinitely.\n\
1065 Up to maxevents are returned to the caller.");
1067 static PyMethodDef pyepoll_methods
[] = {
1068 {"fromfd", (PyCFunction
)pyepoll_fromfd
,
1069 METH_VARARGS
| METH_CLASS
, pyepoll_fromfd_doc
},
1070 {"close", (PyCFunction
)pyepoll_close
, METH_NOARGS
,
1072 {"fileno", (PyCFunction
)pyepoll_fileno
, METH_NOARGS
,
1073 pyepoll_fileno_doc
},
1074 {"modify", (PyCFunction
)pyepoll_modify
,
1075 METH_VARARGS
| METH_KEYWORDS
, pyepoll_modify_doc
},
1076 {"register", (PyCFunction
)pyepoll_register
,
1077 METH_VARARGS
| METH_KEYWORDS
, pyepoll_register_doc
},
1078 {"unregister", (PyCFunction
)pyepoll_unregister
,
1079 METH_VARARGS
| METH_KEYWORDS
, pyepoll_unregister_doc
},
1080 {"poll", (PyCFunction
)pyepoll_poll
,
1081 METH_VARARGS
| METH_KEYWORDS
, pyepoll_poll_doc
},
1085 static PyGetSetDef pyepoll_getsetlist
[] = {
1086 {"closed", (getter
)pyepoll_get_closed
, NULL
,
1087 "True if the epoll handler is closed"},
1091 PyDoc_STRVAR(pyepoll_doc
,
1092 "select.epoll([sizehint=-1])\n\
1094 Returns an epolling object\n\
1096 sizehint must be a positive integer or -1 for the default size. The\n\
1097 sizehint is used to optimize internal data structures. It doesn't limit\n\
1098 the maximum number of monitored events.");
1100 static PyTypeObject pyEpoll_Type
= {
1101 PyVarObject_HEAD_INIT(NULL
, 0)
1102 "select.epoll", /* tp_name */
1103 sizeof(pyEpoll_Object
), /* tp_basicsize */
1104 0, /* tp_itemsize */
1105 (destructor
)pyepoll_dealloc
, /* tp_dealloc */
1111 0, /* tp_as_number */
1112 0, /* tp_as_sequence */
1113 0, /* tp_as_mapping */
1117 PyObject_GenericGetAttr
, /* tp_getattro */
1118 0, /* tp_setattro */
1119 0, /* tp_as_buffer */
1120 Py_TPFLAGS_DEFAULT
, /* tp_flags */
1121 pyepoll_doc
, /* tp_doc */
1122 0, /* tp_traverse */
1124 0, /* tp_richcompare */
1125 0, /* tp_weaklistoffset */
1127 0, /* tp_iternext */
1128 pyepoll_methods
, /* tp_methods */
1130 pyepoll_getsetlist
, /* tp_getset */
1133 0, /* tp_descr_get */
1134 0, /* tp_descr_set */
1135 0, /* tp_dictoffset */
1138 pyepoll_new
, /* tp_new */
1142 #endif /* HAVE_EPOLL */
1145 /* **************************************************************************
1146 * kqueue interface for BSD
1148 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
1149 * All rights reserved.
1151 * Redistribution and use in source and binary forms, with or without
1152 * modification, are permitted provided that the following conditions
1154 * 1. Redistributions of source code must retain the above copyright
1155 * notice, this list of conditions and the following disclaimer.
1156 * 2. Redistributions in binary form must reproduce the above copyright
1157 * notice, this list of conditions and the following disclaimer in the
1158 * documentation and/or other materials provided with the distribution.
1160 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1161 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1162 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1163 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1164 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1165 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1166 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1167 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1168 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1169 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1173 #ifdef HAVE_SYS_EVENT_H
1174 #include <sys/event.h>
1177 PyDoc_STRVAR(kqueue_event_doc
,
1178 "kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
1180 This object is the equivalent of the struct kevent for the C API.\n\
1182 See the kqueue manpage for more detailed information about the meaning\n\
1183 of the arguments.\n\
1185 One minor note: while you might hope that udata could store a\n\
1186 reference to a python object, it cannot, because it is impossible to\n\
1187 keep a proper reference count of the object once it's passed into the\n\
1188 kernel. Therefore, I have restricted it to only storing an integer. I\n\
1189 recommend ignoring it and simply using the 'ident' field to key off\n\
1190 of. You could also set up a dictionary on the python side to store a\n\
1191 udata->object mapping.");
1196 } kqueue_event_Object
;
1198 static PyTypeObject kqueue_event_Type
;
1200 #define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
1204 SOCKET kqfd
; /* kqueue control fd */
1205 } kqueue_queue_Object
;
1207 static PyTypeObject kqueue_queue_Type
;
1209 #define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
1211 #if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
1212 # error uintptr_t does not match void *!
1213 #elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
1214 # define T_UINTPTRT T_ULONGLONG
1215 # define T_INTPTRT T_LONGLONG
1216 # define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
1217 # define UINTPTRT_FMT_UNIT "K"
1218 # define INTPTRT_FMT_UNIT "L"
1219 #elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
1220 # define T_UINTPTRT T_ULONG
1221 # define T_INTPTRT T_LONG
1222 # define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1223 # define UINTPTRT_FMT_UNIT "k"
1224 # define INTPTRT_FMT_UNIT "l"
1225 #elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
1226 # define T_UINTPTRT T_UINT
1227 # define T_INTPTRT T_INT
1228 # define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1229 # define UINTPTRT_FMT_UNIT "I"
1230 # define INTPTRT_FMT_UNIT "i"
1232 # error uintptr_t does not match int, long, or long long!
1236 * kevent is not standard and its members vary across BSDs.
1238 #if !defined(__OpenBSD__)
1239 # define IDENT_TYPE T_UINTPTRT
1240 # define IDENT_CAST Py_intptr_t
1241 # define DATA_TYPE T_INTPTRT
1242 # define DATA_FMT_UNIT INTPTRT_FMT_UNIT
1243 # define IDENT_AsType PyLong_AsUintptr_t
1245 # define IDENT_TYPE T_UINT
1246 # define IDENT_CAST int
1247 # define DATA_TYPE T_INT
1248 # define DATA_FMT_UNIT "i"
1249 # define IDENT_AsType PyLong_AsUnsignedLong
1252 /* Unfortunately, we can't store python objects in udata, because
1253 * kevents in the kernel can be removed without warning, which would
1254 * forever lose the refcount on the object stored with it.
1257 #define KQ_OFF(x) offsetof(kqueue_event_Object, x)
1258 static struct PyMemberDef kqueue_event_members
[] = {
1259 {"ident", IDENT_TYPE
, KQ_OFF(e
.ident
)},
1260 {"filter", T_SHORT
, KQ_OFF(e
.filter
)},
1261 {"flags", T_USHORT
, KQ_OFF(e
.flags
)},
1262 {"fflags", T_UINT
, KQ_OFF(e
.fflags
)},
1263 {"data", DATA_TYPE
, KQ_OFF(e
.data
)},
1264 {"udata", T_UINTPTRT
, KQ_OFF(e
.udata
)},
1265 {NULL
} /* Sentinel */
1271 kqueue_event_repr(kqueue_event_Object
*s
)
1276 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1277 "data=0x%zd udata=%p>",
1278 (size_t)(s
->e
.ident
), s
->e
.filter
, s
->e
.flags
,
1279 s
->e
.fflags
, (Py_ssize_t
)(s
->e
.data
), s
->e
.udata
);
1280 return PyString_FromString(buf
);
1284 kqueue_event_init(kqueue_event_Object
*self
, PyObject
*args
, PyObject
*kwds
)
1287 static char *kwlist
[] = {"ident", "filter", "flags", "fflags",
1288 "data", "udata", NULL
};
1289 static char *fmt
= "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT
":kevent";
1291 EV_SET(&(self
->e
), 0, EVFILT_READ
, EV_ADD
, 0, 0, 0); /* defaults */
1293 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, fmt
, kwlist
,
1294 &pfd
, &(self
->e
.filter
), &(self
->e
.flags
),
1295 &(self
->e
.fflags
), &(self
->e
.data
), &(self
->e
.udata
))) {
1299 if (PyLong_Check(pfd
)
1300 #if IDENT_TYPE == T_UINT
1301 && PyLong_AsUnsignedLong(pfd
) <= UINT_MAX
1304 self
->e
.ident
= IDENT_AsType(pfd
);
1307 self
->e
.ident
= PyObject_AsFileDescriptor(pfd
);
1309 if (PyErr_Occurred()) {
1316 kqueue_event_richcompare(kqueue_event_Object
*s
, kqueue_event_Object
*o
,
1319 Py_intptr_t result
= 0;
1321 if (!kqueue_event_Check(o
)) {
1322 if (op
== Py_EQ
|| op
== Py_NE
) {
1323 PyObject
*res
= op
== Py_EQ
? Py_False
: Py_True
;
1327 PyErr_Format(PyExc_TypeError
,
1328 "can't compare %.200s to %.200s",
1329 Py_TYPE(s
)->tp_name
, Py_TYPE(o
)->tp_name
);
1332 if (((result
= (IDENT_CAST
)(s
->e
.ident
- o
->e
.ident
)) == 0) &&
1333 ((result
= s
->e
.filter
- o
->e
.filter
) == 0) &&
1334 ((result
= s
->e
.flags
- o
->e
.flags
) == 0) &&
1335 ((result
= (int)(s
->e
.fflags
- o
->e
.fflags
)) == 0) &&
1336 ((result
= s
->e
.data
- o
->e
.data
) == 0) &&
1337 ((result
= s
->e
.udata
- o
->e
.udata
) == 0)
1344 result
= (result
== 0);
1347 result
= (result
!= 0);
1350 result
= (result
<= 0);
1353 result
= (result
>= 0);
1356 result
= (result
< 0);
1359 result
= (result
> 0);
1362 return PyBool_FromLong((long)result
);
1365 static PyTypeObject kqueue_event_Type
= {
1366 PyVarObject_HEAD_INIT(NULL
, 0)
1367 "select.kevent", /* tp_name */
1368 sizeof(kqueue_event_Object
), /* tp_basicsize */
1369 0, /* tp_itemsize */
1375 (reprfunc
)kqueue_event_repr
, /* tp_repr */
1376 0, /* tp_as_number */
1377 0, /* tp_as_sequence */
1378 0, /* tp_as_mapping */
1382 0, /* tp_getattro */
1383 0, /* tp_setattro */
1384 0, /* tp_as_buffer */
1385 Py_TPFLAGS_DEFAULT
, /* tp_flags */
1386 kqueue_event_doc
, /* tp_doc */
1387 0, /* tp_traverse */
1389 (richcmpfunc
)kqueue_event_richcompare
, /* tp_richcompare */
1390 0, /* tp_weaklistoffset */
1392 0, /* tp_iternext */
1394 kqueue_event_members
, /* tp_members */
1398 0, /* tp_descr_get */
1399 0, /* tp_descr_set */
1400 0, /* tp_dictoffset */
1401 (initproc
)kqueue_event_init
, /* tp_init */
1408 kqueue_queue_err_closed(void)
1410 PyErr_SetString(PyExc_ValueError
, "I/O operation on closed kqueue fd");
1415 kqueue_queue_internal_close(kqueue_queue_Object
*self
)
1418 if (self
->kqfd
>= 0) {
1419 int kqfd
= self
->kqfd
;
1421 Py_BEGIN_ALLOW_THREADS
1422 if (close(kqfd
) < 0)
1424 Py_END_ALLOW_THREADS
1430 newKqueue_Object(PyTypeObject
*type
, SOCKET fd
)
1432 kqueue_queue_Object
*self
;
1433 assert(type
!= NULL
&& type
->tp_alloc
!= NULL
);
1434 self
= (kqueue_queue_Object
*) type
->tp_alloc(type
, 0);
1440 Py_BEGIN_ALLOW_THREADS
1441 self
->kqfd
= kqueue();
1442 Py_END_ALLOW_THREADS
1447 if (self
->kqfd
< 0) {
1449 PyErr_SetFromErrno(PyExc_IOError
);
1452 return (PyObject
*)self
;
1456 kqueue_queue_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1459 if ((args
!= NULL
&& PyObject_Size(args
)) ||
1460 (kwds
!= NULL
&& PyObject_Size(kwds
))) {
1461 PyErr_SetString(PyExc_ValueError
,
1462 "select.kqueue doesn't accept arguments");
1466 return newKqueue_Object(type
, -1);
1470 kqueue_queue_dealloc(kqueue_queue_Object
*self
)
1472 kqueue_queue_internal_close(self
);
1473 Py_TYPE(self
)->tp_free(self
);
1477 kqueue_queue_close(kqueue_queue_Object
*self
)
1479 errno
= kqueue_queue_internal_close(self
);
1481 PyErr_SetFromErrno(PyExc_IOError
);
1487 PyDoc_STRVAR(kqueue_queue_close_doc
,
1490 Close the kqueue control file descriptor. Further operations on the kqueue\n\
1491 object will raise an exception.");
1494 kqueue_queue_get_closed(kqueue_queue_Object
*self
)
1503 kqueue_queue_fileno(kqueue_queue_Object
*self
)
1506 return kqueue_queue_err_closed();
1507 return PyInt_FromLong(self
->kqfd
);
1510 PyDoc_STRVAR(kqueue_queue_fileno_doc
,
1513 Return the kqueue control file descriptor.");
1516 kqueue_queue_fromfd(PyObject
*cls
, PyObject
*args
)
1520 if (!PyArg_ParseTuple(args
, "i:fromfd", &fd
))
1523 return newKqueue_Object((PyTypeObject
*)cls
, fd
);
1526 PyDoc_STRVAR(kqueue_queue_fromfd_doc
,
1527 "fromfd(fd) -> kqueue\n\
1529 Create a kqueue object from a given control fd.");
1532 kqueue_queue_control(kqueue_queue_Object
*self
, PyObject
*args
)
1538 PyObject
*otimeout
= NULL
;
1539 PyObject
*ch
= NULL
;
1540 PyObject
*it
= NULL
, *ei
= NULL
;
1541 PyObject
*result
= NULL
;
1542 struct kevent
*evl
= NULL
;
1543 struct kevent
*chl
= NULL
;
1544 struct timespec timeoutspec
;
1545 struct timespec
*ptimeoutspec
;
1548 return kqueue_queue_err_closed();
1550 if (!PyArg_ParseTuple(args
, "Oi|O:control", &ch
, &nevents
, &otimeout
))
1554 PyErr_Format(PyExc_ValueError
,
1555 "Length of eventlist must be 0 or positive, got %d",
1560 if (otimeout
== Py_None
|| otimeout
== NULL
) {
1561 ptimeoutspec
= NULL
;
1563 else if (PyNumber_Check(otimeout
)) {
1567 timeout
= PyFloat_AsDouble(otimeout
);
1568 if (timeout
== -1 && PyErr_Occurred())
1570 if (timeout
> (double)LONG_MAX
) {
1571 PyErr_SetString(PyExc_OverflowError
,
1572 "timeout period too long");
1576 PyErr_SetString(PyExc_ValueError
,
1577 "timeout must be positive or None");
1581 seconds
= (long)timeout
;
1582 timeout
= timeout
- (double)seconds
;
1583 timeoutspec
.tv_sec
= seconds
;
1584 timeoutspec
.tv_nsec
= (long)(timeout
* 1E9
);
1585 ptimeoutspec
= &timeoutspec
;
1588 PyErr_Format(PyExc_TypeError
,
1589 "timeout argument must be an number "
1590 "or None, got %.200s",
1591 Py_TYPE(otimeout
)->tp_name
);
1595 if (ch
!= NULL
&& ch
!= Py_None
) {
1596 it
= PyObject_GetIter(ch
);
1598 PyErr_SetString(PyExc_TypeError
,
1599 "changelist is not iterable");
1602 nchanges
= PyObject_Size(ch
);
1607 chl
= PyMem_New(struct kevent
, nchanges
);
1613 while ((ei
= PyIter_Next(it
)) != NULL
) {
1614 if (!kqueue_event_Check(ei
)) {
1616 PyErr_SetString(PyExc_TypeError
,
1617 "changelist must be an iterable of "
1618 "select.kevent objects");
1621 chl
[i
++] = ((kqueue_event_Object
*)ei
)->e
;
1630 evl
= PyMem_New(struct kevent
, nevents
);
1637 Py_BEGIN_ALLOW_THREADS
1638 gotevents
= kevent(self
->kqfd
, chl
, nchanges
,
1639 evl
, nevents
, ptimeoutspec
);
1640 Py_END_ALLOW_THREADS
1642 if (gotevents
== -1) {
1643 PyErr_SetFromErrno(PyExc_OSError
);
1647 result
= PyList_New(gotevents
);
1648 if (result
== NULL
) {
1652 for (i
= 0; i
< gotevents
; i
++) {
1653 kqueue_event_Object
*ch
;
1655 ch
= PyObject_New(kqueue_event_Object
, &kqueue_event_Type
);
1660 PyList_SET_ITEM(result
, i
, (PyObject
*)ch
);
1674 PyDoc_STRVAR(kqueue_queue_control_doc
,
1675 "control(changelist, max_events[, timeout=None]) -> eventlist\n\
1677 Calls the kernel kevent function.\n\
1678 - changelist must be a list of kevent objects describing the changes\n\
1679 to be made to the kernel's watch list or None.\n\
1680 - max_events lets you specify the maximum number of events that the\n\
1681 kernel will return.\n\
1682 - timeout is the maximum time to wait in seconds, or else None,\n\
1683 to wait forever. timeout accepts floats for smaller timeouts, too.");
1686 static PyMethodDef kqueue_queue_methods
[] = {
1687 {"fromfd", (PyCFunction
)kqueue_queue_fromfd
,
1688 METH_VARARGS
| METH_CLASS
, kqueue_queue_fromfd_doc
},
1689 {"close", (PyCFunction
)kqueue_queue_close
, METH_NOARGS
,
1690 kqueue_queue_close_doc
},
1691 {"fileno", (PyCFunction
)kqueue_queue_fileno
, METH_NOARGS
,
1692 kqueue_queue_fileno_doc
},
1693 {"control", (PyCFunction
)kqueue_queue_control
,
1694 METH_VARARGS
, kqueue_queue_control_doc
},
1698 static PyGetSetDef kqueue_queue_getsetlist
[] = {
1699 {"closed", (getter
)kqueue_queue_get_closed
, NULL
,
1700 "True if the kqueue handler is closed"},
1704 PyDoc_STRVAR(kqueue_queue_doc
,
1705 "Kqueue syscall wrapper.\n\
1707 For example, to start watching a socket for input:\n\
1708 >>> kq = kqueue()\n\
1709 >>> sock = socket()\n\
1710 >>> sock.connect((host, port))\n\
1711 >>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
1713 To wait one second for it to become writeable:\n\
1714 >>> kq.control(None, 1, 1000)\n\
1716 To stop listening:\n\
1717 >>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
1719 static PyTypeObject kqueue_queue_Type
= {
1720 PyVarObject_HEAD_INIT(NULL
, 0)
1721 "select.kqueue", /* tp_name */
1722 sizeof(kqueue_queue_Object
), /* tp_basicsize */
1723 0, /* tp_itemsize */
1724 (destructor
)kqueue_queue_dealloc
, /* tp_dealloc */
1730 0, /* tp_as_number */
1731 0, /* tp_as_sequence */
1732 0, /* tp_as_mapping */
1736 0, /* tp_getattro */
1737 0, /* tp_setattro */
1738 0, /* tp_as_buffer */
1739 Py_TPFLAGS_DEFAULT
, /* tp_flags */
1740 kqueue_queue_doc
, /* tp_doc */
1741 0, /* tp_traverse */
1743 0, /* tp_richcompare */
1744 0, /* tp_weaklistoffset */
1746 0, /* tp_iternext */
1747 kqueue_queue_methods
, /* tp_methods */
1749 kqueue_queue_getsetlist
, /* tp_getset */
1752 0, /* tp_descr_get */
1753 0, /* tp_descr_set */
1754 0, /* tp_dictoffset */
1757 kqueue_queue_new
, /* tp_new */
1761 #endif /* HAVE_KQUEUE */
1762 /* ************************************************************************ */
1764 PyDoc_STRVAR(select_doc
,
1765 "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
1767 Wait until one or more file descriptors are ready for some kind of I/O.\n\
1768 The first three arguments are sequences of file descriptors to be waited for:\n\
1769 rlist -- wait until ready for reading\n\
1770 wlist -- wait until ready for writing\n\
1771 xlist -- wait for an ``exceptional condition''\n\
1772 If only one kind of condition is required, pass [] for the other lists.\n\
1773 A file descriptor is either a socket or file object, or a small integer\n\
1774 gotten from a fileno() method call on one of those.\n\
1776 The optional 4th argument specifies a timeout in seconds; it may be\n\
1777 a floating point number to specify fractions of seconds. If it is absent\n\
1778 or None, the call will never time out.\n\
1780 The return value is a tuple of three lists corresponding to the first three\n\
1781 arguments; each contains the subset of the corresponding file descriptors\n\
1784 *** IMPORTANT NOTICE ***\n\
1785 On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\
1786 descriptors can be used.");
1788 static PyMethodDef select_methods
[] = {
1789 {"select", select_select
, METH_VARARGS
, select_doc
},
1790 #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
1791 {"poll", select_poll
, METH_NOARGS
, poll_doc
},
1792 #endif /* HAVE_POLL */
1793 {0, 0}, /* sentinel */
1796 PyDoc_STRVAR(module_doc
,
1797 "This module supports asynchronous I/O on multiple file descriptors.\n\
1799 *** IMPORTANT NOTICE ***\n\
1800 On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
1806 m
= Py_InitModule3("select", select_methods
, module_doc
);
1810 SelectError
= PyErr_NewException("select.error", NULL
, NULL
);
1811 Py_INCREF(SelectError
);
1812 PyModule_AddObject(m
, "error", SelectError
);
1815 #ifdef HAVE_BROKEN_PIPE_BUF
1817 #define PIPE_BUF 512
1819 PyModule_AddIntConstant(m
, "PIPE_BUF", PIPE_BUF
);
1822 #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
1824 if (select_have_broken_poll()) {
1825 if (PyObject_DelAttrString(m
, "poll") == -1) {
1832 Py_TYPE(&poll_Type
) = &PyType_Type
;
1833 PyModule_AddIntConstant(m
, "POLLIN", POLLIN
);
1834 PyModule_AddIntConstant(m
, "POLLPRI", POLLPRI
);
1835 PyModule_AddIntConstant(m
, "POLLOUT", POLLOUT
);
1836 PyModule_AddIntConstant(m
, "POLLERR", POLLERR
);
1837 PyModule_AddIntConstant(m
, "POLLHUP", POLLHUP
);
1838 PyModule_AddIntConstant(m
, "POLLNVAL", POLLNVAL
);
1841 PyModule_AddIntConstant(m
, "POLLRDNORM", POLLRDNORM
);
1844 PyModule_AddIntConstant(m
, "POLLRDBAND", POLLRDBAND
);
1847 PyModule_AddIntConstant(m
, "POLLWRNORM", POLLWRNORM
);
1850 PyModule_AddIntConstant(m
, "POLLWRBAND", POLLWRBAND
);
1853 PyModule_AddIntConstant(m
, "POLLMSG", POLLMSG
);
1856 #endif /* HAVE_POLL */
1859 Py_TYPE(&pyEpoll_Type
) = &PyType_Type
;
1860 if (PyType_Ready(&pyEpoll_Type
) < 0)
1863 Py_INCREF(&pyEpoll_Type
);
1864 PyModule_AddObject(m
, "epoll", (PyObject
*) &pyEpoll_Type
);
1866 PyModule_AddIntConstant(m
, "EPOLLIN", EPOLLIN
);
1867 PyModule_AddIntConstant(m
, "EPOLLOUT", EPOLLOUT
);
1868 PyModule_AddIntConstant(m
, "EPOLLPRI", EPOLLPRI
);
1869 PyModule_AddIntConstant(m
, "EPOLLERR", EPOLLERR
);
1870 PyModule_AddIntConstant(m
, "EPOLLHUP", EPOLLHUP
);
1871 PyModule_AddIntConstant(m
, "EPOLLET", EPOLLET
);
1874 PyModule_AddIntConstant(m
, "EPOLLONESHOT", EPOLLONESHOT
);
1876 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
1877 PyModule_AddIntConstant(m
, "EPOLLRDNORM", EPOLLRDNORM
);
1878 PyModule_AddIntConstant(m
, "EPOLLRDBAND", EPOLLRDBAND
);
1879 PyModule_AddIntConstant(m
, "EPOLLWRNORM", EPOLLWRNORM
);
1880 PyModule_AddIntConstant(m
, "EPOLLWRBAND", EPOLLWRBAND
);
1881 PyModule_AddIntConstant(m
, "EPOLLMSG", EPOLLMSG
);
1882 #endif /* HAVE_EPOLL */
1885 kqueue_event_Type
.tp_new
= PyType_GenericNew
;
1886 Py_TYPE(&kqueue_event_Type
) = &PyType_Type
;
1887 if(PyType_Ready(&kqueue_event_Type
) < 0)
1890 Py_INCREF(&kqueue_event_Type
);
1891 PyModule_AddObject(m
, "kevent", (PyObject
*)&kqueue_event_Type
);
1893 Py_TYPE(&kqueue_queue_Type
) = &PyType_Type
;
1894 if(PyType_Ready(&kqueue_queue_Type
) < 0)
1896 Py_INCREF(&kqueue_queue_Type
);
1897 PyModule_AddObject(m
, "kqueue", (PyObject
*)&kqueue_queue_Type
);
1900 PyModule_AddIntConstant(m
, "KQ_FILTER_READ", EVFILT_READ
);
1901 PyModule_AddIntConstant(m
, "KQ_FILTER_WRITE", EVFILT_WRITE
);
1902 PyModule_AddIntConstant(m
, "KQ_FILTER_AIO", EVFILT_AIO
);
1903 PyModule_AddIntConstant(m
, "KQ_FILTER_VNODE", EVFILT_VNODE
);
1904 PyModule_AddIntConstant(m
, "KQ_FILTER_PROC", EVFILT_PROC
);
1905 #ifdef EVFILT_NETDEV
1906 PyModule_AddIntConstant(m
, "KQ_FILTER_NETDEV", EVFILT_NETDEV
);
1908 PyModule_AddIntConstant(m
, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL
);
1909 PyModule_AddIntConstant(m
, "KQ_FILTER_TIMER", EVFILT_TIMER
);
1912 PyModule_AddIntConstant(m
, "KQ_EV_ADD", EV_ADD
);
1913 PyModule_AddIntConstant(m
, "KQ_EV_DELETE", EV_DELETE
);
1914 PyModule_AddIntConstant(m
, "KQ_EV_ENABLE", EV_ENABLE
);
1915 PyModule_AddIntConstant(m
, "KQ_EV_DISABLE", EV_DISABLE
);
1916 PyModule_AddIntConstant(m
, "KQ_EV_ONESHOT", EV_ONESHOT
);
1917 PyModule_AddIntConstant(m
, "KQ_EV_CLEAR", EV_CLEAR
);
1919 PyModule_AddIntConstant(m
, "KQ_EV_SYSFLAGS", EV_SYSFLAGS
);
1920 PyModule_AddIntConstant(m
, "KQ_EV_FLAG1", EV_FLAG1
);
1922 PyModule_AddIntConstant(m
, "KQ_EV_EOF", EV_EOF
);
1923 PyModule_AddIntConstant(m
, "KQ_EV_ERROR", EV_ERROR
);
1925 /* READ WRITE filter flag */
1926 PyModule_AddIntConstant(m
, "KQ_NOTE_LOWAT", NOTE_LOWAT
);
1928 /* VNODE filter flags */
1929 PyModule_AddIntConstant(m
, "KQ_NOTE_DELETE", NOTE_DELETE
);
1930 PyModule_AddIntConstant(m
, "KQ_NOTE_WRITE", NOTE_WRITE
);
1931 PyModule_AddIntConstant(m
, "KQ_NOTE_EXTEND", NOTE_EXTEND
);
1932 PyModule_AddIntConstant(m
, "KQ_NOTE_ATTRIB", NOTE_ATTRIB
);
1933 PyModule_AddIntConstant(m
, "KQ_NOTE_LINK", NOTE_LINK
);
1934 PyModule_AddIntConstant(m
, "KQ_NOTE_RENAME", NOTE_RENAME
);
1935 PyModule_AddIntConstant(m
, "KQ_NOTE_REVOKE", NOTE_REVOKE
);
1937 /* PROC filter flags */
1938 PyModule_AddIntConstant(m
, "KQ_NOTE_EXIT", NOTE_EXIT
);
1939 PyModule_AddIntConstant(m
, "KQ_NOTE_FORK", NOTE_FORK
);
1940 PyModule_AddIntConstant(m
, "KQ_NOTE_EXEC", NOTE_EXEC
);
1941 PyModule_AddIntConstant(m
, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK
);
1942 PyModule_AddIntConstant(m
, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK
);
1944 PyModule_AddIntConstant(m
, "KQ_NOTE_TRACK", NOTE_TRACK
);
1945 PyModule_AddIntConstant(m
, "KQ_NOTE_CHILD", NOTE_CHILD
);
1946 PyModule_AddIntConstant(m
, "KQ_NOTE_TRACKERR", NOTE_TRACKERR
);
1948 /* NETDEV filter flags */
1949 #ifdef EVFILT_NETDEV
1950 PyModule_AddIntConstant(m
, "KQ_NOTE_LINKUP", NOTE_LINKUP
);
1951 PyModule_AddIntConstant(m
, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN
);
1952 PyModule_AddIntConstant(m
, "KQ_NOTE_LINKINV", NOTE_LINKINV
);
1955 #endif /* HAVE_KQUEUE */