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