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