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