1996-06-12 01:22:53 -03:00
|
|
|
/* select - Module containing unix select(2) call.
|
1996-12-11 20:04:35 -04:00
|
|
|
Under Unix, the file descriptors are small integers.
|
|
|
|
Under Win32, select only exists for sockets, and sockets may
|
|
|
|
have any value except INVALID_SOCKET.
|
1998-08-04 19:53:56 -03:00
|
|
|
Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything
|
|
|
|
>= 0.
|
1996-06-12 01:22:53 -03:00
|
|
|
*/
|
1992-06-23 06:07:03 -03:00
|
|
|
|
1996-12-11 20:04:35 -04:00
|
|
|
#include "Python.h"
|
2008-03-21 20:49:44 -03:00
|
|
|
#include <structmember.h>
|
1992-06-23 06:07:03 -03:00
|
|
|
|
2006-04-23 09:36:23 -03:00
|
|
|
#ifdef __APPLE__
|
|
|
|
/* Perform runtime testing for a broken poll on OSX to make it easier
|
|
|
|
* to use the same binary on multiple releases of the OS.
|
|
|
|
*/
|
|
|
|
#undef HAVE_BROKEN_POLL
|
|
|
|
#endif
|
|
|
|
|
2000-12-11 21:18:41 -04:00
|
|
|
/* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.
|
|
|
|
64 is too small (too many people have bumped into that limit).
|
|
|
|
Here we boost it.
|
|
|
|
Users who want even more than the boosted limit should #define
|
|
|
|
FD_SETSIZE higher before this; e.g., via compiler /D switch.
|
|
|
|
*/
|
|
|
|
#if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
|
|
|
|
#define FD_SETSIZE 512
|
2010-05-09 11:46:46 -03:00
|
|
|
#endif
|
2000-12-11 21:18:41 -04:00
|
|
|
|
2001-07-14 17:54:37 -03:00
|
|
|
#if defined(HAVE_POLL_H)
|
2000-08-24 22:15:33 -03:00
|
|
|
#include <poll.h>
|
2001-07-14 17:54:37 -03:00
|
|
|
#elif defined(HAVE_SYS_POLL_H)
|
|
|
|
#include <sys/poll.h>
|
2000-08-24 22:15:33 -03:00
|
|
|
#endif
|
1996-12-05 19:43:35 -04:00
|
|
|
|
1996-12-09 14:47:43 -04:00
|
|
|
#ifdef __sgi
|
|
|
|
/* This is missing from unistd.h */
|
2000-07-22 20:57:55 -03:00
|
|
|
extern void bzero(void *, int);
|
1996-12-09 14:47:43 -04:00
|
|
|
#endif
|
|
|
|
|
2006-06-10 09:23:46 -03:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
1994-08-01 08:34:53 -03:00
|
|
|
#include <sys/types.h>
|
1999-08-27 17:39:37 -03:00
|
|
|
#endif
|
1996-06-12 01:22:53 -03:00
|
|
|
|
2002-03-02 22:59:16 -04:00
|
|
|
#if defined(PYOS_OS2) && !defined(PYCC_GCC)
|
1997-11-22 17:53:48 -04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <utils.h>
|
|
|
|
#endif
|
|
|
|
|
1996-06-28 17:15:15 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2010-11-03 10:27:33 -03:00
|
|
|
# include <winsock2.h>
|
1996-06-12 01:22:53 -03:00
|
|
|
#else
|
2006-07-09 22:18:57 -03:00
|
|
|
# define SOCKET int
|
|
|
|
# ifdef __BEOS__
|
|
|
|
# include <net/socket.h>
|
|
|
|
# elif defined(__VMS)
|
|
|
|
# include <socket.h>
|
|
|
|
# endif
|
1998-08-04 19:53:56 -03:00
|
|
|
#endif
|
1992-06-23 06:07:03 -03:00
|
|
|
|
1996-12-11 20:04:35 -04:00
|
|
|
static PyObject *SelectError;
|
1992-06-23 06:07:03 -03:00
|
|
|
|
1996-12-12 18:16:21 -04:00
|
|
|
/* list of Python objects and their file descriptor */
|
|
|
|
typedef struct {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *obj; /* owned reference */
|
|
|
|
SOCKET fd;
|
|
|
|
int sentinel; /* -1 == sentinel */
|
1996-06-12 01:22:53 -03:00
|
|
|
} pylist;
|
|
|
|
|
1996-12-12 18:16:21 -04:00
|
|
|
static void
|
2001-08-16 18:59:46 -03:00
|
|
|
reap_obj(pylist fd2obj[FD_SETSIZE + 1])
|
1996-12-12 18:16:21 -04:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
|
|
|
|
Py_XDECREF(fd2obj[i].obj);
|
|
|
|
fd2obj[i].obj = NULL;
|
|
|
|
}
|
|
|
|
fd2obj[0].sentinel = -1;
|
1996-12-12 18:16:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-12-11 20:04:35 -04:00
|
|
|
/* returns -1 and sets the Python exception if an error occurred, otherwise
|
|
|
|
returns a number >= 0
|
|
|
|
*/
|
1996-06-12 01:22:53 -03:00
|
|
|
static int
|
2003-09-10 16:37:42 -03:00
|
|
|
seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
|
1992-06-23 06:07:03 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int i;
|
|
|
|
int max = -1;
|
|
|
|
int index = 0;
|
|
|
|
PyObject* fast_seq = NULL;
|
|
|
|
PyObject* o = NULL;
|
|
|
|
|
|
|
|
fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
|
|
|
|
FD_ZERO(set);
|
|
|
|
|
|
|
|
fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences");
|
|
|
|
if (!fast_seq)
|
|
|
|
return -1;
|
2003-09-10 16:37:42 -03:00
|
|
|
|
2012-11-01 16:13:54 -03:00
|
|
|
for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++) {
|
2010-05-09 11:46:46 -03:00
|
|
|
SOCKET v;
|
1996-12-12 18:16:21 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* any intervening fileno() calls could decr this refcnt */
|
|
|
|
if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
|
|
|
|
return -1;
|
1996-12-13 19:22:42 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_INCREF(o);
|
|
|
|
v = PyObject_AsFileDescriptor( o );
|
|
|
|
if (v == -1) goto finally;
|
1996-12-12 18:16:21 -04:00
|
|
|
|
2000-01-14 12:33:09 -04:00
|
|
|
#if defined(_MSC_VER)
|
2010-05-09 11:46:46 -03:00
|
|
|
max = 0; /* not used for Win32 */
|
1996-12-12 18:16:21 -04:00
|
|
|
#else /* !_MSC_VER */
|
2011-08-28 11:22:33 -03:00
|
|
|
if (!_PyIsSelectable_fd(v)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"filedescriptor out of range in select()");
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
if (v > max)
|
|
|
|
max = v;
|
1996-12-12 18:16:21 -04:00
|
|
|
#endif /* _MSC_VER */
|
2010-05-09 11:46:46 -03:00
|
|
|
FD_SET(v, set);
|
|
|
|
|
|
|
|
/* add object and its file descriptor to the list */
|
|
|
|
if (index >= FD_SETSIZE) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"too many file descriptors in select()");
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
fd2obj[index].obj = o;
|
|
|
|
fd2obj[index].fd = v;
|
|
|
|
fd2obj[index].sentinel = 0;
|
|
|
|
fd2obj[++index].sentinel = -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(fast_seq);
|
|
|
|
return max+1;
|
1996-12-12 18:16:21 -04:00
|
|
|
|
|
|
|
finally:
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_XDECREF(o);
|
|
|
|
Py_DECREF(fast_seq);
|
|
|
|
return -1;
|
1992-06-23 06:07:03 -03:00
|
|
|
}
|
|
|
|
|
1996-12-11 20:04:35 -04:00
|
|
|
/* returns NULL and sets the Python exception if an error occurred */
|
|
|
|
static PyObject *
|
2001-08-16 18:59:46 -03:00
|
|
|
set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
|
1992-06-23 06:07:03 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int i, j, count=0;
|
|
|
|
PyObject *list, *o;
|
|
|
|
SOCKET fd;
|
|
|
|
|
|
|
|
for (j = 0; fd2obj[j].sentinel >= 0; j++) {
|
|
|
|
if (FD_ISSET(fd2obj[j].fd, set))
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
list = PyList_New(count);
|
|
|
|
if (!list)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for (j = 0; fd2obj[j].sentinel >= 0; j++) {
|
|
|
|
fd = fd2obj[j].fd;
|
|
|
|
if (FD_ISSET(fd, set)) {
|
|
|
|
o = fd2obj[j].obj;
|
|
|
|
fd2obj[j].obj = NULL;
|
|
|
|
/* transfer ownership */
|
|
|
|
if (PyList_SetItem(list, i, o) < 0)
|
|
|
|
goto finally;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
1996-12-12 18:16:21 -04:00
|
|
|
finally:
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_DECREF(list);
|
|
|
|
return NULL;
|
1992-06-23 06:07:03 -03:00
|
|
|
}
|
1996-12-12 18:16:21 -04:00
|
|
|
|
2001-08-16 13:52:59 -03:00
|
|
|
#undef SELECT_USES_HEAP
|
|
|
|
#if FD_SETSIZE > 1024
|
|
|
|
#define SELECT_USES_HEAP
|
|
|
|
#endif /* FD_SETSIZE > 1024 */
|
|
|
|
|
1996-12-11 20:04:35 -04:00
|
|
|
static PyObject *
|
2000-07-10 09:29:26 -03:00
|
|
|
select_select(PyObject *self, PyObject *args)
|
1992-06-23 06:07:03 -03:00
|
|
|
{
|
2001-08-16 13:52:59 -03:00
|
|
|
#ifdef SELECT_USES_HEAP
|
2010-05-09 11:46:46 -03:00
|
|
|
pylist *rfd2obj, *wfd2obj, *efd2obj;
|
2001-08-16 13:52:59 -03:00
|
|
|
#else /* !SELECT_USES_HEAP */
|
2010-05-09 11:46:46 -03:00
|
|
|
/* XXX: All this should probably be implemented as follows:
|
|
|
|
* - find the highest descriptor we're interested in
|
|
|
|
* - add one
|
|
|
|
* - that's the size
|
|
|
|
* See: Stevens, APitUE, $12.5.1
|
|
|
|
*/
|
|
|
|
pylist rfd2obj[FD_SETSIZE + 1];
|
|
|
|
pylist wfd2obj[FD_SETSIZE + 1];
|
|
|
|
pylist efd2obj[FD_SETSIZE + 1];
|
2001-08-16 13:52:59 -03:00
|
|
|
#endif /* SELECT_USES_HEAP */
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *ifdlist, *ofdlist, *efdlist;
|
|
|
|
PyObject *ret = NULL;
|
|
|
|
PyObject *tout = Py_None;
|
|
|
|
fd_set ifdset, ofdset, efdset;
|
|
|
|
double timeout;
|
|
|
|
struct timeval tv, *tvp;
|
|
|
|
long seconds;
|
|
|
|
int imax, omax, emax, max;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* convert arguments */
|
|
|
|
if (!PyArg_UnpackTuple(args, "select", 3, 4,
|
|
|
|
&ifdlist, &ofdlist, &efdlist, &tout))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (tout == Py_None)
|
|
|
|
tvp = (struct timeval *)0;
|
|
|
|
else if (!PyNumber_Check(tout)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"timeout must be a float or None");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
timeout = PyFloat_AsDouble(tout);
|
|
|
|
if (timeout == -1 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
if (timeout > (double)LONG_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"timeout period too long");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
seconds = (long)timeout;
|
|
|
|
timeout = timeout - (double)seconds;
|
|
|
|
tv.tv_sec = seconds;
|
|
|
|
tv.tv_usec = (long)(timeout * 1E6);
|
|
|
|
tvp = &tv;
|
|
|
|
}
|
1996-12-11 20:04:35 -04:00
|
|
|
|
|
|
|
|
2001-08-16 13:52:59 -03:00
|
|
|
#ifdef SELECT_USES_HEAP
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Allocate memory for the lists */
|
|
|
|
rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
|
|
|
|
wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
|
|
|
|
efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
|
|
|
|
if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
|
|
|
|
if (rfd2obj) PyMem_DEL(rfd2obj);
|
|
|
|
if (wfd2obj) PyMem_DEL(wfd2obj);
|
|
|
|
if (efd2obj) PyMem_DEL(efd2obj);
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
}
|
2001-08-16 13:52:59 -03:00
|
|
|
#endif /* SELECT_USES_HEAP */
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Convert sequences to fd_sets, and get maximum fd number
|
|
|
|
* propagates the Python exception set in seq2set()
|
|
|
|
*/
|
|
|
|
rfd2obj[0].sentinel = -1;
|
|
|
|
wfd2obj[0].sentinel = -1;
|
|
|
|
efd2obj[0].sentinel = -1;
|
|
|
|
if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
|
|
|
|
goto finally;
|
|
|
|
if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
|
|
|
|
goto finally;
|
|
|
|
if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
|
|
|
|
goto finally;
|
|
|
|
max = imax;
|
|
|
|
if (omax > max) max = omax;
|
|
|
|
if (emax > max) max = emax;
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
n = select(max, &ifdset, &ofdset, &efdset, tvp);
|
|
|
|
Py_END_ALLOW_THREADS
|
1996-12-11 20:04:35 -04:00
|
|
|
|
2002-09-24 13:51:00 -03:00
|
|
|
#ifdef MS_WINDOWS
|
2010-05-09 11:46:46 -03:00
|
|
|
if (n == SOCKET_ERROR) {
|
|
|
|
PyErr_SetExcFromWindowsErr(SelectError, WSAGetLastError());
|
|
|
|
}
|
2002-09-24 13:51:00 -03:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
if (n < 0) {
|
|
|
|
PyErr_SetFromErrno(SelectError);
|
|
|
|
}
|
2002-09-24 13:51:00 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
else {
|
|
|
|
/* any of these three calls can raise an exception. it's more
|
|
|
|
convenient to test for this after all three calls... but
|
|
|
|
is that acceptable?
|
|
|
|
*/
|
|
|
|
ifdlist = set2list(&ifdset, rfd2obj);
|
|
|
|
ofdlist = set2list(&ofdset, wfd2obj);
|
|
|
|
efdlist = set2list(&efdset, efd2obj);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
ret = NULL;
|
|
|
|
else
|
|
|
|
ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
|
|
|
|
|
|
|
|
Py_DECREF(ifdlist);
|
|
|
|
Py_DECREF(ofdlist);
|
|
|
|
Py_DECREF(efdlist);
|
|
|
|
}
|
|
|
|
|
1996-12-12 18:16:21 -04:00
|
|
|
finally:
|
2010-05-09 11:46:46 -03:00
|
|
|
reap_obj(rfd2obj);
|
|
|
|
reap_obj(wfd2obj);
|
|
|
|
reap_obj(efd2obj);
|
2001-08-16 13:52:59 -03:00
|
|
|
#ifdef SELECT_USES_HEAP
|
2010-05-09 11:46:46 -03:00
|
|
|
PyMem_DEL(rfd2obj);
|
|
|
|
PyMem_DEL(wfd2obj);
|
|
|
|
PyMem_DEL(efd2obj);
|
2001-08-16 13:52:59 -03:00
|
|
|
#endif /* SELECT_USES_HEAP */
|
2010-05-09 11:46:46 -03:00
|
|
|
return ret;
|
1992-06-23 06:07:03 -03:00
|
|
|
}
|
|
|
|
|
2004-03-21 19:45:42 -04:00
|
|
|
#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
|
2010-05-09 11:46:46 -03:00
|
|
|
/*
|
2000-08-24 22:15:33 -03:00
|
|
|
* poll() support
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *dict;
|
|
|
|
int ufd_uptodate;
|
|
|
|
int ufd_len;
|
|
|
|
struct pollfd *ufds;
|
2000-08-24 22:15:33 -03:00
|
|
|
} pollObject;
|
|
|
|
|
2002-07-17 13:30:39 -03:00
|
|
|
static PyTypeObject poll_Type;
|
2000-08-24 22:15:33 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Update the malloc'ed array of pollfds to match the dictionary
|
2000-08-24 22:15:33 -03:00
|
|
|
contained within a pollObject. Return 1 on success, 0 on an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
update_ufd_array(pollObject *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_ssize_t i, pos;
|
|
|
|
PyObject *key, *value;
|
|
|
|
struct pollfd *old_ufds = self->ufds;
|
|
|
|
|
|
|
|
self->ufd_len = PyDict_Size(self->dict);
|
|
|
|
PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
|
|
|
|
if (self->ufds == NULL) {
|
|
|
|
self->ufds = old_ufds;
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = pos = 0;
|
|
|
|
while (PyDict_Next(self->dict, &pos, &key, &value)) {
|
|
|
|
self->ufds[i].fd = PyInt_AsLong(key);
|
|
|
|
self->ufds[i].events = (short)PyInt_AsLong(value);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
self->ufd_uptodate = 1;
|
|
|
|
return 1;
|
2000-08-24 22:15:33 -03:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(poll_register_doc,
|
2000-08-24 22:15:33 -03:00
|
|
|
"register(fd [, eventmask] ) -> None\n\n\
|
|
|
|
Register a file descriptor with the polling object.\n\
|
2001-08-16 13:55:10 -03:00
|
|
|
fd -- either an integer, or an object with a fileno() method returning an\n\
|
|
|
|
int.\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
events -- an optional bitmask describing the type of events to check for");
|
2000-08-24 22:15:33 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2010-05-09 11:46:46 -03:00
|
|
|
poll_register(pollObject *self, PyObject *args)
|
2000-08-24 22:15:33 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *o, *key, *value;
|
|
|
|
int fd, events = POLLIN | POLLPRI | POLLOUT;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = PyObject_AsFileDescriptor(o);
|
|
|
|
if (fd == -1) return NULL;
|
|
|
|
|
|
|
|
/* Add entry to the internal dictionary: the key is the
|
|
|
|
file descriptor, and the value is the event mask. */
|
|
|
|
key = PyInt_FromLong(fd);
|
|
|
|
if (key == NULL)
|
|
|
|
return NULL;
|
|
|
|
value = PyInt_FromLong(events);
|
|
|
|
if (value == NULL) {
|
|
|
|
Py_DECREF(key);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
err = PyDict_SetItem(self->dict, key, value);
|
|
|
|
Py_DECREF(key);
|
|
|
|
Py_DECREF(value);
|
|
|
|
if (err < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
self->ufd_uptodate = 0;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
2000-08-24 22:15:33 -03:00
|
|
|
}
|
|
|
|
|
2008-03-21 20:49:44 -03:00
|
|
|
PyDoc_STRVAR(poll_modify_doc,
|
|
|
|
"modify(fd, eventmask) -> None\n\n\
|
2008-03-25 21:16:50 -03:00
|
|
|
Modify an already registered file descriptor.\n\
|
2008-03-21 20:49:44 -03:00
|
|
|
fd -- either an integer, or an object with a fileno() method returning an\n\
|
|
|
|
int.\n\
|
|
|
|
events -- an optional bitmask describing the type of events to check for");
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
poll_modify(pollObject *self, PyObject *args)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *o, *key, *value;
|
|
|
|
int fd, events;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "Oi:modify", &o, &events)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = PyObject_AsFileDescriptor(o);
|
|
|
|
if (fd == -1) return NULL;
|
|
|
|
|
|
|
|
/* Modify registered fd */
|
|
|
|
key = PyInt_FromLong(fd);
|
|
|
|
if (key == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (PyDict_GetItem(self->dict, key) == NULL) {
|
|
|
|
errno = ENOENT;
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
value = PyInt_FromLong(events);
|
|
|
|
if (value == NULL) {
|
|
|
|
Py_DECREF(key);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
err = PyDict_SetItem(self->dict, key, value);
|
|
|
|
Py_DECREF(key);
|
|
|
|
Py_DECREF(value);
|
|
|
|
if (err < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
self->ufd_uptodate = 0;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(poll_unregister_doc,
|
2000-08-24 22:15:33 -03:00
|
|
|
"unregister(fd) -> None\n\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
Remove a file descriptor being tracked by the polling object.");
|
2000-08-24 22:15:33 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2010-05-09 11:46:46 -03:00
|
|
|
poll_unregister(pollObject *self, PyObject *o)
|
2000-08-24 22:15:33 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *key;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = PyObject_AsFileDescriptor( o );
|
|
|
|
if (fd == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Check whether the fd is already in the array */
|
|
|
|
key = PyInt_FromLong(fd);
|
|
|
|
if (key == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (PyDict_DelItem(self->dict, key) == -1) {
|
|
|
|
Py_DECREF(key);
|
|
|
|
/* This will simply raise the KeyError set by PyDict_DelItem
|
|
|
|
if the file descriptor isn't registered. */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(key);
|
|
|
|
self->ufd_uptodate = 0;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
2000-08-24 22:15:33 -03:00
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(poll_poll_doc,
|
2000-08-24 22:15:33 -03:00
|
|
|
"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
|
|
|
|
Polls the set of registered file descriptors, returning a list containing \n\
|
2002-06-13 17:33:02 -03:00
|
|
|
any descriptors that have events or errors to report.");
|
2000-08-24 22:15:33 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2010-05-09 11:46:46 -03:00
|
|
|
poll_poll(pollObject *self, PyObject *args)
|
2000-08-24 22:15:33 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *result_list = NULL, *tout = NULL;
|
|
|
|
int timeout = 0, poll_result, i, j;
|
|
|
|
PyObject *value = NULL, *num = NULL;
|
|
|
|
|
|
|
|
if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check values for timeout */
|
|
|
|
if (tout == NULL || tout == Py_None)
|
|
|
|
timeout = -1;
|
|
|
|
else if (!PyNumber_Check(tout)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"timeout must be an integer or None");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tout = PyNumber_Int(tout);
|
|
|
|
if (!tout)
|
|
|
|
return NULL;
|
|
|
|
timeout = PyInt_AsLong(tout);
|
|
|
|
Py_DECREF(tout);
|
|
|
|
if (timeout == -1 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure the ufd array is up to date */
|
|
|
|
if (!self->ufd_uptodate)
|
|
|
|
if (update_ufd_array(self) == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* call poll() */
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
poll_result = poll(self->ufds, self->ufd_len, timeout);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (poll_result < 0) {
|
|
|
|
PyErr_SetFromErrno(SelectError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the result list */
|
|
|
|
|
|
|
|
result_list = PyList_New(poll_result);
|
|
|
|
if (!result_list)
|
|
|
|
return NULL;
|
|
|
|
else {
|
|
|
|
for (i = 0, j = 0; j < poll_result; j++) {
|
|
|
|
/* skip to the next fired descriptor */
|
|
|
|
while (!self->ufds[i].revents) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
/* if we hit a NULL return, set value to NULL
|
|
|
|
and break out of loop; code at end will
|
|
|
|
clean up result_list */
|
|
|
|
value = PyTuple_New(2);
|
|
|
|
if (value == NULL)
|
|
|
|
goto error;
|
|
|
|
num = PyInt_FromLong(self->ufds[i].fd);
|
|
|
|
if (num == NULL) {
|
|
|
|
Py_DECREF(value);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(value, 0, num);
|
|
|
|
|
|
|
|
/* The &0xffff is a workaround for AIX. 'revents'
|
|
|
|
is a 16-bit short, and IBM assigned POLLNVAL
|
|
|
|
to be 0x8000, so the conversion to int results
|
|
|
|
in a negative number. See SF bug #923315. */
|
|
|
|
num = PyInt_FromLong(self->ufds[i].revents & 0xffff);
|
|
|
|
if (num == NULL) {
|
|
|
|
Py_DECREF(value);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(value, 1, num);
|
|
|
|
if ((PyList_SetItem(result_list, j, value)) == -1) {
|
|
|
|
Py_DECREF(value);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result_list;
|
2000-08-24 22:15:33 -03:00
|
|
|
|
|
|
|
error:
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_DECREF(result_list);
|
|
|
|
return NULL;
|
2000-08-24 22:15:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef poll_methods[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"register", (PyCFunction)poll_register,
|
|
|
|
METH_VARARGS, poll_register_doc},
|
|
|
|
{"modify", (PyCFunction)poll_modify,
|
|
|
|
METH_VARARGS, poll_modify_doc},
|
|
|
|
{"unregister", (PyCFunction)poll_unregister,
|
|
|
|
METH_O, poll_unregister_doc},
|
|
|
|
{"poll", (PyCFunction)poll_poll,
|
|
|
|
METH_VARARGS, poll_poll_doc},
|
|
|
|
{NULL, NULL} /* sentinel */
|
2000-08-24 22:15:33 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static pollObject *
|
2000-08-31 02:18:54 -03:00
|
|
|
newPollObject(void)
|
2000-08-24 22:15:33 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
pollObject *self;
|
|
|
|
self = PyObject_New(pollObject, &poll_Type);
|
|
|
|
if (self == NULL)
|
|
|
|
return NULL;
|
|
|
|
/* ufd_uptodate is a Boolean, denoting whether the
|
|
|
|
array pointed to by ufds matches the contents of the dictionary. */
|
|
|
|
self->ufd_uptodate = 0;
|
|
|
|
self->ufds = NULL;
|
|
|
|
self->dict = PyDict_New();
|
|
|
|
if (self->dict == NULL) {
|
|
|
|
Py_DECREF(self);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return self;
|
2000-08-24 22:15:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
poll_dealloc(pollObject *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (self->ufds != NULL)
|
|
|
|
PyMem_DEL(self->ufds);
|
|
|
|
Py_XDECREF(self->dict);
|
|
|
|
PyObject_Del(self);
|
2000-08-24 22:15:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
poll_getattr(pollObject *self, char *name)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return Py_FindMethod(poll_methods, (PyObject *)self, name);
|
2000-08-24 22:15:33 -03:00
|
|
|
}
|
|
|
|
|
2002-07-17 13:49:03 -03:00
|
|
|
static PyTypeObject poll_Type = {
|
2010-05-09 11:46:46 -03:00
|
|
|
/* The ob_type field must be initialized in the module init function
|
|
|
|
* to be portable to Windows without using C++. */
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
"select.poll", /*tp_name*/
|
|
|
|
sizeof(pollObject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
(destructor)poll_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
(getattrfunc)poll_getattr, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash*/
|
2000-08-24 22:15:33 -03:00
|
|
|
};
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(poll_doc,
|
2000-08-24 22:15:33 -03:00
|
|
|
"Returns a polling object, which supports registering and\n\
|
2002-06-13 17:33:02 -03:00
|
|
|
unregistering file descriptors, and then polling them for I/O events.");
|
2000-08-24 22:15:33 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2006-05-29 18:04:52 -03:00
|
|
|
select_poll(PyObject *self, PyObject *unused)
|
2000-08-24 22:15:33 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
return (PyObject *)newPollObject();
|
2000-08-24 22:15:33 -03:00
|
|
|
}
|
2006-04-23 09:36:23 -03:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2010-05-09 11:46:46 -03:00
|
|
|
/*
|
2006-04-23 09:36:23 -03:00
|
|
|
* On some systems poll() sets errno on invalid file descriptors. We test
|
|
|
|
* for this at runtime because this bug may be fixed or introduced between
|
|
|
|
* OS releases.
|
|
|
|
*/
|
|
|
|
static int select_have_broken_poll(void)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int poll_test;
|
|
|
|
int filedes[2];
|
|
|
|
|
|
|
|
struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
|
|
|
|
|
|
|
|
/* Create a file descriptor to make invalid */
|
|
|
|
if (pipe(filedes) < 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
poll_struct.fd = filedes[0];
|
|
|
|
close(filedes[0]);
|
|
|
|
close(filedes[1]);
|
|
|
|
poll_test = poll(&poll_struct, 1, 0);
|
|
|
|
if (poll_test < 0) {
|
|
|
|
return 1;
|
|
|
|
} else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2006-04-23 09:36:23 -03:00
|
|
|
}
|
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
|
|
|
#endif /* HAVE_POLL */
|
2000-08-24 22:15:33 -03:00
|
|
|
|
2008-03-21 20:49:44 -03:00
|
|
|
#ifdef HAVE_EPOLL
|
|
|
|
/* **************************************************************************
|
|
|
|
* epoll interface for Linux 2.6
|
|
|
|
*
|
|
|
|
* Written by Christian Heimes
|
|
|
|
* Inspired by Twisted's _epoll.pyx and select.poll()
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_EPOLL_H
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
SOCKET epfd; /* epoll control file descriptor */
|
2008-03-21 20:49:44 -03:00
|
|
|
} pyEpoll_Object;
|
|
|
|
|
|
|
|
static PyTypeObject pyEpoll_Type;
|
|
|
|
#define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type))
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
pyepoll_err_closed(void)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll fd");
|
|
|
|
return NULL;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pyepoll_internal_close(pyEpoll_Object *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int save_errno = 0;
|
|
|
|
if (self->epfd >= 0) {
|
|
|
|
int epfd = self->epfd;
|
|
|
|
self->epfd = -1;
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
if (close(epfd) < 0)
|
|
|
|
save_errno = errno;
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
}
|
|
|
|
return save_errno;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
newPyEpoll_Object(PyTypeObject *type, int sizehint, SOCKET fd)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
pyEpoll_Object *self;
|
|
|
|
|
|
|
|
if (sizehint == -1) {
|
|
|
|
sizehint = FD_SETSIZE-1;
|
|
|
|
}
|
|
|
|
else if (sizehint < 1) {
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"sizehint must be greater zero, got %d",
|
|
|
|
sizehint);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(type != NULL && type->tp_alloc != NULL);
|
|
|
|
self = (pyEpoll_Object *) type->tp_alloc(type, 0);
|
|
|
|
if (self == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
self->epfd = epoll_create(sizehint);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self->epfd = fd;
|
|
|
|
}
|
|
|
|
if (self->epfd < 0) {
|
|
|
|
Py_DECREF(self);
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (PyObject *)self;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int sizehint = -1;
|
|
|
|
static char *kwlist[] = {"sizehint", NULL};
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:epoll", kwlist,
|
|
|
|
&sizehint))
|
|
|
|
return NULL;
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return newPyEpoll_Object(type, sizehint, -1);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
pyepoll_dealloc(pyEpoll_Object *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
(void)pyepoll_internal_close(self);
|
|
|
|
Py_TYPE(self)->tp_free(self);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
pyepoll_close(pyEpoll_Object *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
errno = pyepoll_internal_close(self);
|
|
|
|
if (errno < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pyepoll_close_doc,
|
|
|
|
"close() -> None\n\
|
|
|
|
\n\
|
|
|
|
Close the epoll control file descriptor. Further operations on the epoll\n\
|
|
|
|
object will raise an exception.");
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
pyepoll_get_closed(pyEpoll_Object *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (self->epfd < 0)
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
else
|
|
|
|
Py_RETURN_FALSE;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
pyepoll_fileno(pyEpoll_Object *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (self->epfd < 0)
|
|
|
|
return pyepoll_err_closed();
|
|
|
|
return PyInt_FromLong(self->epfd);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pyepoll_fileno_doc,
|
|
|
|
"fileno() -> int\n\
|
|
|
|
\n\
|
|
|
|
Return the epoll control file descriptor.");
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
pyepoll_fromfd(PyObject *cls, PyObject *args)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
SOCKET fd;
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
|
|
|
|
return NULL;
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return newPyEpoll_Object((PyTypeObject*)cls, -1, fd);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pyepoll_fromfd_doc,
|
|
|
|
"fromfd(fd) -> epoll\n\
|
|
|
|
\n\
|
|
|
|
Create an epoll object from a given control fd.");
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
struct epoll_event ev;
|
|
|
|
int result;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (epfd < 0)
|
|
|
|
return pyepoll_err_closed();
|
|
|
|
|
|
|
|
fd = PyObject_AsFileDescriptor(pfd);
|
|
|
|
if (fd == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(op) {
|
|
|
|
case EPOLL_CTL_ADD:
|
|
|
|
case EPOLL_CTL_MOD:
|
|
|
|
ev.events = events;
|
|
|
|
ev.data.fd = fd;
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
result = epoll_ctl(epfd, op, fd, &ev);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
break;
|
|
|
|
case EPOLL_CTL_DEL:
|
|
|
|
/* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
|
|
|
|
* operation required a non-NULL pointer in event, even
|
|
|
|
* though this argument is ignored. */
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
result = epoll_ctl(epfd, op, fd, &ev);
|
|
|
|
if (errno == EBADF) {
|
|
|
|
/* fd already closed */
|
|
|
|
result = 0;
|
|
|
|
errno = 0;
|
|
|
|
}
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = -1;
|
|
|
|
errno = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *pfd;
|
|
|
|
unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
|
|
|
|
static char *kwlist[] = {"fd", "eventmask", NULL};
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
|
|
|
|
&pfd, &events)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pyepoll_register_doc,
|
Merged revisions 83536,83546-83548,83550,83554-83555,83558,83563,83565,83571,83574-83575 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r83536 | georg.brandl | 2010-08-02 19:49:25 +0200 (Mo, 02 Aug 2010) | 1 line
#8578: mention danger of not incref'ing weak referenced object.
........
r83546 | georg.brandl | 2010-08-02 21:16:34 +0200 (Mo, 02 Aug 2010) | 1 line
#7973: Fix distutils options spelling.
........
r83547 | georg.brandl | 2010-08-02 21:19:26 +0200 (Mo, 02 Aug 2010) | 1 line
#7386: add example that shows that trailing path separators are stripped.
........
r83548 | georg.brandl | 2010-08-02 21:23:34 +0200 (Mo, 02 Aug 2010) | 1 line
#8172: how does one use a property?
........
r83550 | georg.brandl | 2010-08-02 21:32:43 +0200 (Mo, 02 Aug 2010) | 1 line
#9451: strengthen warning about __*__ special name usage.
........
r83554 | georg.brandl | 2010-08-02 21:43:05 +0200 (Mo, 02 Aug 2010) | 1 line
#7280: note about nasmw.exe.
........
r83555 | georg.brandl | 2010-08-02 21:44:48 +0200 (Mo, 02 Aug 2010) | 1 line
#8861: remove unused variable.
........
r83558 | georg.brandl | 2010-08-02 22:05:19 +0200 (Mo, 02 Aug 2010) | 1 line
#8648: document UTF-7 codec functions.
........
r83563 | georg.brandl | 2010-08-02 22:21:21 +0200 (Mo, 02 Aug 2010) | 1 line
#9037: add example how to raise custom exceptions from C code.
........
r83565 | georg.brandl | 2010-08-02 22:27:20 +0200 (Mo, 02 Aug 2010) | 1 line
#9111: document that do_help() looks at docstrings.
........
r83571 | georg.brandl | 2010-08-02 22:44:34 +0200 (Mo, 02 Aug 2010) | 1 line
Clarify that abs() is not a namespace.
........
r83574 | georg.brandl | 2010-08-02 22:47:56 +0200 (Mo, 02 Aug 2010) | 1 line
#6867: epoll.register() returns None.
........
r83575 | georg.brandl | 2010-08-02 22:52:10 +0200 (Mo, 02 Aug 2010) | 1 line
#9238: zipfile does handle archive comments.
........
2010-08-02 18:44:25 -03:00
|
|
|
"register(fd[, eventmask]) -> None\n\
|
2008-03-21 20:49:44 -03:00
|
|
|
\n\
|
2011-06-27 03:50:35 -03:00
|
|
|
Registers a new fd or raises an IOError if the fd is already registered.\n\
|
2008-03-25 21:16:50 -03:00
|
|
|
fd is the target file descriptor of the operation.\n\
|
|
|
|
events is a bit set composed of the various EPOLL constants; the default\n\
|
2008-03-21 20:49:44 -03:00
|
|
|
is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\
|
|
|
|
\n\
|
|
|
|
The epoll interface supports all file descriptors that support poll.");
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *pfd;
|
|
|
|
unsigned int events;
|
|
|
|
static char *kwlist[] = {"fd", "eventmask", NULL};
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,
|
|
|
|
&pfd, &events)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pyepoll_modify_doc,
|
|
|
|
"modify(fd, eventmask) -> None\n\
|
|
|
|
\n\
|
|
|
|
fd is the target file descriptor of the operation\n\
|
|
|
|
events is a bit set composed of the various EPOLL constants");
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *pfd;
|
|
|
|
static char *kwlist[] = {"fd", NULL};
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,
|
|
|
|
&pfd)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pyepoll_unregister_doc,
|
|
|
|
"unregister(fd) -> None\n\
|
|
|
|
\n\
|
|
|
|
fd is the target file descriptor of the operation.");
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
double dtimeout = -1.;
|
|
|
|
int timeout;
|
|
|
|
int maxevents = -1;
|
|
|
|
int nfds, i;
|
|
|
|
PyObject *elist = NULL, *etuple = NULL;
|
|
|
|
struct epoll_event *evs = NULL;
|
|
|
|
static char *kwlist[] = {"timeout", "maxevents", NULL};
|
|
|
|
|
|
|
|
if (self->epfd < 0)
|
|
|
|
return pyepoll_err_closed();
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,
|
|
|
|
&dtimeout, &maxevents)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dtimeout < 0) {
|
|
|
|
timeout = -1;
|
|
|
|
}
|
|
|
|
else if (dtimeout * 1000.0 > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"timeout is too large");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
timeout = (int)(dtimeout * 1000.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxevents == -1) {
|
|
|
|
maxevents = FD_SETSIZE-1;
|
|
|
|
}
|
|
|
|
else if (maxevents < 1) {
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"maxevents must be greater than 0, got %d",
|
|
|
|
maxevents);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
evs = PyMem_New(struct epoll_event, maxevents);
|
|
|
|
if (evs == NULL) {
|
|
|
|
Py_DECREF(self);
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (nfds < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
elist = PyList_New(nfds);
|
|
|
|
if (elist == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
|
|
|
|
if (etuple == NULL) {
|
|
|
|
Py_CLEAR(elist);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
PyList_SET_ITEM(elist, i, etuple);
|
|
|
|
}
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2008-03-26 09:57:47 -03:00
|
|
|
error:
|
2010-05-09 11:46:46 -03:00
|
|
|
PyMem_Free(evs);
|
|
|
|
return elist;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pyepoll_poll_doc,
|
|
|
|
"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
|
|
|
|
\n\
|
|
|
|
Wait for events on the epoll file descriptor for a maximum time of timeout\n\
|
|
|
|
in seconds (as float). -1 makes poll wait indefinitely.\n\
|
|
|
|
Up to maxevents are returned to the caller.");
|
|
|
|
|
|
|
|
static PyMethodDef pyepoll_methods[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"fromfd", (PyCFunction)pyepoll_fromfd,
|
|
|
|
METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
|
|
|
|
{"close", (PyCFunction)pyepoll_close, METH_NOARGS,
|
|
|
|
pyepoll_close_doc},
|
|
|
|
{"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
|
|
|
|
pyepoll_fileno_doc},
|
|
|
|
{"modify", (PyCFunction)pyepoll_modify,
|
|
|
|
METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
|
|
|
|
{"register", (PyCFunction)pyepoll_register,
|
|
|
|
METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
|
|
|
|
{"unregister", (PyCFunction)pyepoll_unregister,
|
|
|
|
METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
|
|
|
|
{"poll", (PyCFunction)pyepoll_poll,
|
|
|
|
METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
|
|
|
|
{NULL, NULL},
|
2008-03-21 20:49:44 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyGetSetDef pyepoll_getsetlist[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"closed", (getter)pyepoll_get_closed, NULL,
|
|
|
|
"True if the epoll handler is closed"},
|
|
|
|
{0},
|
2008-03-21 20:49:44 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pyepoll_doc,
|
|
|
|
"select.epoll([sizehint=-1])\n\
|
|
|
|
\n\
|
|
|
|
Returns an epolling object\n\
|
|
|
|
\n\
|
|
|
|
sizehint must be a positive integer or -1 for the default size. The\n\
|
|
|
|
sizehint is used to optimize internal data structures. It doesn't limit\n\
|
|
|
|
the maximum number of monitored events.");
|
|
|
|
|
|
|
|
static PyTypeObject pyEpoll_Type = {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
"select.epoll", /* tp_name */
|
|
|
|
sizeof(pyEpoll_Object), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
(destructor)pyepoll_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
|
pyepoll_doc, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
pyepoll_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
pyepoll_getsetlist, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
pyepoll_new, /* tp_new */
|
|
|
|
0, /* tp_free */
|
2008-03-21 20:49:44 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* HAVE_EPOLL */
|
|
|
|
|
|
|
|
#ifdef HAVE_KQUEUE
|
|
|
|
/* **************************************************************************
|
|
|
|
* kqueue interface for BSD
|
|
|
|
*
|
|
|
|
* Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_EVENT_H
|
|
|
|
#include <sys/event.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PyDoc_STRVAR(kqueue_event_doc,
|
2009-12-29 17:09:17 -04:00
|
|
|
"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
|
2008-03-21 20:49:44 -03:00
|
|
|
\n\
|
|
|
|
This object is the equivalent of the struct kevent for the C API.\n\
|
|
|
|
\n\
|
|
|
|
See the kqueue manpage for more detailed information about the meaning\n\
|
|
|
|
of the arguments.\n\
|
|
|
|
\n\
|
|
|
|
One minor note: while you might hope that udata could store a\n\
|
|
|
|
reference to a python object, it cannot, because it is impossible to\n\
|
|
|
|
keep a proper reference count of the object once it's passed into the\n\
|
|
|
|
kernel. Therefore, I have restricted it to only storing an integer. I\n\
|
|
|
|
recommend ignoring it and simply using the 'ident' field to key off\n\
|
|
|
|
of. You could also set up a dictionary on the python side to store a\n\
|
|
|
|
udata->object mapping.");
|
|
|
|
|
|
|
|
typedef struct {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
struct kevent e;
|
2008-03-21 20:49:44 -03:00
|
|
|
} kqueue_event_Object;
|
|
|
|
|
|
|
|
static PyTypeObject kqueue_event_Type;
|
|
|
|
|
|
|
|
#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
|
|
|
|
|
|
|
|
typedef struct {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
SOCKET kqfd; /* kqueue control fd */
|
2008-03-21 20:49:44 -03:00
|
|
|
} kqueue_queue_Object;
|
|
|
|
|
|
|
|
static PyTypeObject kqueue_queue_Type;
|
|
|
|
|
|
|
|
#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
|
|
|
|
|
2009-11-04 15:25:14 -04:00
|
|
|
#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
|
|
|
|
# error uintptr_t does not match void *!
|
|
|
|
#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
|
|
|
|
# define T_UINTPTRT T_ULONGLONG
|
|
|
|
# define T_INTPTRT T_LONGLONG
|
|
|
|
# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
|
|
|
|
# define UINTPTRT_FMT_UNIT "K"
|
|
|
|
# define INTPTRT_FMT_UNIT "L"
|
|
|
|
#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
|
|
|
|
# define T_UINTPTRT T_ULONG
|
|
|
|
# define T_INTPTRT T_LONG
|
|
|
|
# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
|
|
|
|
# define UINTPTRT_FMT_UNIT "k"
|
|
|
|
# define INTPTRT_FMT_UNIT "l"
|
|
|
|
#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
|
|
|
|
# define T_UINTPTRT T_UINT
|
|
|
|
# define T_INTPTRT T_INT
|
|
|
|
# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
|
|
|
|
# define UINTPTRT_FMT_UNIT "I"
|
|
|
|
# define INTPTRT_FMT_UNIT "i"
|
|
|
|
#else
|
|
|
|
# error uintptr_t does not match int, long, or long long!
|
|
|
|
#endif
|
|
|
|
|
2008-03-21 20:49:44 -03:00
|
|
|
/* Unfortunately, we can't store python objects in udata, because
|
|
|
|
* kevents in the kernel can be removed without warning, which would
|
|
|
|
* forever lose the refcount on the object stored with it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
|
|
|
|
static struct PyMemberDef kqueue_event_members[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"ident", T_UINTPTRT, KQ_OFF(e.ident)},
|
|
|
|
{"filter", T_SHORT, KQ_OFF(e.filter)},
|
|
|
|
{"flags", T_USHORT, KQ_OFF(e.flags)},
|
|
|
|
{"fflags", T_UINT, KQ_OFF(e.fflags)},
|
|
|
|
{"data", T_INTPTRT, KQ_OFF(e.data)},
|
|
|
|
{"udata", T_UINTPTRT, KQ_OFF(e.udata)},
|
|
|
|
{NULL} /* Sentinel */
|
2008-03-21 20:49:44 -03:00
|
|
|
};
|
|
|
|
#undef KQ_OFF
|
|
|
|
|
|
|
|
static PyObject *
|
2010-02-23 17:48:57 -04:00
|
|
|
|
2008-03-21 20:49:44 -03:00
|
|
|
kqueue_event_repr(kqueue_event_Object *s)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
char buf[1024];
|
|
|
|
PyOS_snprintf(
|
|
|
|
buf, sizeof(buf),
|
|
|
|
"<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
|
|
|
|
"data=0x%zd udata=%p>",
|
|
|
|
(size_t)(s->e.ident), s->e.filter, s->e.flags,
|
|
|
|
s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
|
|
|
|
return PyString_FromString(buf);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *pfd;
|
|
|
|
static char *kwlist[] = {"ident", "filter", "flags", "fflags",
|
|
|
|
"data", "udata", NULL};
|
|
|
|
static char *fmt = "O|hhi" INTPTRT_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
|
|
|
|
|
|
|
|
EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
|
|
|
|
&pfd, &(self->e.filter), &(self->e.flags),
|
|
|
|
&(self->e.fflags), &(self->e.data), &(self->e.udata))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyLong_Check(pfd)) {
|
|
|
|
self->e.ident = PyLong_AsUintptr_t(pfd);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self->e.ident = PyObject_AsFileDescriptor(pfd);
|
|
|
|
}
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
|
2010-05-09 11:46:46 -03:00
|
|
|
int op)
|
2008-03-21 20:49:44 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_intptr_t result = 0;
|
|
|
|
|
|
|
|
if (!kqueue_event_Check(o)) {
|
|
|
|
if (op == Py_EQ || op == Py_NE) {
|
|
|
|
PyObject *res = op == Py_EQ ? Py_False : Py_True;
|
|
|
|
Py_INCREF(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"can't compare %.200s to %.200s",
|
|
|
|
Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (((result = s->e.ident - o->e.ident) == 0) &&
|
|
|
|
((result = s->e.filter - o->e.filter) == 0) &&
|
|
|
|
((result = s->e.flags - o->e.flags) == 0) &&
|
|
|
|
((result = s->e.fflags - o->e.fflags) == 0) &&
|
|
|
|
((result = s->e.data - o->e.data) == 0) &&
|
|
|
|
((result = s->e.udata - o->e.udata) == 0)
|
|
|
|
) {
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case Py_EQ:
|
|
|
|
result = (result == 0);
|
|
|
|
break;
|
|
|
|
case Py_NE:
|
|
|
|
result = (result != 0);
|
|
|
|
break;
|
|
|
|
case Py_LE:
|
|
|
|
result = (result <= 0);
|
|
|
|
break;
|
|
|
|
case Py_GE:
|
|
|
|
result = (result >= 0);
|
|
|
|
break;
|
|
|
|
case Py_LT:
|
|
|
|
result = (result < 0);
|
|
|
|
break;
|
|
|
|
case Py_GT:
|
|
|
|
result = (result > 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return PyBool_FromLong((long)result);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyTypeObject kqueue_event_Type = {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
"select.kevent", /* tp_name */
|
|
|
|
sizeof(kqueue_event_Object), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
0, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
(reprfunc)kqueue_event_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
|
kqueue_event_doc, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
(richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
kqueue_event_members, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
(initproc)kqueue_event_init, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
0, /* tp_new */
|
|
|
|
0, /* tp_free */
|
2008-03-21 20:49:44 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
kqueue_queue_err_closed(void)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue fd");
|
|
|
|
return NULL;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kqueue_queue_internal_close(kqueue_queue_Object *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int save_errno = 0;
|
|
|
|
if (self->kqfd >= 0) {
|
|
|
|
int kqfd = self->kqfd;
|
|
|
|
self->kqfd = -1;
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
if (close(kqfd) < 0)
|
|
|
|
save_errno = errno;
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
}
|
|
|
|
return save_errno;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
newKqueue_Object(PyTypeObject *type, SOCKET fd)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
kqueue_queue_Object *self;
|
|
|
|
assert(type != NULL && type->tp_alloc != NULL);
|
|
|
|
self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
|
|
|
|
if (self == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
self->kqfd = kqueue();
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self->kqfd = fd;
|
|
|
|
}
|
|
|
|
if (self->kqfd < 0) {
|
|
|
|
Py_DECREF(self);
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (PyObject *)self;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if ((args != NULL && PyObject_Size(args)) ||
|
|
|
|
(kwds != NULL && PyObject_Size(kwds))) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"select.kqueue doesn't accept arguments");
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return newKqueue_Object(type, -1);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kqueue_queue_dealloc(kqueue_queue_Object *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
kqueue_queue_internal_close(self);
|
|
|
|
Py_TYPE(self)->tp_free(self);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
kqueue_queue_close(kqueue_queue_Object *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
errno = kqueue_queue_internal_close(self);
|
|
|
|
if (errno < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(kqueue_queue_close_doc,
|
|
|
|
"close() -> None\n\
|
|
|
|
\n\
|
|
|
|
Close the kqueue control file descriptor. Further operations on the kqueue\n\
|
|
|
|
object will raise an exception.");
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
kqueue_queue_get_closed(kqueue_queue_Object *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (self->kqfd < 0)
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
else
|
|
|
|
Py_RETURN_FALSE;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
kqueue_queue_fileno(kqueue_queue_Object *self)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
if (self->kqfd < 0)
|
|
|
|
return kqueue_queue_err_closed();
|
|
|
|
return PyInt_FromLong(self->kqfd);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(kqueue_queue_fileno_doc,
|
|
|
|
"fileno() -> int\n\
|
|
|
|
\n\
|
|
|
|
Return the kqueue control file descriptor.");
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
kqueue_queue_fromfd(PyObject *cls, PyObject *args)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
SOCKET fd;
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
|
|
|
|
return NULL;
|
2008-03-21 20:49:44 -03:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
return newKqueue_Object((PyTypeObject*)cls, fd);
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(kqueue_queue_fromfd_doc,
|
|
|
|
"fromfd(fd) -> kqueue\n\
|
|
|
|
\n\
|
|
|
|
Create a kqueue object from a given control fd.");
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
int nevents = 0;
|
|
|
|
int gotevents = 0;
|
|
|
|
int nchanges = 0;
|
|
|
|
int i = 0;
|
|
|
|
PyObject *otimeout = NULL;
|
|
|
|
PyObject *ch = NULL;
|
|
|
|
PyObject *it = NULL, *ei = NULL;
|
|
|
|
PyObject *result = NULL;
|
|
|
|
struct kevent *evl = NULL;
|
|
|
|
struct kevent *chl = NULL;
|
|
|
|
struct timespec timeoutspec;
|
|
|
|
struct timespec *ptimeoutspec;
|
|
|
|
|
|
|
|
if (self->kqfd < 0)
|
|
|
|
return kqueue_queue_err_closed();
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (nevents < 0) {
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"Length of eventlist must be 0 or positive, got %d",
|
|
|
|
nevents);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (otimeout == Py_None || otimeout == NULL) {
|
|
|
|
ptimeoutspec = NULL;
|
|
|
|
}
|
|
|
|
else if (PyNumber_Check(otimeout)) {
|
|
|
|
double timeout;
|
|
|
|
long seconds;
|
|
|
|
|
|
|
|
timeout = PyFloat_AsDouble(otimeout);
|
|
|
|
if (timeout == -1 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
if (timeout > (double)LONG_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"timeout period too long");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (timeout < 0) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"timeout must be positive or None");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
seconds = (long)timeout;
|
|
|
|
timeout = timeout - (double)seconds;
|
|
|
|
timeoutspec.tv_sec = seconds;
|
|
|
|
timeoutspec.tv_nsec = (long)(timeout * 1E9);
|
|
|
|
ptimeoutspec = &timeoutspec;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"timeout argument must be an number "
|
|
|
|
"or None, got %.200s",
|
|
|
|
Py_TYPE(otimeout)->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch != NULL && ch != Py_None) {
|
|
|
|
it = PyObject_GetIter(ch);
|
|
|
|
if (it == NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"changelist is not iterable");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
nchanges = PyObject_Size(ch);
|
|
|
|
if (nchanges < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
chl = PyMem_New(struct kevent, nchanges);
|
|
|
|
if (chl == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
while ((ei = PyIter_Next(it)) != NULL) {
|
|
|
|
if (!kqueue_event_Check(ei)) {
|
|
|
|
Py_DECREF(ei);
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"changelist must be an iterable of "
|
|
|
|
"select.kevent objects");
|
|
|
|
goto error;
|
|
|
|
} else {
|
|
|
|
chl[i++] = ((kqueue_event_Object *)ei)->e;
|
|
|
|
}
|
|
|
|
Py_DECREF(ei);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_CLEAR(it);
|
|
|
|
|
|
|
|
/* event list */
|
|
|
|
if (nevents) {
|
|
|
|
evl = PyMem_New(struct kevent, nevents);
|
|
|
|
if (evl == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
gotevents = kevent(self->kqfd, chl, nchanges,
|
|
|
|
evl, nevents, ptimeoutspec);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (gotevents == -1) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = PyList_New(gotevents);
|
|
|
|
if (result == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < gotevents; i++) {
|
|
|
|
kqueue_event_Object *ch;
|
|
|
|
|
|
|
|
ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
|
|
|
|
if (ch == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
ch->e = evl[i];
|
|
|
|
PyList_SET_ITEM(result, i, (PyObject *)ch);
|
|
|
|
}
|
|
|
|
PyMem_Free(chl);
|
|
|
|
PyMem_Free(evl);
|
|
|
|
return result;
|
2008-03-21 20:49:44 -03:00
|
|
|
|
|
|
|
error:
|
2010-05-09 11:46:46 -03:00
|
|
|
PyMem_Free(chl);
|
|
|
|
PyMem_Free(evl);
|
|
|
|
Py_XDECREF(result);
|
|
|
|
Py_XDECREF(it);
|
|
|
|
return NULL;
|
2008-03-21 20:49:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyDoc_STRVAR(kqueue_queue_control_doc,
|
2008-09-21 04:14:44 -03:00
|
|
|
"control(changelist, max_events[, timeout=None]) -> eventlist\n\
|
2008-03-21 20:49:44 -03:00
|
|
|
\n\
|
|
|
|
Calls the kernel kevent function.\n\
|
|
|
|
- changelist must be a list of kevent objects describing the changes\n\
|
|
|
|
to be made to the kernel's watch list or None.\n\
|
|
|
|
- max_events lets you specify the maximum number of events that the\n\
|
|
|
|
kernel will return.\n\
|
|
|
|
- timeout is the maximum time to wait in seconds, or else None,\n\
|
|
|
|
to wait forever. timeout accepts floats for smaller timeouts, too.");
|
|
|
|
|
|
|
|
|
|
|
|
static PyMethodDef kqueue_queue_methods[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"fromfd", (PyCFunction)kqueue_queue_fromfd,
|
|
|
|
METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
|
|
|
|
{"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
|
|
|
|
kqueue_queue_close_doc},
|
|
|
|
{"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
|
|
|
|
kqueue_queue_fileno_doc},
|
|
|
|
{"control", (PyCFunction)kqueue_queue_control,
|
|
|
|
METH_VARARGS , kqueue_queue_control_doc},
|
|
|
|
{NULL, NULL},
|
2008-03-21 20:49:44 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyGetSetDef kqueue_queue_getsetlist[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"closed", (getter)kqueue_queue_get_closed, NULL,
|
|
|
|
"True if the kqueue handler is closed"},
|
|
|
|
{0},
|
2008-03-21 20:49:44 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(kqueue_queue_doc,
|
|
|
|
"Kqueue syscall wrapper.\n\
|
|
|
|
\n\
|
|
|
|
For example, to start watching a socket for input:\n\
|
|
|
|
>>> kq = kqueue()\n\
|
|
|
|
>>> sock = socket()\n\
|
|
|
|
>>> sock.connect((host, port))\n\
|
|
|
|
>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
|
|
|
|
\n\
|
|
|
|
To wait one second for it to become writeable:\n\
|
|
|
|
>>> kq.control(None, 1, 1000)\n\
|
|
|
|
\n\
|
|
|
|
To stop listening:\n\
|
|
|
|
>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
|
|
|
|
|
|
|
|
static PyTypeObject kqueue_queue_Type = {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
"select.kqueue", /* tp_name */
|
|
|
|
sizeof(kqueue_queue_Object), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
(destructor)kqueue_queue_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
|
kqueue_queue_doc, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
kqueue_queue_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
kqueue_queue_getsetlist, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
kqueue_queue_new, /* tp_new */
|
|
|
|
0, /* tp_free */
|
2008-03-21 20:49:44 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* HAVE_KQUEUE */
|
|
|
|
/* ************************************************************************ */
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(select_doc,
|
1998-06-28 13:54:49 -03:00
|
|
|
"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
|
|
|
|
\n\
|
|
|
|
Wait until one or more file descriptors are ready for some kind of I/O.\n\
|
2003-09-10 16:37:42 -03:00
|
|
|
The first three arguments are sequences of file descriptors to be waited for:\n\
|
1998-06-28 13:54:49 -03:00
|
|
|
rlist -- wait until ready for reading\n\
|
|
|
|
wlist -- wait until ready for writing\n\
|
|
|
|
xlist -- wait for an ``exceptional condition''\n\
|
|
|
|
If only one kind of condition is required, pass [] for the other lists.\n\
|
|
|
|
A file descriptor is either a socket or file object, or a small integer\n\
|
|
|
|
gotten from a fileno() method call on one of those.\n\
|
|
|
|
\n\
|
|
|
|
The optional 4th argument specifies a timeout in seconds; it may be\n\
|
|
|
|
a floating point number to specify fractions of seconds. If it is absent\n\
|
|
|
|
or None, the call will never time out.\n\
|
|
|
|
\n\
|
|
|
|
The return value is a tuple of three lists corresponding to the first three\n\
|
|
|
|
arguments; each contains the subset of the corresponding file descriptors\n\
|
|
|
|
that are ready.\n\
|
|
|
|
\n\
|
|
|
|
*** IMPORTANT NOTICE ***\n\
|
2008-03-21 20:49:44 -03:00
|
|
|
On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\
|
2008-03-25 21:16:50 -03:00
|
|
|
descriptors can be used.");
|
1998-06-28 13:54:49 -03:00
|
|
|
|
1996-12-11 20:04:35 -04:00
|
|
|
static PyMethodDef select_methods[] = {
|
2010-05-09 11:46:46 -03:00
|
|
|
{"select", select_select, METH_VARARGS, select_doc},
|
2008-03-21 20:49:44 -03:00
|
|
|
#ifdef HAVE_POLL
|
2010-05-09 11:46:46 -03:00
|
|
|
{"poll", select_poll, METH_NOARGS, poll_doc},
|
2006-04-23 09:36:23 -03:00
|
|
|
#endif /* HAVE_POLL */
|
2010-05-09 11:46:46 -03:00
|
|
|
{0, 0}, /* sentinel */
|
1992-06-23 06:07:03 -03:00
|
|
|
};
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(module_doc,
|
1998-06-28 13:54:49 -03:00
|
|
|
"This module supports asynchronous I/O on multiple file descriptors.\n\
|
|
|
|
\n\
|
|
|
|
*** IMPORTANT NOTICE ***\n\
|
2006-07-09 22:18:57 -03:00
|
|
|
On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
|
1992-06-23 06:07:03 -03:00
|
|
|
|
2002-07-23 03:31:15 -03:00
|
|
|
PyMODINIT_FUNC
|
2000-07-21 03:00:07 -03:00
|
|
|
initselect(void)
|
1992-06-23 06:07:03 -03:00
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *m;
|
|
|
|
m = Py_InitModule3("select", select_methods, module_doc);
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
2002-04-01 10:53:37 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
SelectError = PyErr_NewException("select.error", NULL, NULL);
|
|
|
|
Py_INCREF(SelectError);
|
|
|
|
PyModule_AddObject(m, "error", SelectError);
|
2006-04-23 09:36:23 -03:00
|
|
|
|
2009-07-09 19:37:22 -03:00
|
|
|
#ifdef PIPE_BUF
|
2010-10-15 21:43:13 -03:00
|
|
|
#ifdef HAVE_BROKEN_PIPE_BUF
|
|
|
|
#undef PIPE_BUF
|
|
|
|
#define PIPE_BUF 512
|
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "PIPE_BUF", PIPE_BUF);
|
2009-07-09 19:37:22 -03:00
|
|
|
#endif
|
2009-07-03 17:48:31 -03:00
|
|
|
|
2008-03-21 20:49:44 -03:00
|
|
|
#if defined(HAVE_POLL)
|
2006-04-23 09:36:23 -03:00
|
|
|
#ifdef __APPLE__
|
2010-05-09 11:46:46 -03:00
|
|
|
if (select_have_broken_poll()) {
|
|
|
|
if (PyObject_DelAttrString(m, "poll") == -1) {
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
} else {
|
2006-04-23 09:36:23 -03:00
|
|
|
#else
|
2010-05-09 11:46:46 -03:00
|
|
|
{
|
2006-04-23 09:36:23 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_TYPE(&poll_Type) = &PyType_Type;
|
|
|
|
PyModule_AddIntConstant(m, "POLLIN", POLLIN);
|
|
|
|
PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
|
|
|
|
PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
|
|
|
|
PyModule_AddIntConstant(m, "POLLERR", POLLERR);
|
|
|
|
PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
|
|
|
|
PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
|
2000-08-24 22:15:33 -03:00
|
|
|
|
2000-09-28 18:33:44 -03:00
|
|
|
#ifdef POLLRDNORM
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
|
2000-09-28 18:33:44 -03:00
|
|
|
#endif
|
|
|
|
#ifdef POLLRDBAND
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
|
2000-09-28 18:33:44 -03:00
|
|
|
#endif
|
|
|
|
#ifdef POLLWRNORM
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
|
2000-09-28 18:33:44 -03:00
|
|
|
#endif
|
|
|
|
#ifdef POLLWRBAND
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
|
2000-09-28 18:33:44 -03:00
|
|
|
#endif
|
2000-08-25 10:59:18 -03:00
|
|
|
#ifdef POLLMSG
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
|
2000-08-25 10:59:18 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
}
|
2006-04-23 09:36:23 -03:00
|
|
|
#endif /* HAVE_POLL */
|
2008-03-21 20:49:44 -03:00
|
|
|
|
|
|
|
#ifdef HAVE_EPOLL
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_TYPE(&pyEpoll_Type) = &PyType_Type;
|
|
|
|
if (PyType_Ready(&pyEpoll_Type) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Py_INCREF(&pyEpoll_Type);
|
|
|
|
PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
|
|
|
|
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLIN", EPOLLIN);
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLOUT", EPOLLOUT);
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLPRI", EPOLLPRI);
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLERR", EPOLLERR);
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLHUP", EPOLLHUP);
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLET", EPOLLET);
|
2008-03-21 20:49:44 -03:00
|
|
|
#ifdef EPOLLONESHOT
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Kernel 2.6.2+ */
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLONESHOT", EPOLLONESHOT);
|
2008-03-21 20:49:44 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
/* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLRDNORM", EPOLLRDNORM);
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLRDBAND", EPOLLRDBAND);
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLWRNORM", EPOLLWRNORM);
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLWRBAND", EPOLLWRBAND);
|
|
|
|
PyModule_AddIntConstant(m, "EPOLLMSG", EPOLLMSG);
|
2008-03-21 20:49:44 -03:00
|
|
|
#endif /* HAVE_EPOLL */
|
|
|
|
|
|
|
|
#ifdef HAVE_KQUEUE
|
2010-05-09 11:46:46 -03:00
|
|
|
kqueue_event_Type.tp_new = PyType_GenericNew;
|
|
|
|
Py_TYPE(&kqueue_event_Type) = &PyType_Type;
|
|
|
|
if(PyType_Ready(&kqueue_event_Type) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Py_INCREF(&kqueue_event_Type);
|
|
|
|
PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
|
|
|
|
|
|
|
|
Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
|
|
|
|
if(PyType_Ready(&kqueue_queue_Type) < 0)
|
|
|
|
return;
|
|
|
|
Py_INCREF(&kqueue_queue_Type);
|
|
|
|
PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
|
|
|
|
|
|
|
|
/* event filters */
|
|
|
|
PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
|
2008-03-21 20:49:44 -03:00
|
|
|
#ifdef EVFILT_NETDEV
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
|
2008-03-21 20:49:44 -03:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
|
|
|
|
|
|
|
|
/* event flags */
|
|
|
|
PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
|
|
|
|
|
|
|
|
PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
|
|
|
|
|
|
|
|
PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
|
|
|
|
|
|
|
|
/* READ WRITE filter flag */
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
|
|
|
|
|
|
|
|
/* VNODE filter flags */
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
|
|
|
|
|
|
|
|
/* PROC filter flags */
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
|
|
|
|
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
|
|
|
|
|
|
|
|
/* NETDEV filter flags */
|
2008-03-21 20:49:44 -03:00
|
|
|
#ifdef EVFILT_NETDEV
|
2010-05-09 11:46:46 -03:00
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
|
|
|
|
PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
|
2008-03-21 20:49:44 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* HAVE_KQUEUE */
|
1992-06-23 06:07:03 -03:00
|
|
|
}
|