| Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 1 | /* select - Module containing unix select(2) call. | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 2 |    Under Unix, the file descriptors are small integers. | 
 | 3 |    Under Win32, select only exists for sockets, and sockets may | 
 | 4 |    have any value except INVALID_SOCKET. | 
| Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 5 | */ | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 6 |  | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 7 | #include "Python.h" | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 8 | #include <structmember.h> | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 9 |  | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 10 | #ifdef HAVE_SYS_DEVPOLL_H | 
 | 11 | #include <sys/resource.h> | 
 | 12 | #include <sys/devpoll.h> | 
 | 13 | #include <sys/types.h> | 
 | 14 | #include <sys/stat.h> | 
 | 15 | #include <fcntl.h> | 
 | 16 | #endif | 
 | 17 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 18 | #ifdef __APPLE__ | 
 | 19 |     /* Perform runtime testing for a broken poll on OSX to make it easier | 
 | 20 |      * to use the same binary on multiple releases of the OS. | 
 | 21 |      */ | 
 | 22 | #undef HAVE_BROKEN_POLL | 
 | 23 | #endif | 
 | 24 |  | 
| Tim Peters | d92dfe0 | 2000-12-12 01:18:41 +0000 | [diff] [blame] | 25 | /* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined. | 
 | 26 |    64 is too small (too many people have bumped into that limit). | 
 | 27 |    Here we boost it. | 
 | 28 |    Users who want even more than the boosted limit should #define | 
 | 29 |    FD_SETSIZE higher before this; e.g., via compiler /D switch. | 
 | 30 | */ | 
 | 31 | #if defined(MS_WINDOWS) && !defined(FD_SETSIZE) | 
 | 32 | #define FD_SETSIZE 512 | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | #endif | 
| Tim Peters | d92dfe0 | 2000-12-12 01:18:41 +0000 | [diff] [blame] | 34 |  | 
| Andrew M. Kuchling | 737fbb3 | 2001-07-14 20:54:37 +0000 | [diff] [blame] | 35 | #if defined(HAVE_POLL_H) | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 36 | #include <poll.h> | 
| Andrew M. Kuchling | 737fbb3 | 2001-07-14 20:54:37 +0000 | [diff] [blame] | 37 | #elif defined(HAVE_SYS_POLL_H) | 
 | 38 | #include <sys/poll.h> | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 39 | #endif | 
| Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 40 |  | 
| Guido van Rossum | 3727317 | 1996-12-09 18:47:43 +0000 | [diff] [blame] | 41 | #ifdef __sgi | 
 | 42 | /* This is missing from unistd.h */ | 
| Thomas Wouters | bd4bc4e | 2000-07-22 23:57:55 +0000 | [diff] [blame] | 43 | extern void bzero(void *, int); | 
| Guido van Rossum | 3727317 | 1996-12-09 18:47:43 +0000 | [diff] [blame] | 44 | #endif | 
 | 45 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 46 | #ifdef HAVE_SYS_TYPES_H | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 47 | #include <sys/types.h> | 
| Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 48 | #endif | 
| Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 49 |  | 
| Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 50 | #ifdef MS_WINDOWS | 
| Christian Heimes | c36625b | 2008-01-04 13:33:00 +0000 | [diff] [blame] | 51 | #  define WIN32_LEAN_AND_MEAN | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 52 | #  include <winsock.h> | 
| Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 53 | #else | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 54 | #  define SOCKET int | 
| Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 55 | #endif | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 56 |  | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 57 | /* list of Python objects and their file descriptor */ | 
 | 58 | typedef struct { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 |     PyObject *obj;                           /* owned reference */ | 
 | 60 |     SOCKET fd; | 
 | 61 |     int sentinel;                            /* -1 == sentinel */ | 
| Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 62 | } pylist; | 
 | 63 |  | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 64 | static void | 
| Tim Peters | 4b046c2 | 2001-08-16 21:59:46 +0000 | [diff] [blame] | 65 | reap_obj(pylist fd2obj[FD_SETSIZE + 1]) | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 66 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 |     int i; | 
 | 68 |     for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) { | 
| Serhiy Storchaka | 505ff75 | 2014-02-09 13:33:53 +0200 | [diff] [blame] | 69 |         Py_CLEAR(fd2obj[i].obj); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 |     } | 
 | 71 |     fd2obj[0].sentinel = -1; | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 72 | } | 
 | 73 |  | 
 | 74 |  | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 75 | /* returns -1 and sets the Python exception if an error occurred, otherwise | 
 | 76 |    returns a number >= 0 | 
 | 77 | */ | 
| Guido van Rossum | 4f0fbf8 | 1996-06-12 04:22:53 +0000 | [diff] [blame] | 78 | static int | 
| Brett Cannon | 62dba4c | 2003-09-10 19:37:42 +0000 | [diff] [blame] | 79 | seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1]) | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 80 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 |     int max = -1; | 
 | 82 |     int index = 0; | 
| Antoine Pitrou | e4ad37e | 2012-11-01 20:13:54 +0100 | [diff] [blame] | 83 |     Py_ssize_t i; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 |     PyObject* fast_seq = NULL; | 
 | 85 |     PyObject* o = NULL; | 
| Guido van Rossum | 07432c0 | 1995-03-29 16:47:45 +0000 | [diff] [blame] | 86 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 |     fd2obj[0].obj = (PyObject*)0;            /* set list to zero size */ | 
 | 88 |     FD_ZERO(set); | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 89 |  | 
| Benjamin Peterson | e0edb8b | 2010-06-27 23:49:45 +0000 | [diff] [blame] | 90 |     fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences"); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 |     if (!fast_seq) | 
 | 92 |         return -1; | 
 | 93 |  | 
| Antoine Pitrou | e4ad37e | 2012-11-01 20:13:54 +0100 | [diff] [blame] | 94 |     for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++)  { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 |         SOCKET v; | 
 | 96 |  | 
 | 97 |         /* any intervening fileno() calls could decr this refcnt */ | 
 | 98 |         if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i))) | 
| Jesus Cea | 62a5c32 | 2012-07-19 21:31:26 +0200 | [diff] [blame] | 99 |             goto finally; | 
| Brett Cannon | 62dba4c | 2003-09-10 19:37:42 +0000 | [diff] [blame] | 100 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 |         Py_INCREF(o); | 
 | 102 |         v = PyObject_AsFileDescriptor( o ); | 
 | 103 |         if (v == -1) goto finally; | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 104 |  | 
| Guido van Rossum | 947a0fa | 2000-01-14 16:33:09 +0000 | [diff] [blame] | 105 | #if defined(_MSC_VER) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 |         max = 0;                             /* not used for Win32 */ | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 107 | #else  /* !_MSC_VER */ | 
| Charles-François Natali | aa26b27 | 2011-08-28 17:51:43 +0200 | [diff] [blame] | 108 |         if (!_PyIsSelectable_fd(v)) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 |             PyErr_SetString(PyExc_ValueError, | 
 | 110 |                         "filedescriptor out of range in select()"); | 
 | 111 |             goto finally; | 
 | 112 |         } | 
 | 113 |         if (v > max) | 
 | 114 |             max = v; | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 115 | #endif /* _MSC_VER */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 |         FD_SET(v, set); | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 117 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 |         /* add object and its file descriptor to the list */ | 
 | 119 |         if (index >= FD_SETSIZE) { | 
 | 120 |             PyErr_SetString(PyExc_ValueError, | 
 | 121 |                           "too many file descriptors in select()"); | 
 | 122 |             goto finally; | 
 | 123 |         } | 
 | 124 |         fd2obj[index].obj = o; | 
 | 125 |         fd2obj[index].fd = v; | 
 | 126 |         fd2obj[index].sentinel = 0; | 
 | 127 |         fd2obj[++index].sentinel = -1; | 
 | 128 |     } | 
 | 129 |     Py_DECREF(fast_seq); | 
 | 130 |     return max+1; | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 131 |  | 
 | 132 |   finally: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 |     Py_XDECREF(o); | 
 | 134 |     Py_DECREF(fast_seq); | 
 | 135 |     return -1; | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 136 | } | 
 | 137 |  | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 138 | /* returns NULL and sets the Python exception if an error occurred */ | 
 | 139 | static PyObject * | 
| Tim Peters | 4b046c2 | 2001-08-16 21:59:46 +0000 | [diff] [blame] | 140 | set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1]) | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 141 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 |     int i, j, count=0; | 
 | 143 |     PyObject *list, *o; | 
 | 144 |     SOCKET fd; | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 145 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 |     for (j = 0; fd2obj[j].sentinel >= 0; j++) { | 
 | 147 |         if (FD_ISSET(fd2obj[j].fd, set)) | 
 | 148 |             count++; | 
 | 149 |     } | 
 | 150 |     list = PyList_New(count); | 
 | 151 |     if (!list) | 
 | 152 |         return NULL; | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 153 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 |     i = 0; | 
 | 155 |     for (j = 0; fd2obj[j].sentinel >= 0; j++) { | 
 | 156 |         fd = fd2obj[j].fd; | 
 | 157 |         if (FD_ISSET(fd, set)) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 |             o = fd2obj[j].obj; | 
 | 159 |             fd2obj[j].obj = NULL; | 
 | 160 |             /* transfer ownership */ | 
 | 161 |             if (PyList_SetItem(list, i, o) < 0) | 
 | 162 |                 goto finally; | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 163 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 |             i++; | 
 | 165 |         } | 
 | 166 |     } | 
 | 167 |     return list; | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 168 |   finally: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 |     Py_DECREF(list); | 
 | 170 |     return NULL; | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 171 | } | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 172 |  | 
| Barry Warsaw | b44740f | 2001-08-16 16:52:59 +0000 | [diff] [blame] | 173 | #undef SELECT_USES_HEAP | 
 | 174 | #if FD_SETSIZE > 1024 | 
 | 175 | #define SELECT_USES_HEAP | 
 | 176 | #endif /* FD_SETSIZE > 1024 */ | 
 | 177 |  | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 178 | static PyObject * | 
| Peter Schneider-Kamp | 41c36ff | 2000-07-10 12:29:26 +0000 | [diff] [blame] | 179 | select_select(PyObject *self, PyObject *args) | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 180 | { | 
| Barry Warsaw | b44740f | 2001-08-16 16:52:59 +0000 | [diff] [blame] | 181 | #ifdef SELECT_USES_HEAP | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 |     pylist *rfd2obj, *wfd2obj, *efd2obj; | 
| Barry Warsaw | b44740f | 2001-08-16 16:52:59 +0000 | [diff] [blame] | 183 | #else  /* !SELECT_USES_HEAP */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 |     /* XXX: All this should probably be implemented as follows: | 
 | 185 |      * - find the highest descriptor we're interested in | 
 | 186 |      * - add one | 
 | 187 |      * - that's the size | 
 | 188 |      * See: Stevens, APitUE, $12.5.1 | 
 | 189 |      */ | 
 | 190 |     pylist rfd2obj[FD_SETSIZE + 1]; | 
 | 191 |     pylist wfd2obj[FD_SETSIZE + 1]; | 
 | 192 |     pylist efd2obj[FD_SETSIZE + 1]; | 
| Barry Warsaw | b44740f | 2001-08-16 16:52:59 +0000 | [diff] [blame] | 193 | #endif /* SELECT_USES_HEAP */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 |     PyObject *ifdlist, *ofdlist, *efdlist; | 
 | 195 |     PyObject *ret = NULL; | 
| Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 196 |     PyObject *timeout_obj = Py_None; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 |     fd_set ifdset, ofdset, efdset; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 |     struct timeval tv, *tvp; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 |     int imax, omax, emax, max; | 
 | 200 |     int n; | 
| Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 201 |     _PyTime_t timeout, deadline = 0; | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 202 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 |     /* convert arguments */ | 
 | 204 |     if (!PyArg_UnpackTuple(args, "select", 3, 4, | 
| Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 205 |                           &ifdlist, &ofdlist, &efdlist, &timeout_obj)) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 |         return NULL; | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 207 |  | 
| Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 208 |     if (timeout_obj == Py_None) | 
 | 209 |         tvp = (struct timeval *)NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 |     else { | 
| Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 211 |         if (_PyTime_FromSecondsObject(&timeout, timeout_obj, | 
 | 212 |                                       _PyTime_ROUND_CEILING) < 0) { | 
 | 213 |             if (PyErr_ExceptionMatches(PyExc_TypeError)) { | 
 | 214 |                 PyErr_SetString(PyExc_TypeError, | 
 | 215 |                                 "timeout must be a float or None"); | 
 | 216 |             } | 
| Victor Stinner | b2a3773 | 2012-03-14 00:20:51 +0100 | [diff] [blame] | 217 |             return NULL; | 
 | 218 |         } | 
| Victor Stinner | c337838 | 2015-03-28 05:07:51 +0100 | [diff] [blame] | 219 |  | 
| Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 220 |         if (_PyTime_AsTimeval(timeout, &tv, _PyTime_ROUND_CEILING) == -1) | 
| Victor Stinner | c337838 | 2015-03-28 05:07:51 +0100 | [diff] [blame] | 221 |             return NULL; | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 222 |         if (tv.tv_sec < 0) { | 
 | 223 |             PyErr_SetString(PyExc_ValueError, "timeout must be non-negative"); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 |             return NULL; | 
 | 225 |         } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 |         tvp = &tv; | 
 | 227 |     } | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 228 |  | 
| Barry Warsaw | b44740f | 2001-08-16 16:52:59 +0000 | [diff] [blame] | 229 | #ifdef SELECT_USES_HEAP | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 |     /* Allocate memory for the lists */ | 
 | 231 |     rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1); | 
 | 232 |     wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1); | 
 | 233 |     efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1); | 
 | 234 |     if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) { | 
 | 235 |         if (rfd2obj) PyMem_DEL(rfd2obj); | 
 | 236 |         if (wfd2obj) PyMem_DEL(wfd2obj); | 
 | 237 |         if (efd2obj) PyMem_DEL(efd2obj); | 
 | 238 |         return PyErr_NoMemory(); | 
 | 239 |     } | 
| Barry Warsaw | b44740f | 2001-08-16 16:52:59 +0000 | [diff] [blame] | 240 | #endif /* SELECT_USES_HEAP */ | 
| Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 241 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 |     /* Convert sequences to fd_sets, and get maximum fd number | 
 | 243 |      * propagates the Python exception set in seq2set() | 
 | 244 |      */ | 
 | 245 |     rfd2obj[0].sentinel = -1; | 
 | 246 |     wfd2obj[0].sentinel = -1; | 
 | 247 |     efd2obj[0].sentinel = -1; | 
 | 248 |     if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0) | 
 | 249 |         goto finally; | 
 | 250 |     if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0) | 
 | 251 |         goto finally; | 
 | 252 |     if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0) | 
 | 253 |         goto finally; | 
| Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 254 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 |     max = imax; | 
 | 256 |     if (omax > max) max = omax; | 
 | 257 |     if (emax > max) max = emax; | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 258 |  | 
| Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 259 |     if (tvp) | 
 | 260 |         deadline = _PyTime_GetMonotonicClock() + timeout; | 
 | 261 |  | 
 | 262 |     do { | 
 | 263 |         Py_BEGIN_ALLOW_THREADS | 
 | 264 |         errno = 0; | 
 | 265 |         n = select(max, &ifdset, &ofdset, &efdset, tvp); | 
 | 266 |         Py_END_ALLOW_THREADS | 
 | 267 |  | 
 | 268 |         if (errno != EINTR) | 
 | 269 |             break; | 
 | 270 |  | 
 | 271 |         /* select() was interrupted by a signal */ | 
 | 272 |         if (PyErr_CheckSignals()) | 
 | 273 |             goto finally; | 
 | 274 |  | 
 | 275 |         if (tvp) { | 
 | 276 |             timeout = deadline - _PyTime_GetMonotonicClock(); | 
 | 277 |             if (timeout < 0) { | 
 | 278 |                 n = 0; | 
 | 279 |                 break; | 
 | 280 |             } | 
 | 281 |             _PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING); | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 282 |             /* retry select() with the recomputed timeout */ | 
| Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 283 |         } | 
 | 284 |     } while (1); | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 285 |  | 
| Thomas Heller | 106f4c7 | 2002-09-24 16:51:00 +0000 | [diff] [blame] | 286 | #ifdef MS_WINDOWS | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 |     if (n == SOCKET_ERROR) { | 
| Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 288 |         PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 289 |     } | 
| Thomas Heller | 106f4c7 | 2002-09-24 16:51:00 +0000 | [diff] [blame] | 290 | #else | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 |     if (n < 0) { | 
| Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 292 |         PyErr_SetFromErrno(PyExc_OSError); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 |     } | 
| Thomas Heller | 106f4c7 | 2002-09-24 16:51:00 +0000 | [diff] [blame] | 294 | #endif | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 |     else { | 
 | 296 |         /* any of these three calls can raise an exception.  it's more | 
 | 297 |            convenient to test for this after all three calls... but | 
 | 298 |            is that acceptable? | 
 | 299 |         */ | 
 | 300 |         ifdlist = set2list(&ifdset, rfd2obj); | 
 | 301 |         ofdlist = set2list(&ofdset, wfd2obj); | 
 | 302 |         efdlist = set2list(&efdset, efd2obj); | 
 | 303 |         if (PyErr_Occurred()) | 
 | 304 |             ret = NULL; | 
 | 305 |         else | 
 | 306 |             ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist); | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 307 |  | 
| Victor Stinner | bbf8ce5 | 2013-07-09 00:49:03 +0200 | [diff] [blame] | 308 |         Py_XDECREF(ifdlist); | 
 | 309 |         Py_XDECREF(ofdlist); | 
 | 310 |         Py_XDECREF(efdlist); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 |     } | 
 | 312 |  | 
| Barry Warsaw | c1cb360 | 1996-12-12 22:16:21 +0000 | [diff] [blame] | 313 |   finally: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 |     reap_obj(rfd2obj); | 
 | 315 |     reap_obj(wfd2obj); | 
 | 316 |     reap_obj(efd2obj); | 
| Barry Warsaw | b44740f | 2001-08-16 16:52:59 +0000 | [diff] [blame] | 317 | #ifdef SELECT_USES_HEAP | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 |     PyMem_DEL(rfd2obj); | 
 | 319 |     PyMem_DEL(wfd2obj); | 
 | 320 |     PyMem_DEL(efd2obj); | 
| Barry Warsaw | b44740f | 2001-08-16 16:52:59 +0000 | [diff] [blame] | 321 | #endif /* SELECT_USES_HEAP */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 |     return ret; | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 323 | } | 
 | 324 |  | 
| Nicholas Bastin | e62c5c8 | 2004-03-21 23:45:42 +0000 | [diff] [blame] | 325 | #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | /* | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 327 |  * poll() support | 
 | 328 |  */ | 
 | 329 |  | 
 | 330 | typedef struct { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 |     PyObject_HEAD | 
 | 332 |     PyObject *dict; | 
 | 333 |     int ufd_uptodate; | 
 | 334 |     int ufd_len; | 
 | 335 |     struct pollfd *ufds; | 
| Serhiy Storchaka | b1973c2 | 2013-08-20 20:38:21 +0300 | [diff] [blame] | 336 |     int poll_running; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 337 | } pollObject; | 
 | 338 |  | 
| Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 339 | static PyTypeObject poll_Type; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 340 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | /* Update the malloc'ed array of pollfds to match the dictionary | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 342 |    contained within a pollObject.  Return 1 on success, 0 on an error. | 
 | 343 | */ | 
 | 344 |  | 
 | 345 | static int | 
 | 346 | update_ufd_array(pollObject *self) | 
 | 347 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 |     Py_ssize_t i, pos; | 
 | 349 |     PyObject *key, *value; | 
 | 350 |     struct pollfd *old_ufds = self->ufds; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 351 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 |     self->ufd_len = PyDict_Size(self->dict); | 
 | 353 |     PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len); | 
 | 354 |     if (self->ufds == NULL) { | 
 | 355 |         self->ufds = old_ufds; | 
 | 356 |         PyErr_NoMemory(); | 
 | 357 |         return 0; | 
 | 358 |     } | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 359 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 |     i = pos = 0; | 
 | 361 |     while (PyDict_Next(self->dict, &pos, &key, &value)) { | 
| Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 362 |         assert(i < self->ufd_len); | 
 | 363 |         /* Never overflow */ | 
 | 364 |         self->ufds[i].fd = (int)PyLong_AsLong(key); | 
| Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 365 |         self->ufds[i].events = (short)(unsigned short)PyLong_AsLong(value); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 |         i++; | 
 | 367 |     } | 
| Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 368 |     assert(i == self->ufd_len); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 |     self->ufd_uptodate = 1; | 
 | 370 |     return 1; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 371 | } | 
 | 372 |  | 
| Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 373 | static int | 
 | 374 | ushort_converter(PyObject *obj, void *ptr) | 
 | 375 | { | 
 | 376 |     unsigned long uval; | 
 | 377 |  | 
 | 378 |     uval = PyLong_AsUnsignedLong(obj); | 
 | 379 |     if (uval == (unsigned long)-1 && PyErr_Occurred()) | 
 | 380 |         return 0; | 
 | 381 |     if (uval > USHRT_MAX) { | 
 | 382 |         PyErr_SetString(PyExc_OverflowError, | 
 | 383 |                         "Python int too large for C unsigned short"); | 
 | 384 |         return 0; | 
 | 385 |     } | 
 | 386 |  | 
 | 387 |     *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short); | 
 | 388 |     return 1; | 
 | 389 | } | 
 | 390 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 391 | PyDoc_STRVAR(poll_register_doc, | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 392 | "register(fd [, eventmask] ) -> None\n\n\ | 
 | 393 | Register a file descriptor with the polling object.\n\ | 
| Barry Warsaw | 2f70455 | 2001-08-16 16:55:10 +0000 | [diff] [blame] | 394 | fd -- either an integer, or an object with a fileno() method returning an\n\ | 
 | 395 |       int.\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 396 | 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] | 397 |  | 
 | 398 | static PyObject * | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | poll_register(pollObject *self, PyObject *args) | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 400 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 |     PyObject *o, *key, *value; | 
| Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 402 |     int fd; | 
 | 403 |     unsigned short events = POLLIN | POLLPRI | POLLOUT; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 |     int err; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 405 |  | 
| Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 406 |     if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events)) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 |         return NULL; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 408 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 |     fd = PyObject_AsFileDescriptor(o); | 
 | 410 |     if (fd == -1) return NULL; | 
| Guido van Rossum | a0dfc85 | 2001-10-25 20:18:35 +0000 | [diff] [blame] | 411 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 |     /* Add entry to the internal dictionary: the key is the | 
 | 413 |        file descriptor, and the value is the event mask. */ | 
 | 414 |     key = PyLong_FromLong(fd); | 
 | 415 |     if (key == NULL) | 
 | 416 |         return NULL; | 
 | 417 |     value = PyLong_FromLong(events); | 
 | 418 |     if (value == NULL) { | 
 | 419 |         Py_DECREF(key); | 
 | 420 |         return NULL; | 
 | 421 |     } | 
 | 422 |     err = PyDict_SetItem(self->dict, key, value); | 
 | 423 |     Py_DECREF(key); | 
 | 424 |     Py_DECREF(value); | 
 | 425 |     if (err < 0) | 
 | 426 |         return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 427 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 |     self->ufd_uptodate = 0; | 
 | 429 |  | 
 | 430 |     Py_INCREF(Py_None); | 
 | 431 |     return Py_None; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 432 | } | 
 | 433 |  | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 434 | PyDoc_STRVAR(poll_modify_doc, | 
 | 435 | "modify(fd, eventmask) -> None\n\n\ | 
| Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 436 | Modify an already registered file descriptor.\n\ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 437 | fd -- either an integer, or an object with a fileno() method returning an\n\ | 
 | 438 |       int.\n\ | 
 | 439 | events -- an optional bitmask describing the type of events to check for"); | 
 | 440 |  | 
 | 441 | static PyObject * | 
 | 442 | poll_modify(pollObject *self, PyObject *args) | 
 | 443 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 |     PyObject *o, *key, *value; | 
| Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 445 |     int fd; | 
 | 446 |     unsigned short events; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 |     int err; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 448 |  | 
| Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 449 |     if (!PyArg_ParseTuple(args, "OO&:modify", &o, ushort_converter, &events)) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 |         return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 451 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 |     fd = PyObject_AsFileDescriptor(o); | 
 | 453 |     if (fd == -1) return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 454 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 |     /* Modify registered fd */ | 
 | 456 |     key = PyLong_FromLong(fd); | 
 | 457 |     if (key == NULL) | 
 | 458 |         return NULL; | 
 | 459 |     if (PyDict_GetItem(self->dict, key) == NULL) { | 
 | 460 |         errno = ENOENT; | 
| Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 461 |         PyErr_SetFromErrno(PyExc_OSError); | 
| Jesus Cea | 62a5c32 | 2012-07-19 21:31:26 +0200 | [diff] [blame] | 462 |         Py_DECREF(key); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 |         return NULL; | 
 | 464 |     } | 
 | 465 |     value = PyLong_FromLong(events); | 
 | 466 |     if (value == NULL) { | 
 | 467 |         Py_DECREF(key); | 
 | 468 |         return NULL; | 
 | 469 |     } | 
 | 470 |     err = PyDict_SetItem(self->dict, key, value); | 
 | 471 |     Py_DECREF(key); | 
 | 472 |     Py_DECREF(value); | 
 | 473 |     if (err < 0) | 
 | 474 |         return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 475 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 |     self->ufd_uptodate = 0; | 
 | 477 |  | 
 | 478 |     Py_INCREF(Py_None); | 
 | 479 |     return Py_None; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 480 | } | 
 | 481 |  | 
 | 482 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 483 | PyDoc_STRVAR(poll_unregister_doc, | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 484 | "unregister(fd) -> None\n\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 485 | Remove a file descriptor being tracked by the polling object."); | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 486 |  | 
 | 487 | static PyObject * | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | poll_unregister(pollObject *self, PyObject *o) | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 489 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 |     PyObject *key; | 
 | 491 |     int fd; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 492 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 |     fd = PyObject_AsFileDescriptor( o ); | 
 | 494 |     if (fd == -1) | 
 | 495 |         return NULL; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 496 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 |     /* Check whether the fd is already in the array */ | 
 | 498 |     key = PyLong_FromLong(fd); | 
 | 499 |     if (key == NULL) | 
 | 500 |         return NULL; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 501 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 |     if (PyDict_DelItem(self->dict, key) == -1) { | 
 | 503 |         Py_DECREF(key); | 
 | 504 |         /* This will simply raise the KeyError set by PyDict_DelItem | 
 | 505 |            if the file descriptor isn't registered. */ | 
 | 506 |         return NULL; | 
 | 507 |     } | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 508 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 |     Py_DECREF(key); | 
 | 510 |     self->ufd_uptodate = 0; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 511 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 |     Py_INCREF(Py_None); | 
 | 513 |     return Py_None; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 514 | } | 
 | 515 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 516 | PyDoc_STRVAR(poll_poll_doc, | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 517 | "poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\ | 
 | 518 | 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] | 519 | any descriptors that have events or errors to report."); | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 520 |  | 
 | 521 | static PyObject * | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | poll_poll(pollObject *self, PyObject *args) | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 523 | { | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 524 |     PyObject *result_list = NULL, *timeout_obj = NULL; | 
 | 525 |     int poll_result, i, j; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 |     PyObject *value = NULL, *num = NULL; | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 527 |     _PyTime_t timeout, ms, deadline; | 
 | 528 |     int async_err = 0; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 529 |  | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 530 |     if (!PyArg_ParseTuple(args, "|O:poll", &timeout_obj)) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 |         return NULL; | 
 | 532 |     } | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 533 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 |     /* Check values for timeout */ | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 535 |     if (timeout_obj == NULL || timeout_obj == Py_None) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 |         timeout = -1; | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 537 |         ms = -1; | 
| Victor Stinner | 41eba22 | 2015-03-30 21:59:21 +0200 | [diff] [blame] | 538 |         deadline = 0;   /* initialize to prevent gcc warning */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 |     } | 
 | 540 |     else { | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 541 |         if (_PyTime_FromMillisecondsObject(&timeout, timeout_obj, | 
 | 542 |                                            _PyTime_ROUND_CEILING) < 0) { | 
 | 543 |             if (PyErr_ExceptionMatches(PyExc_TypeError)) { | 
 | 544 |                 PyErr_SetString(PyExc_TypeError, | 
 | 545 |                                 "timeout must be an integer or None"); | 
 | 546 |             } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 |             return NULL; | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 548 |         } | 
 | 549 |  | 
 | 550 |         ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); | 
 | 551 |         if (ms < INT_MIN || ms > INT_MAX) { | 
 | 552 |             PyErr_SetString(PyExc_OverflowError, "timeout is too large"); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 |             return NULL; | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 554 |         } | 
 | 555 |  | 
 | 556 |         deadline = _PyTime_GetMonotonicClock() + timeout; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 |     } | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 558 |  | 
| Serhiy Storchaka | b1973c2 | 2013-08-20 20:38:21 +0300 | [diff] [blame] | 559 |     /* Avoid concurrent poll() invocation, issue 8865 */ | 
 | 560 |     if (self->poll_running) { | 
 | 561 |         PyErr_SetString(PyExc_RuntimeError, | 
 | 562 |                         "concurrent poll() invocation"); | 
 | 563 |         return NULL; | 
 | 564 |     } | 
 | 565 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 |     /* Ensure the ufd array is up to date */ | 
 | 567 |     if (!self->ufd_uptodate) | 
 | 568 |         if (update_ufd_array(self) == 0) | 
 | 569 |             return NULL; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 570 |  | 
| Serhiy Storchaka | b1973c2 | 2013-08-20 20:38:21 +0300 | [diff] [blame] | 571 |     self->poll_running = 1; | 
 | 572 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 |     /* call poll() */ | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 574 |     async_err = 0; | 
 | 575 |     do { | 
 | 576 |         Py_BEGIN_ALLOW_THREADS | 
 | 577 |         errno = 0; | 
 | 578 |         poll_result = poll(self->ufds, self->ufd_len, (int)ms); | 
 | 579 |         Py_END_ALLOW_THREADS | 
 | 580 |  | 
 | 581 |         if (errno != EINTR) | 
 | 582 |             break; | 
 | 583 |  | 
 | 584 |         /* poll() was interrupted by a signal */ | 
 | 585 |         if (PyErr_CheckSignals()) { | 
 | 586 |             async_err = 1; | 
 | 587 |             break; | 
 | 588 |         } | 
 | 589 |  | 
 | 590 |         if (timeout >= 0) { | 
 | 591 |             timeout = deadline - _PyTime_GetMonotonicClock(); | 
 | 592 |             if (timeout < 0) { | 
 | 593 |                 poll_result = 0; | 
 | 594 |                 break; | 
 | 595 |             } | 
 | 596 |             ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); | 
 | 597 |             /* retry poll() with the recomputed timeout */ | 
 | 598 |         } | 
 | 599 |     } while (1); | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 600 |  | 
| Serhiy Storchaka | b1973c2 | 2013-08-20 20:38:21 +0300 | [diff] [blame] | 601 |     self->poll_running = 0; | 
 | 602 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 |     if (poll_result < 0) { | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 604 |         if (!async_err) | 
 | 605 |             PyErr_SetFromErrno(PyExc_OSError); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 |         return NULL; | 
 | 607 |     } | 
 | 608 |  | 
 | 609 |     /* build the result list */ | 
 | 610 |  | 
 | 611 |     result_list = PyList_New(poll_result); | 
 | 612 |     if (!result_list) | 
 | 613 |         return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 |  | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 615 |     for (i = 0, j = 0; j < poll_result; j++) { | 
 | 616 |         /* skip to the next fired descriptor */ | 
 | 617 |         while (!self->ufds[i].revents) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 |             i++; | 
 | 619 |         } | 
| Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 620 |         /* if we hit a NULL return, set value to NULL | 
 | 621 |            and break out of loop; code at end will | 
 | 622 |            clean up result_list */ | 
 | 623 |         value = PyTuple_New(2); | 
 | 624 |         if (value == NULL) | 
 | 625 |             goto error; | 
 | 626 |         num = PyLong_FromLong(self->ufds[i].fd); | 
 | 627 |         if (num == NULL) { | 
 | 628 |             Py_DECREF(value); | 
 | 629 |             goto error; | 
 | 630 |         } | 
 | 631 |         PyTuple_SET_ITEM(value, 0, num); | 
 | 632 |  | 
 | 633 |         /* The &0xffff is a workaround for AIX.  'revents' | 
 | 634 |            is a 16-bit short, and IBM assigned POLLNVAL | 
 | 635 |            to be 0x8000, so the conversion to int results | 
 | 636 |            in a negative number. See SF bug #923315. */ | 
 | 637 |         num = PyLong_FromLong(self->ufds[i].revents & 0xffff); | 
 | 638 |         if (num == NULL) { | 
 | 639 |             Py_DECREF(value); | 
 | 640 |             goto error; | 
 | 641 |         } | 
 | 642 |         PyTuple_SET_ITEM(value, 1, num); | 
 | 643 |         if ((PyList_SetItem(result_list, j, value)) == -1) { | 
 | 644 |             Py_DECREF(value); | 
 | 645 |             goto error; | 
 | 646 |         } | 
 | 647 |         i++; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 |     } | 
 | 649 |     return result_list; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 650 |  | 
 | 651 |   error: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 |     Py_DECREF(result_list); | 
 | 653 |     return NULL; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 654 | } | 
 | 655 |  | 
 | 656 | static PyMethodDef poll_methods[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 |     {"register",        (PyCFunction)poll_register, | 
 | 658 |      METH_VARARGS,  poll_register_doc}, | 
 | 659 |     {"modify",          (PyCFunction)poll_modify, | 
 | 660 |      METH_VARARGS,  poll_modify_doc}, | 
 | 661 |     {"unregister",      (PyCFunction)poll_unregister, | 
 | 662 |      METH_O,        poll_unregister_doc}, | 
 | 663 |     {"poll",            (PyCFunction)poll_poll, | 
 | 664 |      METH_VARARGS,  poll_poll_doc}, | 
 | 665 |     {NULL,              NULL}           /* sentinel */ | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 666 | }; | 
 | 667 |  | 
 | 668 | static pollObject * | 
| Fred Drake | 8ce159a | 2000-08-31 05:18:54 +0000 | [diff] [blame] | 669 | newPollObject(void) | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 670 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 |     pollObject *self; | 
 | 672 |     self = PyObject_New(pollObject, &poll_Type); | 
 | 673 |     if (self == NULL) | 
 | 674 |         return NULL; | 
 | 675 |     /* ufd_uptodate is a Boolean, denoting whether the | 
 | 676 |        array pointed to by ufds matches the contents of the dictionary. */ | 
 | 677 |     self->ufd_uptodate = 0; | 
 | 678 |     self->ufds = NULL; | 
| Serhiy Storchaka | b1973c2 | 2013-08-20 20:38:21 +0300 | [diff] [blame] | 679 |     self->poll_running = 0; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 |     self->dict = PyDict_New(); | 
 | 681 |     if (self->dict == NULL) { | 
 | 682 |         Py_DECREF(self); | 
 | 683 |         return NULL; | 
 | 684 |     } | 
 | 685 |     return self; | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 686 | } | 
 | 687 |  | 
 | 688 | static void | 
 | 689 | poll_dealloc(pollObject *self) | 
 | 690 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 |     if (self->ufds != NULL) | 
 | 692 |         PyMem_DEL(self->ufds); | 
 | 693 |     Py_XDECREF(self->dict); | 
 | 694 |     PyObject_Del(self); | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 695 | } | 
 | 696 |  | 
| Tim Peters | 0c32279 | 2002-07-17 16:49:03 +0000 | [diff] [blame] | 697 | static PyTypeObject poll_Type = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 |     /* The ob_type field must be initialized in the module init function | 
 | 699 |      * to be portable to Windows without using C++. */ | 
 | 700 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 701 |     "select.poll",              /*tp_name*/ | 
 | 702 |     sizeof(pollObject),         /*tp_basicsize*/ | 
 | 703 |     0,                          /*tp_itemsize*/ | 
 | 704 |     /* methods */ | 
 | 705 |     (destructor)poll_dealloc, /*tp_dealloc*/ | 
 | 706 |     0,                          /*tp_print*/ | 
 | 707 |     0,                          /*tp_getattr*/ | 
 | 708 |     0,                      /*tp_setattr*/ | 
 | 709 |     0,                          /*tp_reserved*/ | 
 | 710 |     0,                          /*tp_repr*/ | 
 | 711 |     0,                          /*tp_as_number*/ | 
 | 712 |     0,                          /*tp_as_sequence*/ | 
 | 713 |     0,                          /*tp_as_mapping*/ | 
 | 714 |     0,                          /*tp_hash*/ | 
 | 715 |     0,                          /*tp_call*/ | 
 | 716 |     0,                          /*tp_str*/ | 
 | 717 |     0,                          /*tp_getattro*/ | 
 | 718 |     0,                          /*tp_setattro*/ | 
 | 719 |     0,                          /*tp_as_buffer*/ | 
 | 720 |     Py_TPFLAGS_DEFAULT,         /*tp_flags*/ | 
 | 721 |     0,                          /*tp_doc*/ | 
 | 722 |     0,                          /*tp_traverse*/ | 
 | 723 |     0,                          /*tp_clear*/ | 
 | 724 |     0,                          /*tp_richcompare*/ | 
 | 725 |     0,                          /*tp_weaklistoffset*/ | 
 | 726 |     0,                          /*tp_iter*/ | 
 | 727 |     0,                          /*tp_iternext*/ | 
 | 728 |     poll_methods,               /*tp_methods*/ | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 729 | }; | 
 | 730 |  | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 731 | #ifdef HAVE_SYS_DEVPOLL_H | 
 | 732 | typedef struct { | 
 | 733 |     PyObject_HEAD | 
 | 734 |     int fd_devpoll; | 
 | 735 |     int max_n_fds; | 
 | 736 |     int n_fds; | 
 | 737 |     struct pollfd *fds; | 
 | 738 | } devpollObject; | 
 | 739 |  | 
 | 740 | static PyTypeObject devpoll_Type; | 
 | 741 |  | 
| Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 742 | static PyObject * | 
 | 743 | devpoll_err_closed(void) | 
 | 744 | { | 
 | 745 |     PyErr_SetString(PyExc_ValueError, "I/O operation on closed devpoll object"); | 
 | 746 |     return NULL; | 
 | 747 | } | 
 | 748 |  | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 749 | static int devpoll_flush(devpollObject *self) | 
 | 750 | { | 
 | 751 |     int size, n; | 
 | 752 |  | 
 | 753 |     if (!self->n_fds) return 0; | 
 | 754 |  | 
 | 755 |     size = sizeof(struct pollfd)*self->n_fds; | 
 | 756 |     self->n_fds = 0; | 
 | 757 |  | 
| Victor Stinner | 5479967 | 2015-03-19 23:33:09 +0100 | [diff] [blame] | 758 |     n = _Py_write(self->fd_devpoll, self->fds, size); | 
 | 759 |     if (n == -1) | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 760 |         return -1; | 
| Victor Stinner | 5479967 | 2015-03-19 23:33:09 +0100 | [diff] [blame] | 761 |  | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 762 |     if (n < size) { | 
 | 763 |         /* | 
 | 764 |         ** Data writed to /dev/poll is a binary data structure. It is not | 
 | 765 |         ** clear what to do if a partial write occurred. For now, raise | 
 | 766 |         ** an exception and see if we actually found this problem in | 
 | 767 |         ** the wild. | 
 | 768 |         ** See http://bugs.python.org/issue6397. | 
 | 769 |         */ | 
 | 770 |         PyErr_Format(PyExc_IOError, "failed to write all pollfds. " | 
 | 771 |                 "Please, report at http://bugs.python.org/. " | 
 | 772 |                 "Data to report: Size tried: %d, actual size written: %d.", | 
 | 773 |                 size, n); | 
 | 774 |         return -1; | 
 | 775 |     } | 
 | 776 |     return 0; | 
 | 777 | } | 
 | 778 |  | 
 | 779 | static PyObject * | 
 | 780 | internal_devpoll_register(devpollObject *self, PyObject *args, int remove) | 
 | 781 | { | 
 | 782 |     PyObject *o; | 
| Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 783 |     int fd; | 
 | 784 |     unsigned short events = POLLIN | POLLPRI | POLLOUT; | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 785 |  | 
| Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 786 |     if (self->fd_devpoll < 0) | 
 | 787 |         return devpoll_err_closed(); | 
 | 788 |  | 
| Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 789 |     if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events)) | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 790 |         return NULL; | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 791 |  | 
 | 792 |     fd = PyObject_AsFileDescriptor(o); | 
 | 793 |     if (fd == -1) return NULL; | 
 | 794 |  | 
 | 795 |     if (remove) { | 
 | 796 |         self->fds[self->n_fds].fd = fd; | 
 | 797 |         self->fds[self->n_fds].events = POLLREMOVE; | 
 | 798 |  | 
 | 799 |         if (++self->n_fds == self->max_n_fds) { | 
 | 800 |             if (devpoll_flush(self)) | 
 | 801 |                 return NULL; | 
 | 802 |         } | 
 | 803 |     } | 
 | 804 |  | 
 | 805 |     self->fds[self->n_fds].fd = fd; | 
| Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 806 |     self->fds[self->n_fds].events = (signed short)events; | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 807 |  | 
 | 808 |     if (++self->n_fds == self->max_n_fds) { | 
 | 809 |         if (devpoll_flush(self)) | 
 | 810 |             return NULL; | 
 | 811 |     } | 
 | 812 |  | 
 | 813 |     Py_RETURN_NONE; | 
 | 814 | } | 
 | 815 |  | 
 | 816 | PyDoc_STRVAR(devpoll_register_doc, | 
 | 817 | "register(fd [, eventmask] ) -> None\n\n\ | 
 | 818 | Register a file descriptor with the polling object.\n\ | 
 | 819 | fd -- either an integer, or an object with a fileno() method returning an\n\ | 
 | 820 |       int.\n\ | 
 | 821 | events -- an optional bitmask describing the type of events to check for"); | 
 | 822 |  | 
 | 823 | static PyObject * | 
 | 824 | devpoll_register(devpollObject *self, PyObject *args) | 
 | 825 | { | 
 | 826 |     return internal_devpoll_register(self, args, 0); | 
 | 827 | } | 
 | 828 |  | 
 | 829 | PyDoc_STRVAR(devpoll_modify_doc, | 
 | 830 | "modify(fd[, eventmask]) -> None\n\n\ | 
 | 831 | Modify a possible already registered file descriptor.\n\ | 
 | 832 | fd -- either an integer, or an object with a fileno() method returning an\n\ | 
 | 833 |       int.\n\ | 
 | 834 | events -- an optional bitmask describing the type of events to check for"); | 
 | 835 |  | 
 | 836 | static PyObject * | 
 | 837 | devpoll_modify(devpollObject *self, PyObject *args) | 
 | 838 | { | 
 | 839 |     return internal_devpoll_register(self, args, 1); | 
 | 840 | } | 
 | 841 |  | 
 | 842 |  | 
 | 843 | PyDoc_STRVAR(devpoll_unregister_doc, | 
 | 844 | "unregister(fd) -> None\n\n\ | 
 | 845 | Remove a file descriptor being tracked by the polling object."); | 
 | 846 |  | 
 | 847 | static PyObject * | 
 | 848 | devpoll_unregister(devpollObject *self, PyObject *o) | 
 | 849 | { | 
 | 850 |     int fd; | 
 | 851 |  | 
| Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 852 |     if (self->fd_devpoll < 0) | 
 | 853 |         return devpoll_err_closed(); | 
 | 854 |  | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 855 |     fd = PyObject_AsFileDescriptor( o ); | 
 | 856 |     if (fd == -1) | 
 | 857 |         return NULL; | 
 | 858 |  | 
 | 859 |     self->fds[self->n_fds].fd = fd; | 
 | 860 |     self->fds[self->n_fds].events = POLLREMOVE; | 
 | 861 |  | 
 | 862 |     if (++self->n_fds == self->max_n_fds) { | 
 | 863 |         if (devpoll_flush(self)) | 
 | 864 |             return NULL; | 
 | 865 |     } | 
 | 866 |  | 
 | 867 |     Py_RETURN_NONE; | 
 | 868 | } | 
 | 869 |  | 
 | 870 | PyDoc_STRVAR(devpoll_poll_doc, | 
 | 871 | "poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\ | 
 | 872 | Polls the set of registered file descriptors, returning a list containing \n\ | 
 | 873 | any descriptors that have events or errors to report."); | 
 | 874 |  | 
 | 875 | static PyObject * | 
 | 876 | devpoll_poll(devpollObject *self, PyObject *args) | 
 | 877 | { | 
 | 878 |     struct dvpoll dvp; | 
| Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 879 |     PyObject *result_list = NULL, *timeout_obj = NULL; | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 880 |     int poll_result, i; | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 881 |     PyObject *value, *num1, *num2; | 
| Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 882 |     _PyTime_t timeout, ms, deadline = 0; | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 883 |  | 
| Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 884 |     if (self->fd_devpoll < 0) | 
 | 885 |         return devpoll_err_closed(); | 
 | 886 |  | 
| Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 887 |     if (!PyArg_ParseTuple(args, "|O:poll", &timeout_obj)) { | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 888 |         return NULL; | 
 | 889 |     } | 
 | 890 |  | 
 | 891 |     /* Check values for timeout */ | 
| Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 892 |     if (timeout_obj == NULL || timeout_obj == Py_None) { | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 893 |         timeout = -1; | 
| Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 894 |         ms = -1; | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 895 |     } | 
 | 896 |     else { | 
| Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 897 |         if (_PyTime_FromMillisecondsObject(&timeout, timeout_obj, | 
 | 898 |                                            _PyTime_ROUND_CEILING) < 0) { | 
 | 899 |             if (PyErr_ExceptionMatches(PyExc_TypeError)) { | 
 | 900 |                 PyErr_SetString(PyExc_TypeError, | 
 | 901 |                                 "timeout must be an integer or None"); | 
 | 902 |             } | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 903 |             return NULL; | 
| Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 904 |         } | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 905 |  | 
| Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 906 |         ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); | 
 | 907 |         if (ms < -1 || ms > INT_MAX) { | 
 | 908 |             PyErr_SetString(PyExc_OverflowError, "timeout is too large"); | 
 | 909 |             return NULL; | 
 | 910 |         } | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 911 |     } | 
 | 912 |  | 
 | 913 |     if (devpoll_flush(self)) | 
 | 914 |         return NULL; | 
 | 915 |  | 
 | 916 |     dvp.dp_fds = self->fds; | 
 | 917 |     dvp.dp_nfds = self->max_n_fds; | 
| Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 918 |     dvp.dp_timeout = (int)ms; | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 919 |  | 
| Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 920 |     if (timeout >= 0) | 
 | 921 |         deadline = _PyTime_GetMonotonicClock() + timeout; | 
 | 922 |  | 
 | 923 |     do { | 
 | 924 |         /* call devpoll() */ | 
 | 925 |         Py_BEGIN_ALLOW_THREADS | 
 | 926 |         errno = 0; | 
 | 927 |         poll_result = ioctl(self->fd_devpoll, DP_POLL, &dvp); | 
 | 928 |         Py_END_ALLOW_THREADS | 
 | 929 |  | 
 | 930 |         if (errno != EINTR) | 
 | 931 |             break; | 
 | 932 |  | 
 | 933 |         /* devpoll() was interrupted by a signal */ | 
 | 934 |         if (PyErr_CheckSignals()) | 
 | 935 |             return NULL; | 
 | 936 |  | 
 | 937 |         if (timeout >= 0) { | 
 | 938 |             timeout = deadline - _PyTime_GetMonotonicClock(); | 
 | 939 |             if (timeout < 0) { | 
 | 940 |                 poll_result = 0; | 
 | 941 |                 break; | 
 | 942 |             } | 
 | 943 |             ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); | 
 | 944 |             dvp.dp_timeout = (int)ms; | 
 | 945 |             /* retry devpoll() with the recomputed timeout */ | 
 | 946 |         } | 
 | 947 |     } while (1); | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 948 |  | 
 | 949 |     if (poll_result < 0) { | 
 | 950 |         PyErr_SetFromErrno(PyExc_IOError); | 
 | 951 |         return NULL; | 
 | 952 |     } | 
 | 953 |  | 
 | 954 |     /* build the result list */ | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 955 |     result_list = PyList_New(poll_result); | 
 | 956 |     if (!result_list) | 
 | 957 |         return NULL; | 
| Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 958 |  | 
 | 959 |     for (i = 0; i < poll_result; i++) { | 
 | 960 |         num1 = PyLong_FromLong(self->fds[i].fd); | 
 | 961 |         num2 = PyLong_FromLong(self->fds[i].revents); | 
 | 962 |         if ((num1 == NULL) || (num2 == NULL)) { | 
 | 963 |             Py_XDECREF(num1); | 
 | 964 |             Py_XDECREF(num2); | 
 | 965 |             goto error; | 
 | 966 |         } | 
 | 967 |         value = PyTuple_Pack(2, num1, num2); | 
 | 968 |         Py_DECREF(num1); | 
 | 969 |         Py_DECREF(num2); | 
 | 970 |         if (value == NULL) | 
 | 971 |             goto error; | 
 | 972 |         if ((PyList_SetItem(result_list, i, value)) == -1) { | 
 | 973 |             Py_DECREF(value); | 
 | 974 |             goto error; | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 975 |         } | 
 | 976 |     } | 
 | 977 |  | 
 | 978 |     return result_list; | 
 | 979 |  | 
 | 980 |   error: | 
 | 981 |     Py_DECREF(result_list); | 
 | 982 |     return NULL; | 
 | 983 | } | 
 | 984 |  | 
| Richard Oudkerk | 168d59b | 2013-08-22 13:31:15 +0100 | [diff] [blame] | 985 | static int | 
 | 986 | devpoll_internal_close(devpollObject *self) | 
 | 987 | { | 
 | 988 |     int save_errno = 0; | 
 | 989 |     if (self->fd_devpoll >= 0) { | 
 | 990 |         int fd = self->fd_devpoll; | 
 | 991 |         self->fd_devpoll = -1; | 
 | 992 |         Py_BEGIN_ALLOW_THREADS | 
 | 993 |         if (close(fd) < 0) | 
 | 994 |             save_errno = errno; | 
 | 995 |         Py_END_ALLOW_THREADS | 
 | 996 |     } | 
 | 997 |     return save_errno; | 
 | 998 | } | 
 | 999 |  | 
| Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 1000 | static PyObject* | 
 | 1001 | devpoll_close(devpollObject *self) | 
 | 1002 | { | 
 | 1003 |     errno = devpoll_internal_close(self); | 
 | 1004 |     if (errno < 0) { | 
 | 1005 |         PyErr_SetFromErrno(PyExc_OSError); | 
 | 1006 |         return NULL; | 
 | 1007 |     } | 
 | 1008 |     Py_RETURN_NONE; | 
 | 1009 | } | 
 | 1010 |  | 
 | 1011 | PyDoc_STRVAR(devpoll_close_doc, | 
 | 1012 | "close() -> None\n\ | 
 | 1013 | \n\ | 
 | 1014 | Close the devpoll file descriptor. Further operations on the devpoll\n\ | 
 | 1015 | object will raise an exception."); | 
 | 1016 |  | 
 | 1017 | static PyObject* | 
 | 1018 | devpoll_get_closed(devpollObject *self) | 
 | 1019 | { | 
 | 1020 |     if (self->fd_devpoll < 0) | 
 | 1021 |         Py_RETURN_TRUE; | 
 | 1022 |     else | 
 | 1023 |         Py_RETURN_FALSE; | 
 | 1024 | } | 
 | 1025 |  | 
 | 1026 | static PyObject* | 
 | 1027 | devpoll_fileno(devpollObject *self) | 
 | 1028 | { | 
 | 1029 |     if (self->fd_devpoll < 0) | 
 | 1030 |         return devpoll_err_closed(); | 
 | 1031 |     return PyLong_FromLong(self->fd_devpoll); | 
 | 1032 | } | 
 | 1033 |  | 
 | 1034 | PyDoc_STRVAR(devpoll_fileno_doc, | 
 | 1035 | "fileno() -> int\n\ | 
 | 1036 | \n\ | 
 | 1037 | Return the file descriptor."); | 
 | 1038 |  | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1039 | static PyMethodDef devpoll_methods[] = { | 
 | 1040 |     {"register",        (PyCFunction)devpoll_register, | 
 | 1041 |      METH_VARARGS,  devpoll_register_doc}, | 
 | 1042 |     {"modify",          (PyCFunction)devpoll_modify, | 
 | 1043 |      METH_VARARGS,  devpoll_modify_doc}, | 
 | 1044 |     {"unregister",      (PyCFunction)devpoll_unregister, | 
 | 1045 |      METH_O,        devpoll_unregister_doc}, | 
 | 1046 |     {"poll",            (PyCFunction)devpoll_poll, | 
 | 1047 |      METH_VARARGS,  devpoll_poll_doc}, | 
| Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 1048 |     {"close",           (PyCFunction)devpoll_close,    METH_NOARGS, | 
 | 1049 |      devpoll_close_doc}, | 
 | 1050 |     {"fileno",          (PyCFunction)devpoll_fileno,    METH_NOARGS, | 
 | 1051 |      devpoll_fileno_doc}, | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1052 |     {NULL,              NULL}           /* sentinel */ | 
 | 1053 | }; | 
 | 1054 |  | 
| Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 1055 | static PyGetSetDef devpoll_getsetlist[] = { | 
 | 1056 |     {"closed", (getter)devpoll_get_closed, NULL, | 
 | 1057 |      "True if the devpoll object is closed"}, | 
 | 1058 |     {0}, | 
 | 1059 | }; | 
 | 1060 |  | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1061 | static devpollObject * | 
 | 1062 | newDevPollObject(void) | 
 | 1063 | { | 
 | 1064 |     devpollObject *self; | 
 | 1065 |     int fd_devpoll, limit_result; | 
 | 1066 |     struct pollfd *fds; | 
 | 1067 |     struct rlimit limit; | 
 | 1068 |  | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1069 |     /* | 
 | 1070 |     ** If we try to process more that getrlimit() | 
 | 1071 |     ** fds, the kernel will give an error, so | 
 | 1072 |     ** we set the limit here. It is a dynamic | 
 | 1073 |     ** value, because we can change rlimit() anytime. | 
 | 1074 |     */ | 
 | 1075 |     limit_result = getrlimit(RLIMIT_NOFILE, &limit); | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1076 |     if (limit_result == -1) { | 
 | 1077 |         PyErr_SetFromErrno(PyExc_OSError); | 
 | 1078 |         return NULL; | 
 | 1079 |     } | 
| Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 1080 |  | 
 | 1081 |     fd_devpoll = _Py_open("/dev/poll", O_RDWR); | 
 | 1082 |     if (fd_devpoll == -1) | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1083 |         return NULL; | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1084 |  | 
 | 1085 |     fds = PyMem_NEW(struct pollfd, limit.rlim_cur); | 
 | 1086 |     if (fds == NULL) { | 
 | 1087 |         close(fd_devpoll); | 
 | 1088 |         PyErr_NoMemory(); | 
 | 1089 |         return NULL; | 
 | 1090 |     } | 
 | 1091 |  | 
 | 1092 |     self = PyObject_New(devpollObject, &devpoll_Type); | 
 | 1093 |     if (self == NULL) { | 
 | 1094 |         close(fd_devpoll); | 
 | 1095 |         PyMem_DEL(fds); | 
 | 1096 |         return NULL; | 
 | 1097 |     } | 
 | 1098 |     self->fd_devpoll = fd_devpoll; | 
 | 1099 |     self->max_n_fds = limit.rlim_cur; | 
 | 1100 |     self->n_fds = 0; | 
 | 1101 |     self->fds = fds; | 
 | 1102 |  | 
 | 1103 |     return self; | 
 | 1104 | } | 
 | 1105 |  | 
 | 1106 | static void | 
 | 1107 | devpoll_dealloc(devpollObject *self) | 
 | 1108 | { | 
| Richard Oudkerk | a93bf7b | 2013-08-22 14:03:44 +0100 | [diff] [blame] | 1109 |     (void)devpoll_internal_close(self); | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1110 |     PyMem_DEL(self->fds); | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1111 |     PyObject_Del(self); | 
 | 1112 | } | 
 | 1113 |  | 
 | 1114 | static PyTypeObject devpoll_Type = { | 
 | 1115 |     /* The ob_type field must be initialized in the module init function | 
 | 1116 |      * to be portable to Windows without using C++. */ | 
 | 1117 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 1118 |     "select.devpoll",           /*tp_name*/ | 
 | 1119 |     sizeof(devpollObject),      /*tp_basicsize*/ | 
 | 1120 |     0,                          /*tp_itemsize*/ | 
 | 1121 |     /* methods */ | 
 | 1122 |     (destructor)devpoll_dealloc, /*tp_dealloc*/ | 
 | 1123 |     0,                          /*tp_print*/ | 
 | 1124 |     0,                          /*tp_getattr*/ | 
 | 1125 |     0,                          /*tp_setattr*/ | 
 | 1126 |     0,                          /*tp_reserved*/ | 
 | 1127 |     0,                          /*tp_repr*/ | 
 | 1128 |     0,                          /*tp_as_number*/ | 
 | 1129 |     0,                          /*tp_as_sequence*/ | 
 | 1130 |     0,                          /*tp_as_mapping*/ | 
 | 1131 |     0,                          /*tp_hash*/ | 
 | 1132 |     0,                          /*tp_call*/ | 
 | 1133 |     0,                          /*tp_str*/ | 
 | 1134 |     0,                          /*tp_getattro*/ | 
 | 1135 |     0,                          /*tp_setattro*/ | 
 | 1136 |     0,                          /*tp_as_buffer*/ | 
 | 1137 |     Py_TPFLAGS_DEFAULT,         /*tp_flags*/ | 
 | 1138 |     0,                          /*tp_doc*/ | 
 | 1139 |     0,                          /*tp_traverse*/ | 
 | 1140 |     0,                          /*tp_clear*/ | 
 | 1141 |     0,                          /*tp_richcompare*/ | 
 | 1142 |     0,                          /*tp_weaklistoffset*/ | 
 | 1143 |     0,                          /*tp_iter*/ | 
 | 1144 |     0,                          /*tp_iternext*/ | 
 | 1145 |     devpoll_methods,            /*tp_methods*/ | 
| Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 1146 |     0,                          /* tp_members */ | 
 | 1147 |     devpoll_getsetlist,         /* tp_getset */ | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1148 | }; | 
 | 1149 | #endif  /* HAVE_SYS_DEVPOLL_H */ | 
 | 1150 |  | 
 | 1151 |  | 
 | 1152 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1153 | PyDoc_STRVAR(poll_doc, | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 1154 | "Returns a polling object, which supports registering and\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1155 | unregistering file descriptors, and then polling them for I/O events."); | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 1156 |  | 
 | 1157 | static PyObject * | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1158 | select_poll(PyObject *self, PyObject *unused) | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 1159 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 |     return (PyObject *)newPollObject(); | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 1161 | } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1162 |  | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1163 | #ifdef HAVE_SYS_DEVPOLL_H | 
 | 1164 | PyDoc_STRVAR(devpoll_doc, | 
 | 1165 | "Returns a polling object, which supports registering and\n\ | 
 | 1166 | unregistering file descriptors, and then polling them for I/O events."); | 
 | 1167 |  | 
 | 1168 | static PyObject * | 
 | 1169 | select_devpoll(PyObject *self, PyObject *unused) | 
 | 1170 | { | 
 | 1171 |     return (PyObject *)newDevPollObject(); | 
 | 1172 | } | 
 | 1173 | #endif | 
 | 1174 |  | 
 | 1175 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1176 | #ifdef __APPLE__ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | /* | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1178 |  * On some systems poll() sets errno on invalid file descriptors. We test | 
 | 1179 |  * for this at runtime because this bug may be fixed or introduced between | 
 | 1180 |  * OS releases. | 
 | 1181 |  */ | 
 | 1182 | static int select_have_broken_poll(void) | 
 | 1183 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 |     int poll_test; | 
 | 1185 |     int filedes[2]; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1186 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 |     struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 }; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1188 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 |     /* Create a file descriptor to make invalid */ | 
 | 1190 |     if (pipe(filedes) < 0) { | 
 | 1191 |         return 1; | 
 | 1192 |     } | 
 | 1193 |     poll_struct.fd = filedes[0]; | 
 | 1194 |     close(filedes[0]); | 
 | 1195 |     close(filedes[1]); | 
 | 1196 |     poll_test = poll(&poll_struct, 1, 0); | 
 | 1197 |     if (poll_test < 0) { | 
 | 1198 |         return 1; | 
 | 1199 |     } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) { | 
 | 1200 |         return 1; | 
 | 1201 |     } | 
 | 1202 |     return 0; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1203 | } | 
 | 1204 | #endif /* __APPLE__ */ | 
 | 1205 |  | 
 | 1206 | #endif /* HAVE_POLL */ | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 1207 |  | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1208 | #ifdef HAVE_EPOLL | 
 | 1209 | /* ************************************************************************** | 
 | 1210 |  *                      epoll interface for Linux 2.6 | 
 | 1211 |  * | 
 | 1212 |  * Written by Christian Heimes | 
 | 1213 |  * Inspired by Twisted's _epoll.pyx and select.poll() | 
 | 1214 |  */ | 
 | 1215 |  | 
 | 1216 | #ifdef HAVE_SYS_EPOLL_H | 
 | 1217 | #include <sys/epoll.h> | 
 | 1218 | #endif | 
 | 1219 |  | 
 | 1220 | typedef struct { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 |     PyObject_HEAD | 
| Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1222 |     SOCKET epfd;                        /* epoll control file descriptor */ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1223 | } pyEpoll_Object; | 
 | 1224 |  | 
 | 1225 | static PyTypeObject pyEpoll_Type; | 
 | 1226 | #define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type)) | 
 | 1227 |  | 
 | 1228 | static PyObject * | 
 | 1229 | pyepoll_err_closed(void) | 
 | 1230 | { | 
| Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 1231 |     PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll object"); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 |     return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1233 | } | 
 | 1234 |  | 
 | 1235 | static int | 
 | 1236 | pyepoll_internal_close(pyEpoll_Object *self) | 
 | 1237 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 |     int save_errno = 0; | 
 | 1239 |     if (self->epfd >= 0) { | 
 | 1240 |         int epfd = self->epfd; | 
 | 1241 |         self->epfd = -1; | 
 | 1242 |         Py_BEGIN_ALLOW_THREADS | 
 | 1243 |         if (close(epfd) < 0) | 
 | 1244 |             save_errno = errno; | 
 | 1245 |         Py_END_ALLOW_THREADS | 
 | 1246 |     } | 
 | 1247 |     return save_errno; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1248 | } | 
 | 1249 |  | 
 | 1250 | static PyObject * | 
| Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 1251 | newPyEpoll_Object(PyTypeObject *type, int sizehint, int flags, SOCKET fd) | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1252 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 |     pyEpoll_Object *self; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1254 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 |     assert(type != NULL && type->tp_alloc != NULL); | 
 | 1256 |     self = (pyEpoll_Object *) type->tp_alloc(type, 0); | 
 | 1257 |     if (self == NULL) | 
 | 1258 |         return NULL; | 
 | 1259 |  | 
 | 1260 |     if (fd == -1) { | 
 | 1261 |         Py_BEGIN_ALLOW_THREADS | 
| Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 1262 | #ifdef HAVE_EPOLL_CREATE1 | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1263 |         flags |= EPOLL_CLOEXEC; | 
| Benjamin Peterson | 83251c1 | 2011-12-27 16:01:21 -0600 | [diff] [blame] | 1264 |         if (flags) | 
 | 1265 |             self->epfd = epoll_create1(flags); | 
 | 1266 |         else | 
| Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 1267 | #endif | 
| Benjamin Peterson | 83251c1 | 2011-12-27 16:01:21 -0600 | [diff] [blame] | 1268 |         self->epfd = epoll_create(sizehint); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 |         Py_END_ALLOW_THREADS | 
 | 1270 |     } | 
 | 1271 |     else { | 
 | 1272 |         self->epfd = fd; | 
 | 1273 |     } | 
 | 1274 |     if (self->epfd < 0) { | 
 | 1275 |         Py_DECREF(self); | 
| Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1276 |         PyErr_SetFromErrno(PyExc_OSError); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 |         return NULL; | 
 | 1278 |     } | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1279 |  | 
 | 1280 | #ifndef HAVE_EPOLL_CREATE1 | 
| Victor Stinner | d72fe89 | 2013-08-28 12:22:39 +0200 | [diff] [blame] | 1281 |     if (fd == -1 && _Py_set_inheritable(self->epfd, 0, NULL) < 0) { | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1282 |         Py_DECREF(self); | 
 | 1283 |         return NULL; | 
 | 1284 |     } | 
 | 1285 | #endif | 
 | 1286 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 |     return (PyObject *)self; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1288 | } | 
 | 1289 |  | 
 | 1290 |  | 
 | 1291 | static PyObject * | 
 | 1292 | pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 1293 | { | 
| Benjamin Peterson | 83251c1 | 2011-12-27 16:01:21 -0600 | [diff] [blame] | 1294 |     int flags = 0, sizehint = FD_SETSIZE - 1; | 
| Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 1295 |     static char *kwlist[] = {"sizehint", "flags", NULL}; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1296 |  | 
| Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 1297 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist, | 
 | 1298 |                                      &sizehint, &flags)) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 |         return NULL; | 
| Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 1300 |     if (sizehint < 0) { | 
 | 1301 |         PyErr_SetString(PyExc_ValueError, "negative sizehint"); | 
 | 1302 |         return NULL; | 
 | 1303 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1304 |  | 
| Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 1305 |     return newPyEpoll_Object(type, sizehint, flags, -1); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1306 | } | 
 | 1307 |  | 
 | 1308 |  | 
 | 1309 | static void | 
 | 1310 | pyepoll_dealloc(pyEpoll_Object *self) | 
 | 1311 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 |     (void)pyepoll_internal_close(self); | 
 | 1313 |     Py_TYPE(self)->tp_free(self); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1314 | } | 
 | 1315 |  | 
 | 1316 | static PyObject* | 
 | 1317 | pyepoll_close(pyEpoll_Object *self) | 
 | 1318 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 |     errno = pyepoll_internal_close(self); | 
 | 1320 |     if (errno < 0) { | 
| Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1321 |         PyErr_SetFromErrno(PyExc_OSError); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 |         return NULL; | 
 | 1323 |     } | 
 | 1324 |     Py_RETURN_NONE; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1325 | } | 
 | 1326 |  | 
 | 1327 | PyDoc_STRVAR(pyepoll_close_doc, | 
 | 1328 | "close() -> None\n\ | 
 | 1329 | \n\ | 
 | 1330 | Close the epoll control file descriptor. Further operations on the epoll\n\ | 
 | 1331 | object will raise an exception."); | 
 | 1332 |  | 
 | 1333 | static PyObject* | 
 | 1334 | pyepoll_get_closed(pyEpoll_Object *self) | 
 | 1335 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 |     if (self->epfd < 0) | 
 | 1337 |         Py_RETURN_TRUE; | 
 | 1338 |     else | 
 | 1339 |         Py_RETURN_FALSE; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1340 | } | 
 | 1341 |  | 
 | 1342 | static PyObject* | 
 | 1343 | pyepoll_fileno(pyEpoll_Object *self) | 
 | 1344 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 |     if (self->epfd < 0) | 
 | 1346 |         return pyepoll_err_closed(); | 
 | 1347 |     return PyLong_FromLong(self->epfd); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1348 | } | 
 | 1349 |  | 
 | 1350 | PyDoc_STRVAR(pyepoll_fileno_doc, | 
 | 1351 | "fileno() -> int\n\ | 
 | 1352 | \n\ | 
 | 1353 | Return the epoll control file descriptor."); | 
 | 1354 |  | 
 | 1355 | static PyObject* | 
 | 1356 | pyepoll_fromfd(PyObject *cls, PyObject *args) | 
 | 1357 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 |     SOCKET fd; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1359 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 |     if (!PyArg_ParseTuple(args, "i:fromfd", &fd)) | 
 | 1361 |         return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1362 |  | 
| Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 1363 |     return newPyEpoll_Object((PyTypeObject*)cls, FD_SETSIZE - 1, 0, fd); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1364 | } | 
 | 1365 |  | 
 | 1366 | PyDoc_STRVAR(pyepoll_fromfd_doc, | 
 | 1367 | "fromfd(fd) -> epoll\n\ | 
 | 1368 | \n\ | 
 | 1369 | Create an epoll object from a given control fd."); | 
 | 1370 |  | 
 | 1371 | static PyObject * | 
 | 1372 | pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events) | 
 | 1373 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 |     struct epoll_event ev; | 
 | 1375 |     int result; | 
 | 1376 |     int fd; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1377 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 |     if (epfd < 0) | 
 | 1379 |         return pyepoll_err_closed(); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1380 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1381 |     fd = PyObject_AsFileDescriptor(pfd); | 
 | 1382 |     if (fd == -1) { | 
 | 1383 |         return NULL; | 
 | 1384 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1385 |  | 
| Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1386 |     switch (op) { | 
 | 1387 |     case EPOLL_CTL_ADD: | 
 | 1388 |     case EPOLL_CTL_MOD: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 |         ev.events = events; | 
 | 1390 |         ev.data.fd = fd; | 
 | 1391 |         Py_BEGIN_ALLOW_THREADS | 
 | 1392 |         result = epoll_ctl(epfd, op, fd, &ev); | 
 | 1393 |         Py_END_ALLOW_THREADS | 
 | 1394 |         break; | 
| Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1395 |     case EPOLL_CTL_DEL: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 |         /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL | 
 | 1397 |          * operation required a non-NULL pointer in event, even | 
 | 1398 |          * though this argument is ignored. */ | 
 | 1399 |         Py_BEGIN_ALLOW_THREADS | 
 | 1400 |         result = epoll_ctl(epfd, op, fd, &ev); | 
 | 1401 |         if (errno == EBADF) { | 
 | 1402 |             /* fd already closed */ | 
 | 1403 |             result = 0; | 
 | 1404 |             errno = 0; | 
 | 1405 |         } | 
 | 1406 |         Py_END_ALLOW_THREADS | 
 | 1407 |         break; | 
| Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1408 |     default: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1409 |         result = -1; | 
 | 1410 |         errno = EINVAL; | 
 | 1411 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1412 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1413 |     if (result < 0) { | 
| Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1414 |         PyErr_SetFromErrno(PyExc_OSError); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1415 |         return NULL; | 
 | 1416 |     } | 
 | 1417 |     Py_RETURN_NONE; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1418 | } | 
 | 1419 |  | 
 | 1420 | static PyObject * | 
 | 1421 | pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds) | 
 | 1422 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 |     PyObject *pfd; | 
 | 1424 |     unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI; | 
 | 1425 |     static char *kwlist[] = {"fd", "eventmask", NULL}; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1426 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist, | 
 | 1428 |                                      &pfd, &events)) { | 
 | 1429 |         return NULL; | 
 | 1430 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1431 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1432 |     return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1433 | } | 
 | 1434 |  | 
 | 1435 | PyDoc_STRVAR(pyepoll_register_doc, | 
| Georg Brandl | 222569d | 2010-08-02 20:47:56 +0000 | [diff] [blame] | 1436 | "register(fd[, eventmask]) -> None\n\ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1437 | \n\ | 
| Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1438 | Registers a new fd or raises an OSError if the fd is already registered.\n\ | 
| Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 1439 | fd is the target file descriptor of the operation.\n\ | 
 | 1440 | events is a bit set composed of the various EPOLL constants; the default\n\ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1441 | is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\ | 
 | 1442 | \n\ | 
 | 1443 | The epoll interface supports all file descriptors that support poll."); | 
 | 1444 |  | 
 | 1445 | static PyObject * | 
 | 1446 | pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds) | 
 | 1447 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 |     PyObject *pfd; | 
 | 1449 |     unsigned int events; | 
 | 1450 |     static char *kwlist[] = {"fd", "eventmask", NULL}; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1451 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist, | 
 | 1453 |                                      &pfd, &events)) { | 
 | 1454 |         return NULL; | 
 | 1455 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1456 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1457 |     return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1458 | } | 
 | 1459 |  | 
 | 1460 | PyDoc_STRVAR(pyepoll_modify_doc, | 
 | 1461 | "modify(fd, eventmask) -> None\n\ | 
 | 1462 | \n\ | 
 | 1463 | fd is the target file descriptor of the operation\n\ | 
 | 1464 | events is a bit set composed of the various EPOLL constants"); | 
 | 1465 |  | 
 | 1466 | static PyObject * | 
 | 1467 | pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds) | 
 | 1468 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 |     PyObject *pfd; | 
 | 1470 |     static char *kwlist[] = {"fd", NULL}; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1471 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1472 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist, | 
 | 1473 |                                      &pfd)) { | 
 | 1474 |         return NULL; | 
 | 1475 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1476 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1477 |     return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1478 | } | 
 | 1479 |  | 
 | 1480 | PyDoc_STRVAR(pyepoll_unregister_doc, | 
 | 1481 | "unregister(fd) -> None\n\ | 
 | 1482 | \n\ | 
 | 1483 | fd is the target file descriptor of the operation."); | 
 | 1484 |  | 
 | 1485 | static PyObject * | 
 | 1486 | pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds) | 
 | 1487 | { | 
| Victor Stinner | 41eba22 | 2015-03-30 21:59:21 +0200 | [diff] [blame] | 1488 |     static char *kwlist[] = {"timeout", "maxevents", NULL}; | 
 | 1489 |     PyObject *timeout_obj = NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 |     int maxevents = -1; | 
 | 1491 |     int nfds, i; | 
 | 1492 |     PyObject *elist = NULL, *etuple = NULL; | 
| Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1493 |     struct epoll_event *evs = NULL; | 
| Victor Stinner | 41eba22 | 2015-03-30 21:59:21 +0200 | [diff] [blame] | 1494 |     _PyTime_t timeout, ms, deadline; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1495 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 |     if (self->epfd < 0) | 
 | 1497 |         return pyepoll_err_closed(); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1498 |  | 
| Victor Stinner | 41eba22 | 2015-03-30 21:59:21 +0200 | [diff] [blame] | 1499 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:poll", kwlist, | 
 | 1500 |                                      &timeout_obj, &maxevents)) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 |         return NULL; | 
 | 1502 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1503 |  | 
| Victor Stinner | 41eba22 | 2015-03-30 21:59:21 +0200 | [diff] [blame] | 1504 |     if (timeout_obj == NULL || timeout_obj == Py_None) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 |         timeout = -1; | 
| Victor Stinner | 41eba22 | 2015-03-30 21:59:21 +0200 | [diff] [blame] | 1506 |         ms = -1; | 
 | 1507 |         deadline = 0;   /* initialize to prevent gcc warning */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1508 |     } | 
 | 1509 |     else { | 
| Victor Stinner | 41eba22 | 2015-03-30 21:59:21 +0200 | [diff] [blame] | 1510 |         /* epoll_wait() has a resolution of 1 millisecond, round towards | 
 | 1511 |            infinity to wait at least timeout seconds. */ | 
 | 1512 |         if (_PyTime_FromSecondsObject(&timeout, timeout_obj, | 
 | 1513 |                                       _PyTime_ROUND_CEILING) < 0) { | 
 | 1514 |             if (PyErr_ExceptionMatches(PyExc_TypeError)) { | 
 | 1515 |                 PyErr_SetString(PyExc_TypeError, | 
 | 1516 |                                 "timeout must be an integer or None"); | 
 | 1517 |             } | 
 | 1518 |             return NULL; | 
 | 1519 |         } | 
 | 1520 |  | 
 | 1521 |         ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); | 
 | 1522 |         if (ms < INT_MIN || ms > INT_MAX) { | 
 | 1523 |             PyErr_SetString(PyExc_OverflowError, "timeout is too large"); | 
 | 1524 |             return NULL; | 
 | 1525 |         } | 
 | 1526 |  | 
 | 1527 |         deadline = _PyTime_GetMonotonicClock() + timeout; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1528 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1529 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 |     if (maxevents == -1) { | 
| Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1531 |         maxevents = FD_SETSIZE-1; | 
 | 1532 |     } | 
 | 1533 |     else if (maxevents < 1) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 |         PyErr_Format(PyExc_ValueError, | 
 | 1535 |                      "maxevents must be greater than 0, got %d", | 
 | 1536 |                      maxevents); | 
 | 1537 |         return NULL; | 
 | 1538 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1539 |  | 
| Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1540 |     evs = PyMem_New(struct epoll_event, maxevents); | 
 | 1541 |     if (evs == NULL) { | 
| Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1542 |         PyErr_NoMemory(); | 
 | 1543 |         return NULL; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1545 |  | 
| Victor Stinner | 41eba22 | 2015-03-30 21:59:21 +0200 | [diff] [blame] | 1546 |     do { | 
 | 1547 |         Py_BEGIN_ALLOW_THREADS | 
 | 1548 |         errno = 0; | 
 | 1549 |         nfds = epoll_wait(self->epfd, evs, maxevents, (int)ms); | 
 | 1550 |         Py_END_ALLOW_THREADS | 
 | 1551 |  | 
 | 1552 |         if (errno != EINTR) | 
 | 1553 |             break; | 
 | 1554 |  | 
 | 1555 |         /* poll() was interrupted by a signal */ | 
 | 1556 |         if (PyErr_CheckSignals()) | 
 | 1557 |             goto error; | 
 | 1558 |  | 
 | 1559 |         if (timeout >= 0) { | 
 | 1560 |             timeout = deadline - _PyTime_GetMonotonicClock(); | 
 | 1561 |             if (timeout < 0) { | 
 | 1562 |                 nfds = 0; | 
 | 1563 |                 break; | 
 | 1564 |             } | 
 | 1565 |             ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); | 
 | 1566 |             /* retry epoll_wait() with the recomputed timeout */ | 
 | 1567 |         } | 
 | 1568 |     } while(1); | 
 | 1569 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 |     if (nfds < 0) { | 
| Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1571 |         PyErr_SetFromErrno(PyExc_OSError); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 |         goto error; | 
 | 1573 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1574 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 |     elist = PyList_New(nfds); | 
 | 1576 |     if (elist == NULL) { | 
 | 1577 |         goto error; | 
 | 1578 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1579 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1580 |     for (i = 0; i < nfds; i++) { | 
| Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1581 |         etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 |         if (etuple == NULL) { | 
 | 1583 |             Py_CLEAR(elist); | 
 | 1584 |             goto error; | 
 | 1585 |         } | 
 | 1586 |         PyList_SET_ITEM(elist, i, etuple); | 
 | 1587 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1588 |  | 
| Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 1589 |     error: | 
| Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1590 |     PyMem_Free(evs); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 |     return elist; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1592 | } | 
 | 1593 |  | 
 | 1594 | PyDoc_STRVAR(pyepoll_poll_doc, | 
 | 1595 | "poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\ | 
 | 1596 | \n\ | 
 | 1597 | Wait for events on the epoll file descriptor for a maximum time of timeout\n\ | 
 | 1598 | in seconds (as float). -1 makes poll wait indefinitely.\n\ | 
 | 1599 | Up to maxevents are returned to the caller."); | 
 | 1600 |  | 
| Antoine Pitrou | 09bb89b | 2012-12-15 21:14:21 +0100 | [diff] [blame] | 1601 | static PyObject * | 
 | 1602 | pyepoll_enter(pyEpoll_Object *self, PyObject *args) | 
 | 1603 | { | 
 | 1604 |     if (self->epfd < 0) | 
 | 1605 |         return pyepoll_err_closed(); | 
 | 1606 |  | 
 | 1607 |     Py_INCREF(self); | 
 | 1608 |     return (PyObject *)self; | 
 | 1609 | } | 
 | 1610 |  | 
 | 1611 | static PyObject * | 
 | 1612 | pyepoll_exit(PyObject *self, PyObject *args) | 
 | 1613 | { | 
 | 1614 |     _Py_IDENTIFIER(close); | 
 | 1615 |  | 
 | 1616 |     return _PyObject_CallMethodId(self, &PyId_close, NULL); | 
 | 1617 | } | 
 | 1618 |  | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1619 | static PyMethodDef pyepoll_methods[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 |     {"fromfd",          (PyCFunction)pyepoll_fromfd, | 
 | 1621 |      METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc}, | 
 | 1622 |     {"close",           (PyCFunction)pyepoll_close,     METH_NOARGS, | 
 | 1623 |      pyepoll_close_doc}, | 
 | 1624 |     {"fileno",          (PyCFunction)pyepoll_fileno,    METH_NOARGS, | 
 | 1625 |      pyepoll_fileno_doc}, | 
 | 1626 |     {"modify",          (PyCFunction)pyepoll_modify, | 
 | 1627 |      METH_VARARGS | METH_KEYWORDS,      pyepoll_modify_doc}, | 
 | 1628 |     {"register",        (PyCFunction)pyepoll_register, | 
 | 1629 |      METH_VARARGS | METH_KEYWORDS,      pyepoll_register_doc}, | 
 | 1630 |     {"unregister",      (PyCFunction)pyepoll_unregister, | 
 | 1631 |      METH_VARARGS | METH_KEYWORDS,      pyepoll_unregister_doc}, | 
 | 1632 |     {"poll",            (PyCFunction)pyepoll_poll, | 
 | 1633 |      METH_VARARGS | METH_KEYWORDS,      pyepoll_poll_doc}, | 
| Antoine Pitrou | 09bb89b | 2012-12-15 21:14:21 +0100 | [diff] [blame] | 1634 |     {"__enter__",           (PyCFunction)pyepoll_enter,     METH_NOARGS, | 
 | 1635 |      NULL}, | 
 | 1636 |     {"__exit__",           (PyCFunction)pyepoll_exit,     METH_VARARGS, | 
 | 1637 |      NULL}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 |     {NULL,      NULL}, | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1639 | }; | 
 | 1640 |  | 
 | 1641 | static PyGetSetDef pyepoll_getsetlist[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1642 |     {"closed", (getter)pyepoll_get_closed, NULL, | 
 | 1643 |      "True if the epoll handler is closed"}, | 
 | 1644 |     {0}, | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1645 | }; | 
 | 1646 |  | 
 | 1647 | PyDoc_STRVAR(pyepoll_doc, | 
| Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 1648 | "select.epoll(sizehint=-1, flags=0)\n\ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1649 | \n\ | 
 | 1650 | Returns an epolling object\n\ | 
 | 1651 | \n\ | 
 | 1652 | sizehint must be a positive integer or -1 for the default size. The\n\ | 
 | 1653 | sizehint is used to optimize internal data structures. It doesn't limit\n\ | 
 | 1654 | the maximum number of monitored events."); | 
 | 1655 |  | 
 | 1656 | static PyTypeObject pyEpoll_Type = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1657 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 1658 |     "select.epoll",                                     /* tp_name */ | 
 | 1659 |     sizeof(pyEpoll_Object),                             /* tp_basicsize */ | 
 | 1660 |     0,                                                  /* tp_itemsize */ | 
 | 1661 |     (destructor)pyepoll_dealloc,                        /* tp_dealloc */ | 
 | 1662 |     0,                                                  /* tp_print */ | 
 | 1663 |     0,                                                  /* tp_getattr */ | 
 | 1664 |     0,                                                  /* tp_setattr */ | 
 | 1665 |     0,                                                  /* tp_reserved */ | 
 | 1666 |     0,                                                  /* tp_repr */ | 
 | 1667 |     0,                                                  /* tp_as_number */ | 
 | 1668 |     0,                                                  /* tp_as_sequence */ | 
 | 1669 |     0,                                                  /* tp_as_mapping */ | 
 | 1670 |     0,                                                  /* tp_hash */ | 
 | 1671 |     0,                                                  /* tp_call */ | 
 | 1672 |     0,                                                  /* tp_str */ | 
 | 1673 |     PyObject_GenericGetAttr,                            /* tp_getattro */ | 
 | 1674 |     0,                                                  /* tp_setattro */ | 
 | 1675 |     0,                                                  /* tp_as_buffer */ | 
 | 1676 |     Py_TPFLAGS_DEFAULT,                                 /* tp_flags */ | 
 | 1677 |     pyepoll_doc,                                        /* tp_doc */ | 
 | 1678 |     0,                                                  /* tp_traverse */ | 
 | 1679 |     0,                                                  /* tp_clear */ | 
 | 1680 |     0,                                                  /* tp_richcompare */ | 
 | 1681 |     0,                                                  /* tp_weaklistoffset */ | 
 | 1682 |     0,                                                  /* tp_iter */ | 
 | 1683 |     0,                                                  /* tp_iternext */ | 
 | 1684 |     pyepoll_methods,                                    /* tp_methods */ | 
 | 1685 |     0,                                                  /* tp_members */ | 
 | 1686 |     pyepoll_getsetlist,                                 /* tp_getset */ | 
 | 1687 |     0,                                                  /* tp_base */ | 
 | 1688 |     0,                                                  /* tp_dict */ | 
 | 1689 |     0,                                                  /* tp_descr_get */ | 
 | 1690 |     0,                                                  /* tp_descr_set */ | 
 | 1691 |     0,                                                  /* tp_dictoffset */ | 
 | 1692 |     0,                                                  /* tp_init */ | 
 | 1693 |     0,                                                  /* tp_alloc */ | 
 | 1694 |     pyepoll_new,                                        /* tp_new */ | 
 | 1695 |     0,                                                  /* tp_free */ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1696 | }; | 
 | 1697 |  | 
 | 1698 | #endif /* HAVE_EPOLL */ | 
 | 1699 |  | 
 | 1700 | #ifdef HAVE_KQUEUE | 
 | 1701 | /* ************************************************************************** | 
 | 1702 |  *                      kqueue interface for BSD | 
 | 1703 |  * | 
 | 1704 |  * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes | 
 | 1705 |  * All rights reserved. | 
 | 1706 |  * | 
 | 1707 |  * Redistribution and use in source and binary forms, with or without | 
 | 1708 |  * modification, are permitted provided that the following conditions | 
 | 1709 |  * are met: | 
 | 1710 |  * 1. Redistributions of source code must retain the above copyright | 
 | 1711 |  *    notice, this list of conditions and the following disclaimer. | 
 | 1712 |  * 2. Redistributions in binary form must reproduce the above copyright | 
 | 1713 |  *    notice, this list of conditions and the following disclaimer in the | 
 | 1714 |  *    documentation and/or other materials provided with the distribution. | 
 | 1715 |  * | 
 | 1716 |  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | 
 | 1717 |  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
 | 1718 |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
 | 1719 |  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | 
 | 1720 |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
 | 1721 |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
 | 1722 |  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
 | 1723 |  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
 | 1724 |  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
 | 1725 |  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
 | 1726 |  * SUCH DAMAGE. | 
 | 1727 |  */ | 
 | 1728 |  | 
 | 1729 | #ifdef HAVE_SYS_EVENT_H | 
 | 1730 | #include <sys/event.h> | 
 | 1731 | #endif | 
 | 1732 |  | 
 | 1733 | PyDoc_STRVAR(kqueue_event_doc, | 
| Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 1734 | "kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1735 | \n\ | 
 | 1736 | This object is the equivalent of the struct kevent for the C API.\n\ | 
 | 1737 | \n\ | 
 | 1738 | See the kqueue manpage for more detailed information about the meaning\n\ | 
 | 1739 | of the arguments.\n\ | 
 | 1740 | \n\ | 
 | 1741 | One minor note: while you might hope that udata could store a\n\ | 
 | 1742 | reference to a python object, it cannot, because it is impossible to\n\ | 
 | 1743 | keep a proper reference count of the object once it's passed into the\n\ | 
 | 1744 | kernel. Therefore, I have restricted it to only storing an integer.  I\n\ | 
 | 1745 | recommend ignoring it and simply using the 'ident' field to key off\n\ | 
 | 1746 | of. You could also set up a dictionary on the python side to store a\n\ | 
 | 1747 | udata->object mapping."); | 
 | 1748 |  | 
 | 1749 | typedef struct { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 |     PyObject_HEAD | 
 | 1751 |     struct kevent e; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1752 | } kqueue_event_Object; | 
 | 1753 |  | 
 | 1754 | static PyTypeObject kqueue_event_Type; | 
 | 1755 |  | 
 | 1756 | #define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type)) | 
 | 1757 |  | 
 | 1758 | typedef struct { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 |     PyObject_HEAD | 
 | 1760 |     SOCKET kqfd;                /* kqueue control fd */ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1761 | } kqueue_queue_Object; | 
 | 1762 |  | 
 | 1763 | static PyTypeObject kqueue_queue_Type; | 
 | 1764 |  | 
 | 1765 | #define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type)) | 
 | 1766 |  | 
| Antoine Pitrou | d83f1e6 | 2009-11-04 21:10:38 +0000 | [diff] [blame] | 1767 | #if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P) | 
 | 1768 | #   error uintptr_t does not match void *! | 
 | 1769 | #elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG) | 
 | 1770 | #   define T_UINTPTRT         T_ULONGLONG | 
 | 1771 | #   define T_INTPTRT          T_LONGLONG | 
 | 1772 | #   define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong | 
 | 1773 | #   define UINTPTRT_FMT_UNIT  "K" | 
 | 1774 | #   define INTPTRT_FMT_UNIT   "L" | 
 | 1775 | #elif (SIZEOF_UINTPTR_T == SIZEOF_LONG) | 
 | 1776 | #   define T_UINTPTRT         T_ULONG | 
 | 1777 | #   define T_INTPTRT          T_LONG | 
 | 1778 | #   define PyLong_AsUintptr_t PyLong_AsUnsignedLong | 
 | 1779 | #   define UINTPTRT_FMT_UNIT  "k" | 
 | 1780 | #   define INTPTRT_FMT_UNIT   "l" | 
 | 1781 | #elif (SIZEOF_UINTPTR_T == SIZEOF_INT) | 
 | 1782 | #   define T_UINTPTRT         T_UINT | 
 | 1783 | #   define T_INTPTRT          T_INT | 
 | 1784 | #   define PyLong_AsUintptr_t PyLong_AsUnsignedLong | 
 | 1785 | #   define UINTPTRT_FMT_UNIT  "I" | 
 | 1786 | #   define INTPTRT_FMT_UNIT   "i" | 
 | 1787 | #else | 
 | 1788 | #   error uintptr_t does not match int, long, or long long! | 
 | 1789 | #endif | 
 | 1790 |  | 
| Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1791 | /* | 
 | 1792 |  * kevent is not standard and its members vary across BSDs. | 
 | 1793 |  */ | 
 | 1794 | #if !defined(__OpenBSD__) | 
| Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 1795 | #   define IDENT_TYPE  T_UINTPTRT | 
 | 1796 | #   define IDENT_CAST  Py_intptr_t | 
 | 1797 | #   define DATA_TYPE   T_INTPTRT | 
| Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1798 | #   define DATA_FMT_UNIT INTPTRT_FMT_UNIT | 
| Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 1799 | #   define IDENT_AsType PyLong_AsUintptr_t | 
| Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1800 | #else | 
| Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 1801 | #   define IDENT_TYPE  T_UINT | 
 | 1802 | #   define IDENT_CAST  int | 
 | 1803 | #   define DATA_TYPE   T_INT | 
| Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1804 | #   define DATA_FMT_UNIT "i" | 
| Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 1805 | #   define IDENT_AsType PyLong_AsUnsignedLong | 
| Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1806 | #endif | 
 | 1807 |  | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1808 | /* Unfortunately, we can't store python objects in udata, because | 
 | 1809 |  * kevents in the kernel can be removed without warning, which would | 
 | 1810 |  * forever lose the refcount on the object stored with it. | 
 | 1811 |  */ | 
 | 1812 |  | 
 | 1813 | #define KQ_OFF(x) offsetof(kqueue_event_Object, x) | 
 | 1814 | static struct PyMemberDef kqueue_event_members[] = { | 
| Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1815 |     {"ident",           IDENT_TYPE,     KQ_OFF(e.ident)}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 |     {"filter",          T_SHORT,        KQ_OFF(e.filter)}, | 
 | 1817 |     {"flags",           T_USHORT,       KQ_OFF(e.flags)}, | 
 | 1818 |     {"fflags",          T_UINT,         KQ_OFF(e.fflags)}, | 
| Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1819 |     {"data",            DATA_TYPE,      KQ_OFF(e.data)}, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 |     {"udata",           T_UINTPTRT,     KQ_OFF(e.udata)}, | 
 | 1821 |     {NULL} /* Sentinel */ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1822 | }; | 
 | 1823 | #undef KQ_OFF | 
 | 1824 |  | 
 | 1825 | static PyObject * | 
| Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 1826 |  | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1827 | kqueue_event_repr(kqueue_event_Object *s) | 
 | 1828 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 |     char buf[1024]; | 
 | 1830 |     PyOS_snprintf( | 
 | 1831 |         buf, sizeof(buf), | 
 | 1832 |         "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x " | 
 | 1833 |         "data=0x%zd udata=%p>", | 
 | 1834 |         (size_t)(s->e.ident), s->e.filter, s->e.flags, | 
 | 1835 |         s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata); | 
 | 1836 |     return PyUnicode_FromString(buf); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1837 | } | 
 | 1838 |  | 
 | 1839 | static int | 
 | 1840 | kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds) | 
 | 1841 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 |     PyObject *pfd; | 
 | 1843 |     static char *kwlist[] = {"ident", "filter", "flags", "fflags", | 
 | 1844 |                              "data", "udata", NULL}; | 
| Christian Heimes | f1fe159 | 2013-08-25 14:57:00 +0200 | [diff] [blame] | 1845 |     static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent"; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1846 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 |     EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1848 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 |     if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist, | 
 | 1850 |         &pfd, &(self->e.filter), &(self->e.flags), | 
 | 1851 |         &(self->e.fflags), &(self->e.data), &(self->e.udata))) { | 
 | 1852 |         return -1; | 
 | 1853 |     } | 
 | 1854 |  | 
| Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1855 |     if (PyLong_Check(pfd) | 
 | 1856 | #if IDENT_TYPE == T_UINT | 
| Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 1857 |         && PyLong_AsUnsignedLong(pfd) <= UINT_MAX | 
| Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1858 | #endif | 
 | 1859 |     ) { | 
 | 1860 |         self->e.ident = IDENT_AsType(pfd); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 |     } | 
 | 1862 |     else { | 
 | 1863 |         self->e.ident = PyObject_AsFileDescriptor(pfd); | 
 | 1864 |     } | 
 | 1865 |     if (PyErr_Occurred()) { | 
 | 1866 |         return -1; | 
 | 1867 |     } | 
 | 1868 |     return 0; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1869 | } | 
 | 1870 |  | 
 | 1871 | static PyObject * | 
 | 1872 | kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o, | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 |                          int op) | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1874 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 |     Py_intptr_t result = 0; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1876 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1877 |     if (!kqueue_event_Check(o)) { | 
 | 1878 |         if (op == Py_EQ || op == Py_NE) { | 
 | 1879 |             PyObject *res = op == Py_EQ ? Py_False : Py_True; | 
 | 1880 |             Py_INCREF(res); | 
 | 1881 |             return res; | 
 | 1882 |         } | 
 | 1883 |         PyErr_Format(PyExc_TypeError, | 
 | 1884 |             "can't compare %.200s to %.200s", | 
 | 1885 |             Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name); | 
 | 1886 |         return NULL; | 
 | 1887 |     } | 
| Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1888 |     if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) && | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 |         ((result = s->e.filter - o->e.filter) == 0) && | 
 | 1890 |         ((result = s->e.flags - o->e.flags) == 0) && | 
| Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1891 |         ((result = (int)(s->e.fflags - o->e.fflags)) == 0) && | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 |         ((result = s->e.data - o->e.data) == 0) && | 
 | 1893 |         ((result = s->e.udata - o->e.udata) == 0) | 
 | 1894 |        ) { | 
 | 1895 |         result = 0; | 
 | 1896 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1897 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 |     switch (op) { | 
| Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1899 |     case Py_EQ: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 |         result = (result == 0); | 
 | 1901 |         break; | 
| Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1902 |     case Py_NE: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 |         result = (result != 0); | 
 | 1904 |         break; | 
| Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1905 |     case Py_LE: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 |         result = (result <= 0); | 
 | 1907 |         break; | 
| Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1908 |     case Py_GE: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 |         result = (result >= 0); | 
 | 1910 |         break; | 
| Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1911 |     case Py_LT: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 |         result = (result < 0); | 
 | 1913 |         break; | 
| Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1914 |     case Py_GT: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 |         result = (result > 0); | 
 | 1916 |         break; | 
 | 1917 |     } | 
 | 1918 |     return PyBool_FromLong((long)result); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1919 | } | 
 | 1920 |  | 
 | 1921 | static PyTypeObject kqueue_event_Type = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1922 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 1923 |     "select.kevent",                                    /* tp_name */ | 
 | 1924 |     sizeof(kqueue_event_Object),                        /* tp_basicsize */ | 
 | 1925 |     0,                                                  /* tp_itemsize */ | 
 | 1926 |     0,                                                  /* tp_dealloc */ | 
 | 1927 |     0,                                                  /* tp_print */ | 
 | 1928 |     0,                                                  /* tp_getattr */ | 
 | 1929 |     0,                                                  /* tp_setattr */ | 
 | 1930 |     0,                                                  /* tp_reserved */ | 
 | 1931 |     (reprfunc)kqueue_event_repr,                        /* tp_repr */ | 
 | 1932 |     0,                                                  /* tp_as_number */ | 
 | 1933 |     0,                                                  /* tp_as_sequence */ | 
 | 1934 |     0,                                                  /* tp_as_mapping */ | 
 | 1935 |     0,                                                  /* tp_hash */ | 
 | 1936 |     0,                                                  /* tp_call */ | 
 | 1937 |     0,                                                  /* tp_str */ | 
 | 1938 |     0,                                                  /* tp_getattro */ | 
 | 1939 |     0,                                                  /* tp_setattro */ | 
 | 1940 |     0,                                                  /* tp_as_buffer */ | 
 | 1941 |     Py_TPFLAGS_DEFAULT,                                 /* tp_flags */ | 
 | 1942 |     kqueue_event_doc,                                   /* tp_doc */ | 
 | 1943 |     0,                                                  /* tp_traverse */ | 
 | 1944 |     0,                                                  /* tp_clear */ | 
 | 1945 |     (richcmpfunc)kqueue_event_richcompare,              /* tp_richcompare */ | 
 | 1946 |     0,                                                  /* tp_weaklistoffset */ | 
 | 1947 |     0,                                                  /* tp_iter */ | 
 | 1948 |     0,                                                  /* tp_iternext */ | 
 | 1949 |     0,                                                  /* tp_methods */ | 
 | 1950 |     kqueue_event_members,                               /* tp_members */ | 
 | 1951 |     0,                                                  /* tp_getset */ | 
 | 1952 |     0,                                                  /* tp_base */ | 
 | 1953 |     0,                                                  /* tp_dict */ | 
 | 1954 |     0,                                                  /* tp_descr_get */ | 
 | 1955 |     0,                                                  /* tp_descr_set */ | 
 | 1956 |     0,                                                  /* tp_dictoffset */ | 
 | 1957 |     (initproc)kqueue_event_init,                        /* tp_init */ | 
 | 1958 |     0,                                                  /* tp_alloc */ | 
 | 1959 |     0,                                                  /* tp_new */ | 
 | 1960 |     0,                                                  /* tp_free */ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1961 | }; | 
 | 1962 |  | 
 | 1963 | static PyObject * | 
 | 1964 | kqueue_queue_err_closed(void) | 
 | 1965 | { | 
| Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 1966 |     PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue object"); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1967 |     return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1968 | } | 
 | 1969 |  | 
 | 1970 | static int | 
 | 1971 | kqueue_queue_internal_close(kqueue_queue_Object *self) | 
 | 1972 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1973 |     int save_errno = 0; | 
 | 1974 |     if (self->kqfd >= 0) { | 
 | 1975 |         int kqfd = self->kqfd; | 
 | 1976 |         self->kqfd = -1; | 
 | 1977 |         Py_BEGIN_ALLOW_THREADS | 
 | 1978 |         if (close(kqfd) < 0) | 
 | 1979 |             save_errno = errno; | 
 | 1980 |         Py_END_ALLOW_THREADS | 
 | 1981 |     } | 
 | 1982 |     return save_errno; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1983 | } | 
 | 1984 |  | 
 | 1985 | static PyObject * | 
 | 1986 | newKqueue_Object(PyTypeObject *type, SOCKET fd) | 
 | 1987 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 |     kqueue_queue_Object *self; | 
 | 1989 |     assert(type != NULL && type->tp_alloc != NULL); | 
 | 1990 |     self = (kqueue_queue_Object *) type->tp_alloc(type, 0); | 
 | 1991 |     if (self == NULL) { | 
 | 1992 |         return NULL; | 
 | 1993 |     } | 
 | 1994 |  | 
 | 1995 |     if (fd == -1) { | 
 | 1996 |         Py_BEGIN_ALLOW_THREADS | 
 | 1997 |         self->kqfd = kqueue(); | 
 | 1998 |         Py_END_ALLOW_THREADS | 
 | 1999 |     } | 
 | 2000 |     else { | 
 | 2001 |         self->kqfd = fd; | 
 | 2002 |     } | 
 | 2003 |     if (self->kqfd < 0) { | 
 | 2004 |         Py_DECREF(self); | 
| Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2005 |         PyErr_SetFromErrno(PyExc_OSError); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2006 |         return NULL; | 
 | 2007 |     } | 
| Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 2008 |  | 
 | 2009 |     if (fd == -1) { | 
 | 2010 |         if (_Py_set_inheritable(self->kqfd, 0, NULL) < 0) { | 
 | 2011 |             Py_DECREF(self); | 
 | 2012 |             return NULL; | 
 | 2013 |         } | 
 | 2014 |     } | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 |     return (PyObject *)self; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2016 | } | 
 | 2017 |  | 
 | 2018 | static PyObject * | 
 | 2019 | kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 2020 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2021 |     if ((args != NULL && PyObject_Size(args)) || | 
 | 2022 |                     (kwds != NULL && PyObject_Size(kwds))) { | 
 | 2023 |         PyErr_SetString(PyExc_ValueError, | 
 | 2024 |                         "select.kqueue doesn't accept arguments"); | 
 | 2025 |         return NULL; | 
 | 2026 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2027 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 |     return newKqueue_Object(type, -1); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2029 | } | 
 | 2030 |  | 
 | 2031 | static void | 
 | 2032 | kqueue_queue_dealloc(kqueue_queue_Object *self) | 
 | 2033 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2034 |     kqueue_queue_internal_close(self); | 
 | 2035 |     Py_TYPE(self)->tp_free(self); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2036 | } | 
 | 2037 |  | 
 | 2038 | static PyObject* | 
 | 2039 | kqueue_queue_close(kqueue_queue_Object *self) | 
 | 2040 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 |     errno = kqueue_queue_internal_close(self); | 
 | 2042 |     if (errno < 0) { | 
| Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2043 |         PyErr_SetFromErrno(PyExc_OSError); | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 |         return NULL; | 
 | 2045 |     } | 
 | 2046 |     Py_RETURN_NONE; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2047 | } | 
 | 2048 |  | 
 | 2049 | PyDoc_STRVAR(kqueue_queue_close_doc, | 
 | 2050 | "close() -> None\n\ | 
 | 2051 | \n\ | 
 | 2052 | Close the kqueue control file descriptor. Further operations on the kqueue\n\ | 
 | 2053 | object will raise an exception."); | 
 | 2054 |  | 
 | 2055 | static PyObject* | 
 | 2056 | kqueue_queue_get_closed(kqueue_queue_Object *self) | 
 | 2057 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2058 |     if (self->kqfd < 0) | 
 | 2059 |         Py_RETURN_TRUE; | 
 | 2060 |     else | 
 | 2061 |         Py_RETURN_FALSE; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2062 | } | 
 | 2063 |  | 
 | 2064 | static PyObject* | 
 | 2065 | kqueue_queue_fileno(kqueue_queue_Object *self) | 
 | 2066 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 |     if (self->kqfd < 0) | 
 | 2068 |         return kqueue_queue_err_closed(); | 
 | 2069 |     return PyLong_FromLong(self->kqfd); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2070 | } | 
 | 2071 |  | 
 | 2072 | PyDoc_STRVAR(kqueue_queue_fileno_doc, | 
 | 2073 | "fileno() -> int\n\ | 
 | 2074 | \n\ | 
 | 2075 | Return the kqueue control file descriptor."); | 
 | 2076 |  | 
 | 2077 | static PyObject* | 
 | 2078 | kqueue_queue_fromfd(PyObject *cls, PyObject *args) | 
 | 2079 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 |     SOCKET fd; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2081 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 |     if (!PyArg_ParseTuple(args, "i:fromfd", &fd)) | 
 | 2083 |         return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2084 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 |     return newKqueue_Object((PyTypeObject*)cls, fd); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2086 | } | 
 | 2087 |  | 
 | 2088 | PyDoc_STRVAR(kqueue_queue_fromfd_doc, | 
 | 2089 | "fromfd(fd) -> kqueue\n\ | 
 | 2090 | \n\ | 
 | 2091 | Create a kqueue object from a given control fd."); | 
 | 2092 |  | 
 | 2093 | static PyObject * | 
 | 2094 | kqueue_queue_control(kqueue_queue_Object *self, PyObject *args) | 
 | 2095 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 |     int nevents = 0; | 
 | 2097 |     int gotevents = 0; | 
 | 2098 |     int nchanges = 0; | 
 | 2099 |     int i = 0; | 
 | 2100 |     PyObject *otimeout = NULL; | 
 | 2101 |     PyObject *ch = NULL; | 
 | 2102 |     PyObject *it = NULL, *ei = NULL; | 
 | 2103 |     PyObject *result = NULL; | 
 | 2104 |     struct kevent *evl = NULL; | 
 | 2105 |     struct kevent *chl = NULL; | 
| Victor Stinner | 4448c08 | 2015-03-31 11:48:34 +0200 | [diff] [blame] | 2106 |     struct timespec timeoutspec; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 |     struct timespec *ptimeoutspec; | 
| Victor Stinner | 4448c08 | 2015-03-31 11:48:34 +0200 | [diff] [blame] | 2108 |     _PyTime_t timeout, deadline = 0; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2109 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2110 |     if (self->kqfd < 0) | 
 | 2111 |         return kqueue_queue_err_closed(); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2112 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2113 |     if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout)) | 
 | 2114 |         return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2115 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 |     if (nevents < 0) { | 
 | 2117 |         PyErr_Format(PyExc_ValueError, | 
 | 2118 |             "Length of eventlist must be 0 or positive, got %d", | 
 | 2119 |             nevents); | 
 | 2120 |         return NULL; | 
 | 2121 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2122 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 |     if (otimeout == Py_None || otimeout == NULL) { | 
 | 2124 |         ptimeoutspec = NULL; | 
 | 2125 |     } | 
| Victor Stinner | c337838 | 2015-03-28 05:07:51 +0100 | [diff] [blame] | 2126 |     else { | 
| Victor Stinner | 4448c08 | 2015-03-31 11:48:34 +0200 | [diff] [blame] | 2127 |         if (_PyTime_FromSecondsObject(&timeout, | 
| Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 2128 |                                       otimeout, _PyTime_ROUND_CEILING) < 0) { | 
| Victor Stinner | c337838 | 2015-03-28 05:07:51 +0100 | [diff] [blame] | 2129 |             PyErr_Format(PyExc_TypeError, | 
 | 2130 |                 "timeout argument must be an number " | 
 | 2131 |                 "or None, got %.200s", | 
 | 2132 |                 Py_TYPE(otimeout)->tp_name); | 
 | 2133 |             return NULL; | 
 | 2134 |         } | 
 | 2135 |  | 
| Victor Stinner | 4448c08 | 2015-03-31 11:48:34 +0200 | [diff] [blame] | 2136 |         if (_PyTime_AsTimespec(timeout, &timeoutspec) == -1) | 
| Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2137 |             return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2138 |  | 
| Victor Stinner | 4448c08 | 2015-03-31 11:48:34 +0200 | [diff] [blame] | 2139 |         if (timeoutspec.tv_sec < 0) { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 |             PyErr_SetString(PyExc_ValueError, | 
 | 2141 |                             "timeout must be positive or None"); | 
 | 2142 |             return NULL; | 
 | 2143 |         } | 
| Victor Stinner | 4448c08 | 2015-03-31 11:48:34 +0200 | [diff] [blame] | 2144 |         ptimeoutspec = &timeoutspec; | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2146 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2147 |     if (ch != NULL && ch != Py_None) { | 
 | 2148 |         it = PyObject_GetIter(ch); | 
 | 2149 |         if (it == NULL) { | 
 | 2150 |             PyErr_SetString(PyExc_TypeError, | 
 | 2151 |                             "changelist is not iterable"); | 
 | 2152 |             return NULL; | 
 | 2153 |         } | 
 | 2154 |         nchanges = PyObject_Size(ch); | 
 | 2155 |         if (nchanges < 0) { | 
 | 2156 |             goto error; | 
 | 2157 |         } | 
| Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 2158 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 |         chl = PyMem_New(struct kevent, nchanges); | 
 | 2160 |         if (chl == NULL) { | 
 | 2161 |             PyErr_NoMemory(); | 
 | 2162 |             goto error; | 
 | 2163 |         } | 
 | 2164 |         i = 0; | 
 | 2165 |         while ((ei = PyIter_Next(it)) != NULL) { | 
 | 2166 |             if (!kqueue_event_Check(ei)) { | 
 | 2167 |                 Py_DECREF(ei); | 
 | 2168 |                 PyErr_SetString(PyExc_TypeError, | 
 | 2169 |                     "changelist must be an iterable of " | 
 | 2170 |                     "select.kevent objects"); | 
 | 2171 |                 goto error; | 
 | 2172 |             } else { | 
 | 2173 |                 chl[i++] = ((kqueue_event_Object *)ei)->e; | 
 | 2174 |             } | 
 | 2175 |             Py_DECREF(ei); | 
 | 2176 |         } | 
 | 2177 |     } | 
 | 2178 |     Py_CLEAR(it); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2179 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2180 |     /* event list */ | 
 | 2181 |     if (nevents) { | 
 | 2182 |         evl = PyMem_New(struct kevent, nevents); | 
 | 2183 |         if (evl == NULL) { | 
 | 2184 |             PyErr_NoMemory(); | 
 | 2185 |             goto error; | 
 | 2186 |         } | 
 | 2187 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2188 |  | 
| Victor Stinner | 4448c08 | 2015-03-31 11:48:34 +0200 | [diff] [blame] | 2189 |     if (ptimeoutspec) | 
 | 2190 |         deadline = _PyTime_GetMonotonicClock() + timeout; | 
 | 2191 |  | 
 | 2192 |     do { | 
 | 2193 |         Py_BEGIN_ALLOW_THREADS | 
 | 2194 |         errno = 0; | 
 | 2195 |         gotevents = kevent(self->kqfd, chl, nchanges, | 
 | 2196 |                            evl, nevents, ptimeoutspec); | 
 | 2197 |         Py_END_ALLOW_THREADS | 
 | 2198 |  | 
 | 2199 |         if (errno != EINTR) | 
 | 2200 |             break; | 
 | 2201 |  | 
 | 2202 |         /* kevent() was interrupted by a signal */ | 
 | 2203 |         if (PyErr_CheckSignals()) | 
 | 2204 |             goto error; | 
 | 2205 |  | 
 | 2206 |         if (ptimeoutspec) { | 
 | 2207 |             timeout = deadline - _PyTime_GetMonotonicClock(); | 
 | 2208 |             if (timeout < 0) { | 
 | 2209 |                 gotevents = 0; | 
 | 2210 |                 break; | 
 | 2211 |             } | 
 | 2212 |             if (_PyTime_AsTimespec(timeout, &timeoutspec) == -1) | 
 | 2213 |                 goto error; | 
 | 2214 |             /* retry kevent() with the recomputed timeout */ | 
 | 2215 |         } | 
 | 2216 |     } while (1); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2217 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 |     if (gotevents == -1) { | 
 | 2219 |         PyErr_SetFromErrno(PyExc_OSError); | 
 | 2220 |         goto error; | 
 | 2221 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2222 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 |     result = PyList_New(gotevents); | 
 | 2224 |     if (result == NULL) { | 
 | 2225 |         goto error; | 
 | 2226 |     } | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2227 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 |     for (i = 0; i < gotevents; i++) { | 
 | 2229 |         kqueue_event_Object *ch; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2230 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2231 |         ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type); | 
 | 2232 |         if (ch == NULL) { | 
 | 2233 |             goto error; | 
 | 2234 |         } | 
 | 2235 |         ch->e = evl[i]; | 
 | 2236 |         PyList_SET_ITEM(result, i, (PyObject *)ch); | 
 | 2237 |     } | 
 | 2238 |     PyMem_Free(chl); | 
 | 2239 |     PyMem_Free(evl); | 
 | 2240 |     return result; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2241 |  | 
 | 2242 |     error: | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2243 |     PyMem_Free(chl); | 
 | 2244 |     PyMem_Free(evl); | 
 | 2245 |     Py_XDECREF(result); | 
 | 2246 |     Py_XDECREF(it); | 
 | 2247 |     return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2248 | } | 
 | 2249 |  | 
 | 2250 | PyDoc_STRVAR(kqueue_queue_control_doc, | 
| Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 2251 | "control(changelist, max_events[, timeout=None]) -> eventlist\n\ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2252 | \n\ | 
 | 2253 | Calls the kernel kevent function.\n\ | 
 | 2254 | - changelist must be a list of kevent objects describing the changes\n\ | 
 | 2255 |   to be made to the kernel's watch list or None.\n\ | 
 | 2256 | - max_events lets you specify the maximum number of events that the\n\ | 
 | 2257 |   kernel will return.\n\ | 
 | 2258 | - timeout is the maximum time to wait in seconds, or else None,\n\ | 
 | 2259 |   to wait forever. timeout accepts floats for smaller timeouts, too."); | 
 | 2260 |  | 
 | 2261 |  | 
 | 2262 | static PyMethodDef kqueue_queue_methods[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 |     {"fromfd",          (PyCFunction)kqueue_queue_fromfd, | 
 | 2264 |      METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc}, | 
 | 2265 |     {"close",           (PyCFunction)kqueue_queue_close,        METH_NOARGS, | 
 | 2266 |      kqueue_queue_close_doc}, | 
 | 2267 |     {"fileno",          (PyCFunction)kqueue_queue_fileno,       METH_NOARGS, | 
 | 2268 |      kqueue_queue_fileno_doc}, | 
 | 2269 |     {"control",         (PyCFunction)kqueue_queue_control, | 
 | 2270 |      METH_VARARGS ,     kqueue_queue_control_doc}, | 
 | 2271 |     {NULL,      NULL}, | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2272 | }; | 
 | 2273 |  | 
 | 2274 | static PyGetSetDef kqueue_queue_getsetlist[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 |     {"closed", (getter)kqueue_queue_get_closed, NULL, | 
 | 2276 |      "True if the kqueue handler is closed"}, | 
 | 2277 |     {0}, | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2278 | }; | 
 | 2279 |  | 
 | 2280 | PyDoc_STRVAR(kqueue_queue_doc, | 
 | 2281 | "Kqueue syscall wrapper.\n\ | 
 | 2282 | \n\ | 
 | 2283 | For example, to start watching a socket for input:\n\ | 
 | 2284 | >>> kq = kqueue()\n\ | 
 | 2285 | >>> sock = socket()\n\ | 
 | 2286 | >>> sock.connect((host, port))\n\ | 
 | 2287 | >>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\ | 
 | 2288 | \n\ | 
 | 2289 | To wait one second for it to become writeable:\n\ | 
 | 2290 | >>> kq.control(None, 1, 1000)\n\ | 
 | 2291 | \n\ | 
 | 2292 | To stop listening:\n\ | 
 | 2293 | >>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)"); | 
 | 2294 |  | 
 | 2295 | static PyTypeObject kqueue_queue_Type = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 |     PyVarObject_HEAD_INIT(NULL, 0) | 
 | 2297 |     "select.kqueue",                                    /* tp_name */ | 
 | 2298 |     sizeof(kqueue_queue_Object),                        /* tp_basicsize */ | 
 | 2299 |     0,                                                  /* tp_itemsize */ | 
 | 2300 |     (destructor)kqueue_queue_dealloc,                   /* tp_dealloc */ | 
 | 2301 |     0,                                                  /* tp_print */ | 
 | 2302 |     0,                                                  /* tp_getattr */ | 
 | 2303 |     0,                                                  /* tp_setattr */ | 
 | 2304 |     0,                                                  /* tp_reserved */ | 
 | 2305 |     0,                                                  /* tp_repr */ | 
 | 2306 |     0,                                                  /* tp_as_number */ | 
 | 2307 |     0,                                                  /* tp_as_sequence */ | 
 | 2308 |     0,                                                  /* tp_as_mapping */ | 
 | 2309 |     0,                                                  /* tp_hash */ | 
 | 2310 |     0,                                                  /* tp_call */ | 
 | 2311 |     0,                                                  /* tp_str */ | 
 | 2312 |     0,                                                  /* tp_getattro */ | 
 | 2313 |     0,                                                  /* tp_setattro */ | 
 | 2314 |     0,                                                  /* tp_as_buffer */ | 
 | 2315 |     Py_TPFLAGS_DEFAULT,                                 /* tp_flags */ | 
 | 2316 |     kqueue_queue_doc,                                   /* tp_doc */ | 
 | 2317 |     0,                                                  /* tp_traverse */ | 
 | 2318 |     0,                                                  /* tp_clear */ | 
 | 2319 |     0,                                                  /* tp_richcompare */ | 
 | 2320 |     0,                                                  /* tp_weaklistoffset */ | 
 | 2321 |     0,                                                  /* tp_iter */ | 
 | 2322 |     0,                                                  /* tp_iternext */ | 
 | 2323 |     kqueue_queue_methods,                               /* tp_methods */ | 
 | 2324 |     0,                                                  /* tp_members */ | 
 | 2325 |     kqueue_queue_getsetlist,                            /* tp_getset */ | 
 | 2326 |     0,                                                  /* tp_base */ | 
 | 2327 |     0,                                                  /* tp_dict */ | 
 | 2328 |     0,                                                  /* tp_descr_get */ | 
 | 2329 |     0,                                                  /* tp_descr_set */ | 
 | 2330 |     0,                                                  /* tp_dictoffset */ | 
 | 2331 |     0,                                                  /* tp_init */ | 
 | 2332 |     0,                                                  /* tp_alloc */ | 
 | 2333 |     kqueue_queue_new,                                   /* tp_new */ | 
 | 2334 |     0,                                                  /* tp_free */ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2335 | }; | 
 | 2336 |  | 
 | 2337 | #endif /* HAVE_KQUEUE */ | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 2338 |  | 
 | 2339 |  | 
 | 2340 |  | 
 | 2341 |  | 
 | 2342 |  | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2343 | /* ************************************************************************ */ | 
 | 2344 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2345 | PyDoc_STRVAR(select_doc, | 
| Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 2346 | "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\ | 
 | 2347 | \n\ | 
 | 2348 | 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] | 2349 | 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] | 2350 | rlist -- wait until ready for reading\n\ | 
 | 2351 | wlist -- wait until ready for writing\n\ | 
 | 2352 | xlist -- wait for an ``exceptional condition''\n\ | 
 | 2353 | If only one kind of condition is required, pass [] for the other lists.\n\ | 
 | 2354 | A file descriptor is either a socket or file object, or a small integer\n\ | 
 | 2355 | gotten from a fileno() method call on one of those.\n\ | 
 | 2356 | \n\ | 
 | 2357 | The optional 4th argument specifies a timeout in seconds; it may be\n\ | 
 | 2358 | a floating point number to specify fractions of seconds.  If it is absent\n\ | 
 | 2359 | or None, the call will never time out.\n\ | 
 | 2360 | \n\ | 
 | 2361 | The return value is a tuple of three lists corresponding to the first three\n\ | 
 | 2362 | arguments; each contains the subset of the corresponding file descriptors\n\ | 
 | 2363 | that are ready.\n\ | 
 | 2364 | \n\ | 
 | 2365 | *** IMPORTANT NOTICE ***\n\ | 
| Benjamin Peterson | b397e3b | 2015-10-10 19:32:20 -0700 | [diff] [blame] | 2366 | On Windows, only sockets are supported; on Unix, all file\n\ | 
| Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 2367 | descriptors can be used."); | 
| Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 2368 |  | 
| Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 2369 | static PyMethodDef select_methods[] = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 |     {"select",          select_select,  METH_VARARGS,   select_doc}, | 
| Charles-François Natali | 986a56c | 2013-01-19 12:19:10 +0100 | [diff] [blame] | 2371 | #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL) | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2372 |     {"poll",            select_poll,    METH_NOARGS,    poll_doc}, | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2373 | #endif /* HAVE_POLL */ | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 2374 | #ifdef HAVE_SYS_DEVPOLL_H | 
 | 2375 |     {"devpoll",         select_devpoll, METH_NOARGS,    devpoll_doc}, | 
 | 2376 | #endif | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2377 |     {0,         0},     /* sentinel */ | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2378 | }; | 
 | 2379 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2380 | PyDoc_STRVAR(module_doc, | 
| Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 2381 | "This module supports asynchronous I/O on multiple file descriptors.\n\ | 
 | 2382 | \n\ | 
 | 2383 | *** IMPORTANT NOTICE ***\n\ | 
| Benjamin Peterson | b397e3b | 2015-10-10 19:32:20 -0700 | [diff] [blame] | 2384 | On Windows, only sockets are supported; on Unix, all file descriptors."); | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2385 |  | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2386 |  | 
 | 2387 | static struct PyModuleDef selectmodule = { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 |     PyModuleDef_HEAD_INIT, | 
 | 2389 |     "select", | 
 | 2390 |     module_doc, | 
 | 2391 |     -1, | 
 | 2392 |     select_methods, | 
 | 2393 |     NULL, | 
 | 2394 |     NULL, | 
 | 2395 |     NULL, | 
 | 2396 |     NULL | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2397 | }; | 
 | 2398 |  | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 2399 |  | 
 | 2400 |  | 
 | 2401 |  | 
| Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 2402 | PyMODINIT_FUNC | 
| Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2403 | PyInit_select(void) | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2404 | { | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2405 |     PyObject *m; | 
 | 2406 |     m = PyModule_Create(&selectmodule); | 
 | 2407 |     if (m == NULL) | 
 | 2408 |         return NULL; | 
| Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 2409 |  | 
| Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2410 |     Py_INCREF(PyExc_OSError); | 
 | 2411 |     PyModule_AddObject(m, "error", PyExc_OSError); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2412 |  | 
| Amaury Forgeot d'Arc | ace3102 | 2009-07-09 22:44:11 +0000 | [diff] [blame] | 2413 | #ifdef PIPE_BUF | 
| R. David Murray | e16cda9 | 2010-10-15 23:12:57 +0000 | [diff] [blame] | 2414 | #ifdef HAVE_BROKEN_PIPE_BUF | 
 | 2415 | #undef PIPE_BUF | 
 | 2416 | #define PIPE_BUF 512 | 
 | 2417 | #endif | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2418 |     PyModule_AddIntMacro(m, PIPE_BUF); | 
| Amaury Forgeot d'Arc | ace3102 | 2009-07-09 22:44:11 +0000 | [diff] [blame] | 2419 | #endif | 
| Gregory P. Smith | b970b86 | 2009-07-04 02:28:47 +0000 | [diff] [blame] | 2420 |  | 
| Charles-François Natali | 986a56c | 2013-01-19 12:19:10 +0100 | [diff] [blame] | 2421 | #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2422 | #ifdef __APPLE__ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2423 |     if (select_have_broken_poll()) { | 
 | 2424 |         if (PyObject_DelAttrString(m, "poll") == -1) { | 
 | 2425 |             PyErr_Clear(); | 
 | 2426 |         } | 
 | 2427 |     } else { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2428 | #else | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2429 |     { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2430 | #endif | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 |         if (PyType_Ready(&poll_Type) < 0) | 
 | 2432 |             return NULL; | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2433 |         PyModule_AddIntMacro(m, POLLIN); | 
 | 2434 |         PyModule_AddIntMacro(m, POLLPRI); | 
 | 2435 |         PyModule_AddIntMacro(m, POLLOUT); | 
 | 2436 |         PyModule_AddIntMacro(m, POLLERR); | 
 | 2437 |         PyModule_AddIntMacro(m, POLLHUP); | 
 | 2438 |         PyModule_AddIntMacro(m, POLLNVAL); | 
| Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 2439 |  | 
| Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 2440 | #ifdef POLLRDNORM | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2441 |         PyModule_AddIntMacro(m, POLLRDNORM); | 
| Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 2442 | #endif | 
 | 2443 | #ifdef POLLRDBAND | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2444 |         PyModule_AddIntMacro(m, POLLRDBAND); | 
| Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 2445 | #endif | 
 | 2446 | #ifdef POLLWRNORM | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2447 |         PyModule_AddIntMacro(m, POLLWRNORM); | 
| Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 2448 | #endif | 
 | 2449 | #ifdef POLLWRBAND | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2450 |         PyModule_AddIntMacro(m, POLLWRBAND); | 
| Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 2451 | #endif | 
| Sjoerd Mullender | 239f836 | 2000-08-25 13:59:18 +0000 | [diff] [blame] | 2452 | #ifdef POLLMSG | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2453 |         PyModule_AddIntMacro(m, POLLMSG); | 
| Sjoerd Mullender | 239f836 | 2000-08-25 13:59:18 +0000 | [diff] [blame] | 2454 | #endif | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2455 |     } | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2456 | #endif /* HAVE_POLL */ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2457 |  | 
| Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 2458 | #ifdef HAVE_SYS_DEVPOLL_H | 
 | 2459 |     if (PyType_Ready(&devpoll_Type) < 0) | 
 | 2460 |         return NULL; | 
 | 2461 | #endif | 
 | 2462 |  | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2463 | #ifdef HAVE_EPOLL | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2464 |     Py_TYPE(&pyEpoll_Type) = &PyType_Type; | 
 | 2465 |     if (PyType_Ready(&pyEpoll_Type) < 0) | 
 | 2466 |         return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2467 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2468 |     Py_INCREF(&pyEpoll_Type); | 
 | 2469 |     PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2470 |  | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2471 |     PyModule_AddIntMacro(m, EPOLLIN); | 
 | 2472 |     PyModule_AddIntMacro(m, EPOLLOUT); | 
 | 2473 |     PyModule_AddIntMacro(m, EPOLLPRI); | 
 | 2474 |     PyModule_AddIntMacro(m, EPOLLERR); | 
 | 2475 |     PyModule_AddIntMacro(m, EPOLLHUP); | 
 | 2476 |     PyModule_AddIntMacro(m, EPOLLET); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2477 | #ifdef EPOLLONESHOT | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2478 |     /* Kernel 2.6.2+ */ | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2479 |     PyModule_AddIntMacro(m, EPOLLONESHOT); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2480 | #endif | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2481 |     /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */ | 
| Zachary Ware | 3e77677 | 2015-08-01 21:34:05 -0500 | [diff] [blame] | 2482 |  | 
 | 2483 | #ifdef EPOLLRDNORM | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2484 |     PyModule_AddIntMacro(m, EPOLLRDNORM); | 
| Zachary Ware | 3e77677 | 2015-08-01 21:34:05 -0500 | [diff] [blame] | 2485 | #endif | 
 | 2486 | #ifdef EPOLLRDBAND | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2487 |     PyModule_AddIntMacro(m, EPOLLRDBAND); | 
| Zachary Ware | 3e77677 | 2015-08-01 21:34:05 -0500 | [diff] [blame] | 2488 | #endif | 
 | 2489 | #ifdef EPOLLWRNORM | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2490 |     PyModule_AddIntMacro(m, EPOLLWRNORM); | 
| Zachary Ware | 3e77677 | 2015-08-01 21:34:05 -0500 | [diff] [blame] | 2491 | #endif | 
 | 2492 | #ifdef EPOLLWRBAND | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2493 |     PyModule_AddIntMacro(m, EPOLLWRBAND); | 
| Zachary Ware | 3e77677 | 2015-08-01 21:34:05 -0500 | [diff] [blame] | 2494 | #endif | 
 | 2495 | #ifdef EPOLLMSG | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2496 |     PyModule_AddIntMacro(m, EPOLLMSG); | 
| Zachary Ware | 3e77677 | 2015-08-01 21:34:05 -0500 | [diff] [blame] | 2497 | #endif | 
| Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 2498 |  | 
| Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 2499 | #ifdef EPOLL_CLOEXEC | 
| Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2500 |     PyModule_AddIntMacro(m, EPOLL_CLOEXEC); | 
| Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 2501 | #endif | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2502 | #endif /* HAVE_EPOLL */ | 
 | 2503 |  | 
 | 2504 | #ifdef HAVE_KQUEUE | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2505 |     kqueue_event_Type.tp_new = PyType_GenericNew; | 
 | 2506 |     Py_TYPE(&kqueue_event_Type) = &PyType_Type; | 
 | 2507 |     if(PyType_Ready(&kqueue_event_Type) < 0) | 
 | 2508 |         return NULL; | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2509 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2510 |     Py_INCREF(&kqueue_event_Type); | 
 | 2511 |     PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2512 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2513 |     Py_TYPE(&kqueue_queue_Type) = &PyType_Type; | 
 | 2514 |     if(PyType_Ready(&kqueue_queue_Type) < 0) | 
 | 2515 |         return NULL; | 
 | 2516 |     Py_INCREF(&kqueue_queue_Type); | 
 | 2517 |     PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type); | 
 | 2518 |  | 
 | 2519 |     /* event filters */ | 
 | 2520 |     PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ); | 
 | 2521 |     PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE); | 
 | 2522 |     PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO); | 
 | 2523 |     PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE); | 
 | 2524 |     PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2525 | #ifdef EVFILT_NETDEV | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 |     PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2527 | #endif | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 |     PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL); | 
 | 2529 |     PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2530 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2531 |     /* event flags */ | 
 | 2532 |     PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD); | 
 | 2533 |     PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE); | 
 | 2534 |     PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE); | 
 | 2535 |     PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE); | 
 | 2536 |     PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT); | 
 | 2537 |     PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2538 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 |     PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS); | 
 | 2540 |     PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2541 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2542 |     PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF); | 
 | 2543 |     PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2544 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 |     /* READ WRITE filter flag */ | 
 | 2546 |     PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2547 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2548 |     /* VNODE filter flags  */ | 
 | 2549 |     PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE); | 
 | 2550 |     PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE); | 
 | 2551 |     PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND); | 
 | 2552 |     PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB); | 
 | 2553 |     PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK); | 
 | 2554 |     PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME); | 
 | 2555 |     PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2556 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2557 |     /* PROC filter flags  */ | 
 | 2558 |     PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT); | 
 | 2559 |     PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK); | 
 | 2560 |     PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC); | 
 | 2561 |     PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK); | 
 | 2562 |     PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2563 |  | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2564 |     PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK); | 
 | 2565 |     PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD); | 
 | 2566 |     PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR); | 
 | 2567 |  | 
 | 2568 |     /* NETDEV filter flags */ | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2569 | #ifdef EVFILT_NETDEV | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2570 |     PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP); | 
 | 2571 |     PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN); | 
 | 2572 |     PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV); | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2573 | #endif | 
 | 2574 |  | 
 | 2575 | #endif /* HAVE_KQUEUE */ | 
| Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2576 |     return m; | 
| Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2577 | } |