Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(13)

Delta Between Two Patch Sets: Modules/selectmodule.c

Issue 3871: cross and native build of python for mingw32 with distutils
Left Patch Set: Created 9 years, 5 months ago
Right Patch Set: Created 7 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Modules/readline.c ('k') | Modules/Setup.config.in » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* select - Module containing unix select(2) call. 1 /* select - Module containing unix select(2) call.
2 Under Unix, the file descriptors are small integers. 2 Under Unix, the file descriptors are small integers.
3 Under Win32, select only exists for sockets, and sockets may 3 Under Win32, select only exists for sockets, and sockets may
4 have any value except INVALID_SOCKET. 4 have any value except INVALID_SOCKET.
5 */ 5 */
6 6
7 #include "Python.h" 7 #include "Python.h"
8 #include <structmember.h> 8 #include <structmember.h>
9
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
9 17
10 #ifdef __APPLE__ 18 #ifdef __APPLE__
11 /* Perform runtime testing for a broken poll on OSX to make it easier 19 /* Perform runtime testing for a broken poll on OSX to make it easier
12 * to use the same binary on multiple releases of the OS. 20 * to use the same binary on multiple releases of the OS.
13 */ 21 */
14 #undef HAVE_BROKEN_POLL 22 #undef HAVE_BROKEN_POLL
15 #endif 23 #endif
16 24
17 /* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined. 25 /* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.
18 64 is too small (too many people have bumped into that limit). 26 64 is too small (too many people have bumped into that limit).
(...skipping 28 matching lines...) Expand all
47 #ifdef MS_WINDOWS 55 #ifdef MS_WINDOWS
48 # define WIN32_LEAN_AND_MEAN 56 # define WIN32_LEAN_AND_MEAN
49 # include <winsock.h> 57 # include <winsock.h>
50 #else 58 #else
51 # define SOCKET int 59 # define SOCKET int
52 # if defined(__VMS) 60 # if defined(__VMS)
53 # include <socket.h> 61 # include <socket.h>
54 # endif 62 # endif
55 #endif 63 #endif
56 64
57 static PyObject *SelectError;
58
59 /* list of Python objects and their file descriptor */ 65 /* list of Python objects and their file descriptor */
60 typedef struct { 66 typedef struct {
61 PyObject *obj; /* owned reference */ 67 PyObject *obj; /* owned reference */
62 SOCKET fd; 68 SOCKET fd;
63 int sentinel; /* -1 == sentinel */ 69 int sentinel; /* -1 == sentinel */
64 } pylist; 70 } pylist;
65 71
66 static void 72 static void
67 reap_obj(pylist fd2obj[FD_SETSIZE + 1]) 73 reap_obj(pylist fd2obj[FD_SETSIZE + 1])
68 { 74 {
69 int i; 75 int i;
70 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) { 76 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
71 Py_XDECREF(fd2obj[i].obj); 77 Py_XDECREF(fd2obj[i].obj);
72 fd2obj[i].obj = NULL; 78 fd2obj[i].obj = NULL;
73 } 79 }
74 fd2obj[0].sentinel = -1; 80 fd2obj[0].sentinel = -1;
75 } 81 }
76 82
77 83
78 /* returns -1 and sets the Python exception if an error occurred, otherwise 84 /* returns -1 and sets the Python exception if an error occurred, otherwise
79 returns a number >= 0 85 returns a number >= 0
80 */ 86 */
81 static int 87 static int
82 seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1]) 88 seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
83 { 89 {
84 int i;
85 int max = -1; 90 int max = -1;
86 int index = 0; 91 int index = 0;
87 int len = -1; 92 Py_ssize_t i;
88 PyObject* fast_seq = NULL; 93 PyObject* fast_seq = NULL;
89 PyObject* o = NULL; 94 PyObject* o = NULL;
90 95
91 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */ 96 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
92 FD_ZERO(set); 97 FD_ZERO(set);
93 98
94 fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences"); 99 fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences");
95 if (!fast_seq) 100 if (!fast_seq)
96 return -1; 101 return -1;
97 102
98 len = PySequence_Fast_GET_SIZE(fast_seq); 103 for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++) {
99
100 for (i = 0; i < len; i++) {
101 SOCKET v; 104 SOCKET v;
102 105
103 /* any intervening fileno() calls could decr this refcnt */ 106 /* any intervening fileno() calls could decr this refcnt */
104 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i))) 107 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
105 return -1; 108 goto finally;
106 109
107 Py_INCREF(o); 110 Py_INCREF(o);
108 v = PyObject_AsFileDescriptor( o ); 111 v = PyObject_AsFileDescriptor( o );
109 if (v == -1) goto finally; 112 if (v == -1) goto finally;
110 113
111 #if defined(MS_WINDOWS) 114 #if defined(MS_WIN32)
112 max = 0; /* not used for Win32 */ 115 max = 0; /* not used for Win32 */
113 #else /* !_MSC_VER */ 116 #else /* !MS_WIN32 */
114 if (v < 0 || v >= FD_SETSIZE) { 117 if (!_PyIsSelectable_fd(v)) {
115 PyErr_SetString(PyExc_ValueError, 118 PyErr_SetString(PyExc_ValueError,
116 "filedescriptor out of range in select()"); 119 "filedescriptor out of range in select()");
117 goto finally; 120 goto finally;
118 } 121 }
119 if (v > max) 122 if (v > max)
120 max = v; 123 max = v;
121 #endif /* _MSC_VER */ 124 #endif /* MS_WIN32 */
122 FD_SET(v, set); 125 FD_SET(v, set);
123 126
124 /* add object and its file descriptor to the list */ 127 /* add object and its file descriptor to the list */
125 if (index >= FD_SETSIZE) { 128 if (index >= FD_SETSIZE) {
126 PyErr_SetString(PyExc_ValueError, 129 PyErr_SetString(PyExc_ValueError,
127 "too many file descriptors in select()"); 130 "too many file descriptors in select()");
128 goto finally; 131 goto finally;
129 } 132 }
130 fd2obj[index].obj = o; 133 fd2obj[index].obj = o;
131 fd2obj[index].fd = v; 134 fd2obj[index].fd = v;
(...skipping 22 matching lines...) Expand all
154 count++; 157 count++;
155 } 158 }
156 list = PyList_New(count); 159 list = PyList_New(count);
157 if (!list) 160 if (!list)
158 return NULL; 161 return NULL;
159 162
160 i = 0; 163 i = 0;
161 for (j = 0; fd2obj[j].sentinel >= 0; j++) { 164 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
162 fd = fd2obj[j].fd; 165 fd = fd2obj[j].fd;
163 if (FD_ISSET(fd, set)) { 166 if (FD_ISSET(fd, set)) {
164 #ifndef MS_WINDOWS
165 if (fd > FD_SETSIZE) {
166 PyErr_SetString(PyExc_SystemError,
167 "filedescriptor out of range returned in select()");
168 goto finally;
169 }
170 #endif
171 o = fd2obj[j].obj; 167 o = fd2obj[j].obj;
172 fd2obj[j].obj = NULL; 168 fd2obj[j].obj = NULL;
173 /* transfer ownership */ 169 /* transfer ownership */
174 if (PyList_SetItem(list, i, o) < 0) 170 if (PyList_SetItem(list, i, o) < 0)
175 goto finally; 171 goto finally;
176 172
177 i++; 173 i++;
178 } 174 }
179 } 175 }
180 return list; 176 return list;
(...skipping 20 matching lines...) Expand all
201 * See: Stevens, APitUE, $12.5.1 197 * See: Stevens, APitUE, $12.5.1
202 */ 198 */
203 pylist rfd2obj[FD_SETSIZE + 1]; 199 pylist rfd2obj[FD_SETSIZE + 1];
204 pylist wfd2obj[FD_SETSIZE + 1]; 200 pylist wfd2obj[FD_SETSIZE + 1];
205 pylist efd2obj[FD_SETSIZE + 1]; 201 pylist efd2obj[FD_SETSIZE + 1];
206 #endif /* SELECT_USES_HEAP */ 202 #endif /* SELECT_USES_HEAP */
207 PyObject *ifdlist, *ofdlist, *efdlist; 203 PyObject *ifdlist, *ofdlist, *efdlist;
208 PyObject *ret = NULL; 204 PyObject *ret = NULL;
209 PyObject *tout = Py_None; 205 PyObject *tout = Py_None;
210 fd_set ifdset, ofdset, efdset; 206 fd_set ifdset, ofdset, efdset;
211 double timeout;
212 struct timeval tv, *tvp; 207 struct timeval tv, *tvp;
213 long seconds;
214 int imax, omax, emax, max; 208 int imax, omax, emax, max;
215 int n; 209 int n;
216 210
217 /* convert arguments */ 211 /* convert arguments */
218 if (!PyArg_UnpackTuple(args, "select", 3, 4, 212 if (!PyArg_UnpackTuple(args, "select", 3, 4,
219 &ifdlist, &ofdlist, &efdlist, &tout)) 213 &ifdlist, &ofdlist, &efdlist, &tout))
220 return NULL; 214 return NULL;
221 215
222 if (tout == Py_None) 216 if (tout == Py_None)
223 tvp = (struct timeval *)0; 217 tvp = (struct timeval *)0;
224 else if (!PyNumber_Check(tout)) { 218 else if (!PyNumber_Check(tout)) {
225 PyErr_SetString(PyExc_TypeError, 219 PyErr_SetString(PyExc_TypeError,
226 "timeout must be a float or None"); 220 "timeout must be a float or None");
227 return NULL; 221 return NULL;
228 } 222 }
229 else { 223 else {
230 timeout = PyFloat_AsDouble(tout); 224 #ifdef MS_WINDOWS
231 if (timeout == -1 && PyErr_Occurred()) 225 time_t sec;
226 if (_PyTime_ObjectToTimeval(tout, &sec, &tv.tv_usec) == -1)
232 return NULL; 227 return NULL;
233 if (timeout > (double)LONG_MAX) { 228 assert(sizeof(tv.tv_sec) == sizeof(long));
229 #if SIZEOF_TIME_T > SIZEOF_LONG
230 if (sec > LONG_MAX) {
234 PyErr_SetString(PyExc_OverflowError, 231 PyErr_SetString(PyExc_OverflowError,
235 "timeout period too long"); 232 "timeout is too large");
236 return NULL; 233 return NULL;
237 } 234 }
238 seconds = (long)timeout; 235 #endif
239 timeout = timeout - (double)seconds; 236 tv.tv_sec = (long)sec;
240 tv.tv_sec = seconds; 237 #else
241 tv.tv_usec = (long)(timeout * 1E6); 238 /* 64-bit OS X has struct timeval.tv_usec as an int (and thus still 4
239 bytes as required), but no longer defined by a long. */
240 long tv_usec;
241 if (_PyTime_ObjectToTimeval(tout, &tv.tv_sec, &tv_usec) == -1)
242 return NULL;
243 tv.tv_usec = tv_usec;
244 #endif
245 if (tv.tv_sec < 0) {
246 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
247 return NULL;
248 }
242 tvp = &tv; 249 tvp = &tv;
243 } 250 }
244 251
245 252
246 #ifdef SELECT_USES_HEAP 253 #ifdef SELECT_USES_HEAP
247 /* Allocate memory for the lists */ 254 /* Allocate memory for the lists */
248 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1); 255 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
249 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1); 256 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
250 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1); 257 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
251 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) { 258 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
(...skipping 18 matching lines...) Expand all
270 max = imax; 277 max = imax;
271 if (omax > max) max = omax; 278 if (omax > max) max = omax;
272 if (emax > max) max = emax; 279 if (emax > max) max = emax;
273 280
274 Py_BEGIN_ALLOW_THREADS 281 Py_BEGIN_ALLOW_THREADS
275 n = select(max, &ifdset, &ofdset, &efdset, tvp); 282 n = select(max, &ifdset, &ofdset, &efdset, tvp);
276 Py_END_ALLOW_THREADS 283 Py_END_ALLOW_THREADS
277 284
278 #ifdef MS_WINDOWS 285 #ifdef MS_WINDOWS
279 if (n == SOCKET_ERROR) { 286 if (n == SOCKET_ERROR) {
280 PyErr_SetExcFromWindowsErr(SelectError, WSAGetLastError()); 287 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
281 } 288 }
282 #else 289 #else
283 if (n < 0) { 290 if (n < 0) {
284 PyErr_SetFromErrno(SelectError); 291 PyErr_SetFromErrno(PyExc_OSError);
285 } 292 }
286 #endif 293 #endif
287 else { 294 else {
288 /* any of these three calls can raise an exception. it's more 295 /* any of these three calls can raise an exception. it's more
289 convenient to test for this after all three calls... but 296 convenient to test for this after all three calls... but
290 is that acceptable? 297 is that acceptable?
291 */ 298 */
292 ifdlist = set2list(&ifdset, rfd2obj); 299 ifdlist = set2list(&ifdset, rfd2obj);
293 ofdlist = set2list(&ofdset, wfd2obj); 300 ofdlist = set2list(&ofdset, wfd2obj);
294 efdlist = set2list(&efdset, efd2obj); 301 efdlist = set2list(&efdset, efd2obj);
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 428
422 fd = PyObject_AsFileDescriptor(o); 429 fd = PyObject_AsFileDescriptor(o);
423 if (fd == -1) return NULL; 430 if (fd == -1) return NULL;
424 431
425 /* Modify registered fd */ 432 /* Modify registered fd */
426 key = PyLong_FromLong(fd); 433 key = PyLong_FromLong(fd);
427 if (key == NULL) 434 if (key == NULL)
428 return NULL; 435 return NULL;
429 if (PyDict_GetItem(self->dict, key) == NULL) { 436 if (PyDict_GetItem(self->dict, key) == NULL) {
430 errno = ENOENT; 437 errno = ENOENT;
431 PyErr_SetFromErrno(PyExc_IOError); 438 PyErr_SetFromErrno(PyExc_OSError);
439 Py_DECREF(key);
432 return NULL; 440 return NULL;
433 } 441 }
434 value = PyLong_FromLong(events); 442 value = PyLong_FromLong(events);
435 if (value == NULL) { 443 if (value == NULL) {
436 Py_DECREF(key); 444 Py_DECREF(key);
437 return NULL; 445 return NULL;
438 } 446 }
439 err = PyDict_SetItem(self->dict, key, value); 447 err = PyDict_SetItem(self->dict, key, value);
440 Py_DECREF(key); 448 Py_DECREF(key);
441 Py_DECREF(value); 449 Py_DECREF(value);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 if (!self->ufd_uptodate) 528 if (!self->ufd_uptodate)
521 if (update_ufd_array(self) == 0) 529 if (update_ufd_array(self) == 0)
522 return NULL; 530 return NULL;
523 531
524 /* call poll() */ 532 /* call poll() */
525 Py_BEGIN_ALLOW_THREADS 533 Py_BEGIN_ALLOW_THREADS
526 poll_result = poll(self->ufds, self->ufd_len, timeout); 534 poll_result = poll(self->ufds, self->ufd_len, timeout);
527 Py_END_ALLOW_THREADS 535 Py_END_ALLOW_THREADS
528 536
529 if (poll_result < 0) { 537 if (poll_result < 0) {
530 PyErr_SetFromErrno(SelectError); 538 PyErr_SetFromErrno(PyExc_OSError);
531 return NULL; 539 return NULL;
532 } 540 }
533 541
534 /* build the result list */ 542 /* build the result list */
535 543
536 result_list = PyList_New(poll_result); 544 result_list = PyList_New(poll_result);
537 if (!result_list) 545 if (!result_list)
538 return NULL; 546 return NULL;
539 else { 547 else {
540 for (i = 0, j = 0; j < poll_result; j++) { 548 for (i = 0, j = 0; j < poll_result; j++) {
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 0, /*tp_doc*/ 654 0, /*tp_doc*/
647 0, /*tp_traverse*/ 655 0, /*tp_traverse*/
648 0, /*tp_clear*/ 656 0, /*tp_clear*/
649 0, /*tp_richcompare*/ 657 0, /*tp_richcompare*/
650 0, /*tp_weaklistoffset*/ 658 0, /*tp_weaklistoffset*/
651 0, /*tp_iter*/ 659 0, /*tp_iter*/
652 0, /*tp_iternext*/ 660 0, /*tp_iternext*/
653 poll_methods, /*tp_methods*/ 661 poll_methods, /*tp_methods*/
654 }; 662 };
655 663
664 #ifdef HAVE_SYS_DEVPOLL_H
665 typedef struct {
666 PyObject_HEAD
667 int fd_devpoll;
668 int max_n_fds;
669 int n_fds;
670 struct pollfd *fds;
671 } devpollObject;
672
673 static PyTypeObject devpoll_Type;
674
675 static int devpoll_flush(devpollObject *self)
676 {
677 int size, n;
678
679 if (!self->n_fds) return 0;
680
681 size = sizeof(struct pollfd)*self->n_fds;
682 self->n_fds = 0;
683
684 Py_BEGIN_ALLOW_THREADS
685 n = write(self->fd_devpoll, self->fds, size);
686 Py_END_ALLOW_THREADS
687
688 if (n == -1 ) {
689 PyErr_SetFromErrno(PyExc_IOError);
690 return -1;
691 }
692 if (n < size) {
693 /*
694 ** Data writed to /dev/poll is a binary data structure. It is not
695 ** clear what to do if a partial write occurred. For now, raise
696 ** an exception and see if we actually found this problem in
697 ** the wild.
698 ** See http://bugs.python.org/issue6397.
699 */
700 PyErr_Format(PyExc_IOError, "failed to write all pollfds. "
701 "Please, report at http://bugs.python.org/. "
702 "Data to report: Size tried: %d, actual size written: %d.",
703 size, n);
704 return -1;
705 }
706 return 0;
707 }
708
709 static PyObject *
710 internal_devpoll_register(devpollObject *self, PyObject *args, int remove)
711 {
712 PyObject *o;
713 int fd, events = POLLIN | POLLPRI | POLLOUT;
714
715 if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
716 return NULL;
717 }
718
719 fd = PyObject_AsFileDescriptor(o);
720 if (fd == -1) return NULL;
721
722 if (remove) {
723 self->fds[self->n_fds].fd = fd;
724 self->fds[self->n_fds].events = POLLREMOVE;
725
726 if (++self->n_fds == self->max_n_fds) {
727 if (devpoll_flush(self))
728 return NULL;
729 }
730 }
731
732 self->fds[self->n_fds].fd = fd;
733 self->fds[self->n_fds].events = events;
734
735 if (++self->n_fds == self->max_n_fds) {
736 if (devpoll_flush(self))
737 return NULL;
738 }
739
740 Py_RETURN_NONE;
741 }
742
743 PyDoc_STRVAR(devpoll_register_doc,
744 "register(fd [, eventmask] ) -> None\n\n\
745 Register a file descriptor with the polling object.\n\
746 fd -- either an integer, or an object with a fileno() method returning an\n\
747 int.\n\
748 events -- an optional bitmask describing the type of events to check for");
749
750 static PyObject *
751 devpoll_register(devpollObject *self, PyObject *args)
752 {
753 return internal_devpoll_register(self, args, 0);
754 }
755
756 PyDoc_STRVAR(devpoll_modify_doc,
757 "modify(fd[, eventmask]) -> None\n\n\
758 Modify a possible already registered file descriptor.\n\
759 fd -- either an integer, or an object with a fileno() method returning an\n\
760 int.\n\
761 events -- an optional bitmask describing the type of events to check for");
762
763 static PyObject *
764 devpoll_modify(devpollObject *self, PyObject *args)
765 {
766 return internal_devpoll_register(self, args, 1);
767 }
768
769
770 PyDoc_STRVAR(devpoll_unregister_doc,
771 "unregister(fd) -> None\n\n\
772 Remove a file descriptor being tracked by the polling object.");
773
774 static PyObject *
775 devpoll_unregister(devpollObject *self, PyObject *o)
776 {
777 int fd;
778
779 fd = PyObject_AsFileDescriptor( o );
780 if (fd == -1)
781 return NULL;
782
783 self->fds[self->n_fds].fd = fd;
784 self->fds[self->n_fds].events = POLLREMOVE;
785
786 if (++self->n_fds == self->max_n_fds) {
787 if (devpoll_flush(self))
788 return NULL;
789 }
790
791 Py_RETURN_NONE;
792 }
793
794 PyDoc_STRVAR(devpoll_poll_doc,
795 "poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
796 Polls the set of registered file descriptors, returning a list containing \n\
797 any descriptors that have events or errors to report.");
798
799 static PyObject *
800 devpoll_poll(devpollObject *self, PyObject *args)
801 {
802 struct dvpoll dvp;
803 PyObject *result_list = NULL, *tout = NULL;
804 int poll_result, i;
805 long timeout;
806 PyObject *value, *num1, *num2;
807
808 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
809 return NULL;
810 }
811
812 /* Check values for timeout */
813 if (tout == NULL || tout == Py_None)
814 timeout = -1;
815 else if (!PyNumber_Check(tout)) {
816 PyErr_SetString(PyExc_TypeError,
817 "timeout must be an integer or None");
818 return NULL;
819 }
820 else {
821 tout = PyNumber_Long(tout);
822 if (!tout)
823 return NULL;
824 timeout = PyLong_AsLong(tout);
825 Py_DECREF(tout);
826 if (timeout == -1 && PyErr_Occurred())
827 return NULL;
828 }
829
830 if ((timeout < -1) || (timeout > INT_MAX)) {
831 PyErr_SetString(PyExc_OverflowError,
832 "timeout is out of range");
833 return NULL;
834 }
835
836 if (devpoll_flush(self))
837 return NULL;
838
839 dvp.dp_fds = self->fds;
840 dvp.dp_nfds = self->max_n_fds;
841 dvp.dp_timeout = timeout;
842
843 /* call devpoll() */
844 Py_BEGIN_ALLOW_THREADS
845 poll_result = ioctl(self->fd_devpoll, DP_POLL, &dvp);
846 Py_END_ALLOW_THREADS
847
848 if (poll_result < 0) {
849 PyErr_SetFromErrno(PyExc_IOError);
850 return NULL;
851 }
852
853 /* build the result list */
854
855 result_list = PyList_New(poll_result);
856 if (!result_list)
857 return NULL;
858 else {
859 for (i = 0; i < poll_result; i++) {
860 num1 = PyLong_FromLong(self->fds[i].fd);
861 num2 = PyLong_FromLong(self->fds[i].revents);
862 if ((num1 == NULL) || (num2 == NULL)) {
863 Py_XDECREF(num1);
864 Py_XDECREF(num2);
865 goto error;
866 }
867 value = PyTuple_Pack(2, num1, num2);
868 Py_DECREF(num1);
869 Py_DECREF(num2);
870 if (value == NULL)
871 goto error;
872 if ((PyList_SetItem(result_list, i, value)) == -1) {
873 Py_DECREF(value);
874 goto error;
875 }
876 }
877 }
878
879 return result_list;
880
881 error:
882 Py_DECREF(result_list);
883 return NULL;
884 }
885
886 static PyMethodDef devpoll_methods[] = {
887 {"register", (PyCFunction)devpoll_register,
888 METH_VARARGS, devpoll_register_doc},
889 {"modify", (PyCFunction)devpoll_modify,
890 METH_VARARGS, devpoll_modify_doc},
891 {"unregister", (PyCFunction)devpoll_unregister,
892 METH_O, devpoll_unregister_doc},
893 {"poll", (PyCFunction)devpoll_poll,
894 METH_VARARGS, devpoll_poll_doc},
895 {NULL, NULL} /* sentinel */
896 };
897
898 static devpollObject *
899 newDevPollObject(void)
900 {
901 devpollObject *self;
902 int fd_devpoll, limit_result;
903 struct pollfd *fds;
904 struct rlimit limit;
905
906 Py_BEGIN_ALLOW_THREADS
907 /*
908 ** If we try to process more that getrlimit()
909 ** fds, the kernel will give an error, so
910 ** we set the limit here. It is a dynamic
911 ** value, because we can change rlimit() anytime.
912 */
913 limit_result = getrlimit(RLIMIT_NOFILE, &limit);
914 if (limit_result != -1)
915 fd_devpoll = open("/dev/poll", O_RDWR);
916 Py_END_ALLOW_THREADS
917
918 if (limit_result == -1) {
919 PyErr_SetFromErrno(PyExc_OSError);
920 return NULL;
921 }
922 if (fd_devpoll == -1) {
923 PyErr_SetFromErrnoWithFilename(PyExc_IOError, "/dev/poll");
924 return NULL;
925 }
926
927 fds = PyMem_NEW(struct pollfd, limit.rlim_cur);
928 if (fds == NULL) {
929 close(fd_devpoll);
930 PyErr_NoMemory();
931 return NULL;
932 }
933
934 self = PyObject_New(devpollObject, &devpoll_Type);
935 if (self == NULL) {
936 close(fd_devpoll);
937 PyMem_DEL(fds);
938 return NULL;
939 }
940 self->fd_devpoll = fd_devpoll;
941 self->max_n_fds = limit.rlim_cur;
942 self->n_fds = 0;
943 self->fds = fds;
944
945 return self;
946 }
947
948 static void
949 devpoll_dealloc(devpollObject *self)
950 {
951 Py_BEGIN_ALLOW_THREADS
952 close(self->fd_devpoll);
953 Py_END_ALLOW_THREADS
954
955 PyMem_DEL(self->fds);
956
957 PyObject_Del(self);
958 }
959
960 static PyTypeObject devpoll_Type = {
961 /* The ob_type field must be initialized in the module init function
962 * to be portable to Windows without using C++. */
963 PyVarObject_HEAD_INIT(NULL, 0)
964 "select.devpoll", /*tp_name*/
965 sizeof(devpollObject), /*tp_basicsize*/
966 0, /*tp_itemsize*/
967 /* methods */
968 (destructor)devpoll_dealloc, /*tp_dealloc*/
969 0, /*tp_print*/
970 0, /*tp_getattr*/
971 0, /*tp_setattr*/
972 0, /*tp_reserved*/
973 0, /*tp_repr*/
974 0, /*tp_as_number*/
975 0, /*tp_as_sequence*/
976 0, /*tp_as_mapping*/
977 0, /*tp_hash*/
978 0, /*tp_call*/
979 0, /*tp_str*/
980 0, /*tp_getattro*/
981 0, /*tp_setattro*/
982 0, /*tp_as_buffer*/
983 Py_TPFLAGS_DEFAULT, /*tp_flags*/
984 0, /*tp_doc*/
985 0, /*tp_traverse*/
986 0, /*tp_clear*/
987 0, /*tp_richcompare*/
988 0, /*tp_weaklistoffset*/
989 0, /*tp_iter*/
990 0, /*tp_iternext*/
991 devpoll_methods, /*tp_methods*/
992 };
993 #endif /* HAVE_SYS_DEVPOLL_H */
994
995
996
656 PyDoc_STRVAR(poll_doc, 997 PyDoc_STRVAR(poll_doc,
657 "Returns a polling object, which supports registering and\n\ 998 "Returns a polling object, which supports registering and\n\
658 unregistering file descriptors, and then polling them for I/O events."); 999 unregistering file descriptors, and then polling them for I/O events.");
659 1000
660 static PyObject * 1001 static PyObject *
661 select_poll(PyObject *self, PyObject *unused) 1002 select_poll(PyObject *self, PyObject *unused)
662 { 1003 {
663 return (PyObject *)newPollObject(); 1004 return (PyObject *)newPollObject();
664 } 1005 }
1006
1007 #ifdef HAVE_SYS_DEVPOLL_H
1008 PyDoc_STRVAR(devpoll_doc,
1009 "Returns a polling object, which supports registering and\n\
1010 unregistering file descriptors, and then polling them for I/O events.");
1011
1012 static PyObject *
1013 select_devpoll(PyObject *self, PyObject *unused)
1014 {
1015 return (PyObject *)newDevPollObject();
1016 }
1017 #endif
1018
665 1019
666 #ifdef __APPLE__ 1020 #ifdef __APPLE__
667 /* 1021 /*
668 * On some systems poll() sets errno on invalid file descriptors. We test 1022 * On some systems poll() sets errno on invalid file descriptors. We test
669 * for this at runtime because this bug may be fixed or introduced between 1023 * for this at runtime because this bug may be fixed or introduced between
670 * OS releases. 1024 * OS releases.
671 */ 1025 */
672 static int select_have_broken_poll(void) 1026 static int select_have_broken_poll(void)
673 { 1027 {
674 int poll_test; 1028 int poll_test;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 self->epfd = -1; 1085 self->epfd = -1;
732 Py_BEGIN_ALLOW_THREADS 1086 Py_BEGIN_ALLOW_THREADS
733 if (close(epfd) < 0) 1087 if (close(epfd) < 0)
734 save_errno = errno; 1088 save_errno = errno;
735 Py_END_ALLOW_THREADS 1089 Py_END_ALLOW_THREADS
736 } 1090 }
737 return save_errno; 1091 return save_errno;
738 } 1092 }
739 1093
740 static PyObject * 1094 static PyObject *
741 newPyEpoll_Object(PyTypeObject *type, int sizehint, SOCKET fd) 1095 newPyEpoll_Object(PyTypeObject *type, int sizehint, int flags, SOCKET fd)
742 { 1096 {
743 pyEpoll_Object *self; 1097 pyEpoll_Object *self;
744
745 if (sizehint == -1) {
746 sizehint = FD_SETSIZE-1;
747 }
748 else if (sizehint < 1) {
749 PyErr_Format(PyExc_ValueError,
750 "sizehint must be greater zero, got %d",
751 sizehint);
752 return NULL;
753 }
754 1098
755 assert(type != NULL && type->tp_alloc != NULL); 1099 assert(type != NULL && type->tp_alloc != NULL);
756 self = (pyEpoll_Object *) type->tp_alloc(type, 0); 1100 self = (pyEpoll_Object *) type->tp_alloc(type, 0);
757 if (self == NULL) 1101 if (self == NULL)
758 return NULL; 1102 return NULL;
759 1103
760 if (fd == -1) { 1104 if (fd == -1) {
761 Py_BEGIN_ALLOW_THREADS 1105 Py_BEGIN_ALLOW_THREADS
1106 #ifdef HAVE_EPOLL_CREATE1
1107 if (flags)
1108 self->epfd = epoll_create1(flags);
1109 else
1110 #endif
762 self->epfd = epoll_create(sizehint); 1111 self->epfd = epoll_create(sizehint);
763 Py_END_ALLOW_THREADS 1112 Py_END_ALLOW_THREADS
764 } 1113 }
765 else { 1114 else {
766 self->epfd = fd; 1115 self->epfd = fd;
767 } 1116 }
768 if (self->epfd < 0) { 1117 if (self->epfd < 0) {
769 Py_DECREF(self); 1118 Py_DECREF(self);
770 PyErr_SetFromErrno(PyExc_IOError); 1119 PyErr_SetFromErrno(PyExc_OSError);
771 return NULL; 1120 return NULL;
772 } 1121 }
773 return (PyObject *)self; 1122 return (PyObject *)self;
774 } 1123 }
775 1124
776 1125
777 static PyObject * 1126 static PyObject *
778 pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 1127 pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
779 { 1128 {
780 int sizehint = -1; 1129 int flags = 0, sizehint = FD_SETSIZE - 1;
781 static char *kwlist[] = {"sizehint", NULL}; 1130 static char *kwlist[] = {"sizehint", "flags", NULL};
782 1131
783 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:epoll", kwlist, 1132 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist,
784 &sizehint)) 1133 &sizehint, &flags))
785 return NULL; 1134 return NULL;
786 1135 if (sizehint < 0) {
787 return newPyEpoll_Object(type, sizehint, -1); 1136 PyErr_SetString(PyExc_ValueError, "negative sizehint");
1137 return NULL;
1138 }
1139
1140 return newPyEpoll_Object(type, sizehint, flags, -1);
788 } 1141 }
789 1142
790 1143
791 static void 1144 static void
792 pyepoll_dealloc(pyEpoll_Object *self) 1145 pyepoll_dealloc(pyEpoll_Object *self)
793 { 1146 {
794 (void)pyepoll_internal_close(self); 1147 (void)pyepoll_internal_close(self);
795 Py_TYPE(self)->tp_free(self); 1148 Py_TYPE(self)->tp_free(self);
796 } 1149 }
797 1150
798 static PyObject* 1151 static PyObject*
799 pyepoll_close(pyEpoll_Object *self) 1152 pyepoll_close(pyEpoll_Object *self)
800 { 1153 {
801 errno = pyepoll_internal_close(self); 1154 errno = pyepoll_internal_close(self);
802 if (errno < 0) { 1155 if (errno < 0) {
803 PyErr_SetFromErrno(PyExc_IOError); 1156 PyErr_SetFromErrno(PyExc_OSError);
804 return NULL; 1157 return NULL;
805 } 1158 }
806 Py_RETURN_NONE; 1159 Py_RETURN_NONE;
807 } 1160 }
808 1161
809 PyDoc_STRVAR(pyepoll_close_doc, 1162 PyDoc_STRVAR(pyepoll_close_doc,
810 "close() -> None\n\ 1163 "close() -> None\n\
811 \n\ 1164 \n\
812 Close the epoll control file descriptor. Further operations on the epoll\n\ 1165 Close the epoll control file descriptor. Further operations on the epoll\n\
813 object will raise an exception."); 1166 object will raise an exception.");
(...skipping 21 matching lines...) Expand all
835 Return the epoll control file descriptor."); 1188 Return the epoll control file descriptor.");
836 1189
837 static PyObject* 1190 static PyObject*
838 pyepoll_fromfd(PyObject *cls, PyObject *args) 1191 pyepoll_fromfd(PyObject *cls, PyObject *args)
839 { 1192 {
840 SOCKET fd; 1193 SOCKET fd;
841 1194
842 if (!PyArg_ParseTuple(args, "i:fromfd", &fd)) 1195 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
843 return NULL; 1196 return NULL;
844 1197
845 return newPyEpoll_Object((PyTypeObject*)cls, -1, fd); 1198 return newPyEpoll_Object((PyTypeObject*)cls, FD_SETSIZE - 1, 0, fd);
846 } 1199 }
847 1200
848 PyDoc_STRVAR(pyepoll_fromfd_doc, 1201 PyDoc_STRVAR(pyepoll_fromfd_doc,
849 "fromfd(fd) -> epoll\n\ 1202 "fromfd(fd) -> epoll\n\
850 \n\ 1203 \n\
851 Create an epoll object from a given control fd."); 1204 Create an epoll object from a given control fd.");
852 1205
853 static PyObject * 1206 static PyObject *
854 pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events) 1207 pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)
855 { 1208 {
(...skipping 30 matching lines...) Expand all
886 errno = 0; 1239 errno = 0;
887 } 1240 }
888 Py_END_ALLOW_THREADS 1241 Py_END_ALLOW_THREADS
889 break; 1242 break;
890 default: 1243 default:
891 result = -1; 1244 result = -1;
892 errno = EINVAL; 1245 errno = EINVAL;
893 } 1246 }
894 1247
895 if (result < 0) { 1248 if (result < 0) {
896 PyErr_SetFromErrno(PyExc_IOError); 1249 PyErr_SetFromErrno(PyExc_OSError);
897 return NULL; 1250 return NULL;
898 } 1251 }
899 Py_RETURN_NONE; 1252 Py_RETURN_NONE;
900 } 1253 }
901 1254
902 static PyObject * 1255 static PyObject *
903 pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds) 1256 pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
904 { 1257 {
905 PyObject *pfd; 1258 PyObject *pfd;
906 unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI; 1259 unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
907 static char *kwlist[] = {"fd", "eventmask", NULL}; 1260 static char *kwlist[] = {"fd", "eventmask", NULL};
908 1261
909 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist, 1262 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
910 &pfd, &events)) { 1263 &pfd, &events)) {
911 return NULL; 1264 return NULL;
912 } 1265 }
913 1266
914 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events); 1267 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
915 } 1268 }
916 1269
917 PyDoc_STRVAR(pyepoll_register_doc, 1270 PyDoc_STRVAR(pyepoll_register_doc,
918 "register(fd[, eventmask]) -> None\n\ 1271 "register(fd[, eventmask]) -> None\n\
919 \n\ 1272 \n\
920 Registers a new fd or modifies an already registered fd.\n\ 1273 Registers a new fd or raises an OSError if the fd is already registered.\n\
921 fd is the target file descriptor of the operation.\n\ 1274 fd is the target file descriptor of the operation.\n\
922 events is a bit set composed of the various EPOLL constants; the default\n\ 1275 events is a bit set composed of the various EPOLL constants; the default\n\
923 is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\ 1276 is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\
924 \n\ 1277 \n\
925 The epoll interface supports all file descriptors that support poll."); 1278 The epoll interface supports all file descriptors that support poll.");
926 1279
927 static PyObject * 1280 static PyObject *
928 pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds) 1281 pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
929 { 1282 {
930 PyObject *pfd; 1283 PyObject *pfd;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1009 if (evs == NULL) { 1362 if (evs == NULL) {
1010 Py_DECREF(self); 1363 Py_DECREF(self);
1011 PyErr_NoMemory(); 1364 PyErr_NoMemory();
1012 return NULL; 1365 return NULL;
1013 } 1366 }
1014 1367
1015 Py_BEGIN_ALLOW_THREADS 1368 Py_BEGIN_ALLOW_THREADS
1016 nfds = epoll_wait(self->epfd, evs, maxevents, timeout); 1369 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
1017 Py_END_ALLOW_THREADS 1370 Py_END_ALLOW_THREADS
1018 if (nfds < 0) { 1371 if (nfds < 0) {
1019 PyErr_SetFromErrno(PyExc_IOError); 1372 PyErr_SetFromErrno(PyExc_OSError);
1020 goto error; 1373 goto error;
1021 } 1374 }
1022 1375
1023 elist = PyList_New(nfds); 1376 elist = PyList_New(nfds);
1024 if (elist == NULL) { 1377 if (elist == NULL) {
1025 goto error; 1378 goto error;
1026 } 1379 }
1027 1380
1028 for (i = 0; i < nfds; i++) { 1381 for (i = 0; i < nfds; i++) {
1029 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events); 1382 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1064 {NULL, NULL}, 1417 {NULL, NULL},
1065 }; 1418 };
1066 1419
1067 static PyGetSetDef pyepoll_getsetlist[] = { 1420 static PyGetSetDef pyepoll_getsetlist[] = {
1068 {"closed", (getter)pyepoll_get_closed, NULL, 1421 {"closed", (getter)pyepoll_get_closed, NULL,
1069 "True if the epoll handler is closed"}, 1422 "True if the epoll handler is closed"},
1070 {0}, 1423 {0},
1071 }; 1424 };
1072 1425
1073 PyDoc_STRVAR(pyepoll_doc, 1426 PyDoc_STRVAR(pyepoll_doc,
1074 "select.epoll([sizehint=-1])\n\ 1427 "select.epoll(sizehint=-1, flags=0)\n\
1075 \n\ 1428 \n\
1076 Returns an epolling object\n\ 1429 Returns an epolling object\n\
1077 \n\ 1430 \n\
1078 sizehint must be a positive integer or -1 for the default size. The\n\ 1431 sizehint must be a positive integer or -1 for the default size. The\n\
1079 sizehint is used to optimize internal data structures. It doesn't limit\n\ 1432 sizehint is used to optimize internal data structures. It doesn't limit\n\
1080 the maximum number of monitored events."); 1433 the maximum number of monitored events.");
1081 1434
1082 static PyTypeObject pyEpoll_Type = { 1435 static PyTypeObject pyEpoll_Type = {
1083 PyVarObject_HEAD_INIT(NULL, 0) 1436 PyVarObject_HEAD_INIT(NULL, 0)
1084 "select.epoll", /* tp_name */ 1437 "select.epoll", /* tp_name */
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
1400 if (fd == -1) { 1753 if (fd == -1) {
1401 Py_BEGIN_ALLOW_THREADS 1754 Py_BEGIN_ALLOW_THREADS
1402 self->kqfd = kqueue(); 1755 self->kqfd = kqueue();
1403 Py_END_ALLOW_THREADS 1756 Py_END_ALLOW_THREADS
1404 } 1757 }
1405 else { 1758 else {
1406 self->kqfd = fd; 1759 self->kqfd = fd;
1407 } 1760 }
1408 if (self->kqfd < 0) { 1761 if (self->kqfd < 0) {
1409 Py_DECREF(self); 1762 Py_DECREF(self);
1410 PyErr_SetFromErrno(PyExc_IOError); 1763 PyErr_SetFromErrno(PyExc_OSError);
1411 return NULL; 1764 return NULL;
1412 } 1765 }
1413 return (PyObject *)self; 1766 return (PyObject *)self;
1414 } 1767 }
1415 1768
1416 static PyObject * 1769 static PyObject *
1417 kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 1770 kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1418 { 1771 {
1419 1772
1420 if ((args != NULL && PyObject_Size(args)) || 1773 if ((args != NULL && PyObject_Size(args)) ||
(...skipping 11 matching lines...) Expand all
1432 { 1785 {
1433 kqueue_queue_internal_close(self); 1786 kqueue_queue_internal_close(self);
1434 Py_TYPE(self)->tp_free(self); 1787 Py_TYPE(self)->tp_free(self);
1435 } 1788 }
1436 1789
1437 static PyObject* 1790 static PyObject*
1438 kqueue_queue_close(kqueue_queue_Object *self) 1791 kqueue_queue_close(kqueue_queue_Object *self)
1439 { 1792 {
1440 errno = kqueue_queue_internal_close(self); 1793 errno = kqueue_queue_internal_close(self);
1441 if (errno < 0) { 1794 if (errno < 0) {
1442 PyErr_SetFromErrno(PyExc_IOError); 1795 PyErr_SetFromErrno(PyExc_OSError);
1443 return NULL; 1796 return NULL;
1444 } 1797 }
1445 Py_RETURN_NONE; 1798 Py_RETURN_NONE;
1446 } 1799 }
1447 1800
1448 PyDoc_STRVAR(kqueue_queue_close_doc, 1801 PyDoc_STRVAR(kqueue_queue_close_doc,
1449 "close() -> None\n\ 1802 "close() -> None\n\
1450 \n\ 1803 \n\
1451 Close the kqueue control file descriptor. Further operations on the kqueue\n\ 1804 Close the kqueue control file descriptor. Further operations on the kqueue\n\
1452 object will raise an exception."); 1805 object will raise an exception.");
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 int nevents = 0; 1848 int nevents = 0;
1496 int gotevents = 0; 1849 int gotevents = 0;
1497 int nchanges = 0; 1850 int nchanges = 0;
1498 int i = 0; 1851 int i = 0;
1499 PyObject *otimeout = NULL; 1852 PyObject *otimeout = NULL;
1500 PyObject *ch = NULL; 1853 PyObject *ch = NULL;
1501 PyObject *it = NULL, *ei = NULL; 1854 PyObject *it = NULL, *ei = NULL;
1502 PyObject *result = NULL; 1855 PyObject *result = NULL;
1503 struct kevent *evl = NULL; 1856 struct kevent *evl = NULL;
1504 struct kevent *chl = NULL; 1857 struct kevent *chl = NULL;
1505 struct timespec timeoutspec; 1858 struct timespec timeout;
1506 struct timespec *ptimeoutspec; 1859 struct timespec *ptimeoutspec;
1507 1860
1508 if (self->kqfd < 0) 1861 if (self->kqfd < 0)
1509 return kqueue_queue_err_closed(); 1862 return kqueue_queue_err_closed();
1510 1863
1511 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout)) 1864 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
1512 return NULL; 1865 return NULL;
1513 1866
1514 if (nevents < 0) { 1867 if (nevents < 0) {
1515 PyErr_Format(PyExc_ValueError, 1868 PyErr_Format(PyExc_ValueError,
1516 "Length of eventlist must be 0 or positive, got %d", 1869 "Length of eventlist must be 0 or positive, got %d",
1517 nevents); 1870 nevents);
1518 return NULL; 1871 return NULL;
1519 } 1872 }
1520 1873
1521 if (otimeout == Py_None || otimeout == NULL) { 1874 if (otimeout == Py_None || otimeout == NULL) {
1522 ptimeoutspec = NULL; 1875 ptimeoutspec = NULL;
1523 } 1876 }
1524 else if (PyNumber_Check(otimeout)) { 1877 else if (PyNumber_Check(otimeout)) {
1525 double timeout; 1878 if (_PyTime_ObjectToTimespec(otimeout,
1526 long seconds; 1879 &timeout.tv_sec, &timeout.tv_nsec) == -1)
1527
1528 timeout = PyFloat_AsDouble(otimeout);
1529 if (timeout == -1 && PyErr_Occurred())
1530 return NULL; 1880 return NULL;
1531 if (timeout > (double)LONG_MAX) { 1881
1532 PyErr_SetString(PyExc_OverflowError, 1882 if (timeout.tv_sec < 0) {
1533 "timeout period too long");
1534 return NULL;
1535 }
1536 if (timeout < 0) {
1537 PyErr_SetString(PyExc_ValueError, 1883 PyErr_SetString(PyExc_ValueError,
1538 "timeout must be positive or None"); 1884 "timeout must be positive or None");
1539 return NULL; 1885 return NULL;
1540 } 1886 }
1541 1887 ptimeoutspec = &timeout;
1542 seconds = (long)timeout;
1543 timeout = timeout - (double)seconds;
1544 timeoutspec.tv_sec = seconds;
1545 timeoutspec.tv_nsec = (long)(timeout * 1E9);
1546 ptimeoutspec = &timeoutspec;
1547 } 1888 }
1548 else { 1889 else {
1549 PyErr_Format(PyExc_TypeError, 1890 PyErr_Format(PyExc_TypeError,
1550 "timeout argument must be an number " 1891 "timeout argument must be an number "
1551 "or None, got %.200s", 1892 "or None, got %.200s",
1552 Py_TYPE(otimeout)->tp_name); 1893 Py_TYPE(otimeout)->tp_name);
1553 return NULL; 1894 return NULL;
1554 } 1895 }
1555 1896
1556 if (ch != NULL && ch != Py_None) { 1897 if (ch != NULL && ch != Py_None) {
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
1713 0, /* tp_descr_get */ 2054 0, /* tp_descr_get */
1714 0, /* tp_descr_set */ 2055 0, /* tp_descr_set */
1715 0, /* tp_dictoffset */ 2056 0, /* tp_dictoffset */
1716 0, /* tp_init */ 2057 0, /* tp_init */
1717 0, /* tp_alloc */ 2058 0, /* tp_alloc */
1718 kqueue_queue_new, /* tp_new */ 2059 kqueue_queue_new, /* tp_new */
1719 0, /* tp_free */ 2060 0, /* tp_free */
1720 }; 2061 };
1721 2062
1722 #endif /* HAVE_KQUEUE */ 2063 #endif /* HAVE_KQUEUE */
2064
2065
2066
2067
2068
1723 /* ************************************************************************ */ 2069 /* ************************************************************************ */
1724 2070
1725 PyDoc_STRVAR(select_doc, 2071 PyDoc_STRVAR(select_doc,
1726 "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\ 2072 "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
1727 \n\ 2073 \n\
1728 Wait until one or more file descriptors are ready for some kind of I/O.\n\ 2074 Wait until one or more file descriptors are ready for some kind of I/O.\n\
1729 The first three arguments are sequences of file descriptors to be waited for:\n\ 2075 The first three arguments are sequences of file descriptors to be waited for:\n\
1730 rlist -- wait until ready for reading\n\ 2076 rlist -- wait until ready for reading\n\
1731 wlist -- wait until ready for writing\n\ 2077 wlist -- wait until ready for writing\n\
1732 xlist -- wait for an ``exceptional condition''\n\ 2078 xlist -- wait for an ``exceptional condition''\n\
(...skipping 11 matching lines...) Expand all
1744 \n\ 2090 \n\
1745 *** IMPORTANT NOTICE ***\n\ 2091 *** IMPORTANT NOTICE ***\n\
1746 On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\ 2092 On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\
1747 descriptors can be used."); 2093 descriptors can be used.");
1748 2094
1749 static PyMethodDef select_methods[] = { 2095 static PyMethodDef select_methods[] = {
1750 {"select", select_select, METH_VARARGS, select_doc}, 2096 {"select", select_select, METH_VARARGS, select_doc},
1751 #ifdef HAVE_POLL 2097 #ifdef HAVE_POLL
1752 {"poll", select_poll, METH_NOARGS, poll_doc}, 2098 {"poll", select_poll, METH_NOARGS, poll_doc},
1753 #endif /* HAVE_POLL */ 2099 #endif /* HAVE_POLL */
2100 #ifdef HAVE_SYS_DEVPOLL_H
2101 {"devpoll", select_devpoll, METH_NOARGS, devpoll_doc},
2102 #endif
1754 {0, 0}, /* sentinel */ 2103 {0, 0}, /* sentinel */
1755 }; 2104 };
1756 2105
1757 PyDoc_STRVAR(module_doc, 2106 PyDoc_STRVAR(module_doc,
1758 "This module supports asynchronous I/O on multiple file descriptors.\n\ 2107 "This module supports asynchronous I/O on multiple file descriptors.\n\
1759 \n\ 2108 \n\
1760 *** IMPORTANT NOTICE ***\n\ 2109 *** IMPORTANT NOTICE ***\n\
1761 On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptor s."); 2110 On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptor s.");
1762 2111
1763 2112
1764 static struct PyModuleDef selectmodule = { 2113 static struct PyModuleDef selectmodule = {
1765 PyModuleDef_HEAD_INIT, 2114 PyModuleDef_HEAD_INIT,
1766 "select", 2115 "select",
1767 module_doc, 2116 module_doc,
1768 -1, 2117 -1,
1769 select_methods, 2118 select_methods,
1770 NULL, 2119 NULL,
1771 NULL, 2120 NULL,
1772 NULL, 2121 NULL,
1773 NULL 2122 NULL
1774 }; 2123 };
1775 2124
2125
2126
2127
1776 PyMODINIT_FUNC 2128 PyMODINIT_FUNC
1777 PyInit_select(void) 2129 PyInit_select(void)
1778 { 2130 {
1779 PyObject *m; 2131 PyObject *m;
1780 m = PyModule_Create(&selectmodule); 2132 m = PyModule_Create(&selectmodule);
1781 if (m == NULL) 2133 if (m == NULL)
1782 return NULL; 2134 return NULL;
1783 2135
1784 SelectError = PyErr_NewException("select.error", NULL, NULL); 2136 Py_INCREF(PyExc_OSError);
1785 Py_INCREF(SelectError); 2137 PyModule_AddObject(m, "error", PyExc_OSError);
1786 PyModule_AddObject(m, "error", SelectError);
1787 2138
1788 #ifdef PIPE_BUF 2139 #ifdef PIPE_BUF
2140 #ifdef HAVE_BROKEN_PIPE_BUF
2141 #undef PIPE_BUF
2142 #define PIPE_BUF 512
2143 #endif
1789 PyModule_AddIntConstant(m, "PIPE_BUF", PIPE_BUF); 2144 PyModule_AddIntConstant(m, "PIPE_BUF", PIPE_BUF);
1790 #endif 2145 #endif
1791 2146
1792 #if defined(HAVE_POLL) 2147 #if defined(HAVE_POLL)
1793 #ifdef __APPLE__ 2148 #ifdef __APPLE__
1794 if (select_have_broken_poll()) { 2149 if (select_have_broken_poll()) {
1795 if (PyObject_DelAttrString(m, "poll") == -1) { 2150 if (PyObject_DelAttrString(m, "poll") == -1) {
1796 PyErr_Clear(); 2151 PyErr_Clear();
1797 } 2152 }
1798 } else { 2153 } else {
(...skipping 20 matching lines...) Expand all
1819 #endif 2174 #endif
1820 #ifdef POLLWRBAND 2175 #ifdef POLLWRBAND
1821 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND); 2176 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
1822 #endif 2177 #endif
1823 #ifdef POLLMSG 2178 #ifdef POLLMSG
1824 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG); 2179 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
1825 #endif 2180 #endif
1826 } 2181 }
1827 #endif /* HAVE_POLL */ 2182 #endif /* HAVE_POLL */
1828 2183
2184 #ifdef HAVE_SYS_DEVPOLL_H
2185 if (PyType_Ready(&devpoll_Type) < 0)
2186 return NULL;
2187 #endif
2188
1829 #ifdef HAVE_EPOLL 2189 #ifdef HAVE_EPOLL
1830 Py_TYPE(&pyEpoll_Type) = &PyType_Type; 2190 Py_TYPE(&pyEpoll_Type) = &PyType_Type;
1831 if (PyType_Ready(&pyEpoll_Type) < 0) 2191 if (PyType_Ready(&pyEpoll_Type) < 0)
1832 return NULL; 2192 return NULL;
1833 2193
1834 Py_INCREF(&pyEpoll_Type); 2194 Py_INCREF(&pyEpoll_Type);
1835 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type); 2195 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
1836 2196
1837 PyModule_AddIntConstant(m, "EPOLLIN", EPOLLIN); 2197 PyModule_AddIntConstant(m, "EPOLLIN", EPOLLIN);
1838 PyModule_AddIntConstant(m, "EPOLLOUT", EPOLLOUT); 2198 PyModule_AddIntConstant(m, "EPOLLOUT", EPOLLOUT);
1839 PyModule_AddIntConstant(m, "EPOLLPRI", EPOLLPRI); 2199 PyModule_AddIntConstant(m, "EPOLLPRI", EPOLLPRI);
1840 PyModule_AddIntConstant(m, "EPOLLERR", EPOLLERR); 2200 PyModule_AddIntConstant(m, "EPOLLERR", EPOLLERR);
1841 PyModule_AddIntConstant(m, "EPOLLHUP", EPOLLHUP); 2201 PyModule_AddIntConstant(m, "EPOLLHUP", EPOLLHUP);
1842 PyModule_AddIntConstant(m, "EPOLLET", EPOLLET); 2202 PyModule_AddIntConstant(m, "EPOLLET", EPOLLET);
1843 #ifdef EPOLLONESHOT 2203 #ifdef EPOLLONESHOT
1844 /* Kernel 2.6.2+ */ 2204 /* Kernel 2.6.2+ */
1845 PyModule_AddIntConstant(m, "EPOLLONESHOT", EPOLLONESHOT); 2205 PyModule_AddIntConstant(m, "EPOLLONESHOT", EPOLLONESHOT);
1846 #endif 2206 #endif
1847 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */ 2207 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
1848 PyModule_AddIntConstant(m, "EPOLLRDNORM", EPOLLRDNORM); 2208 PyModule_AddIntConstant(m, "EPOLLRDNORM", EPOLLRDNORM);
1849 PyModule_AddIntConstant(m, "EPOLLRDBAND", EPOLLRDBAND); 2209 PyModule_AddIntConstant(m, "EPOLLRDBAND", EPOLLRDBAND);
1850 PyModule_AddIntConstant(m, "EPOLLWRNORM", EPOLLWRNORM); 2210 PyModule_AddIntConstant(m, "EPOLLWRNORM", EPOLLWRNORM);
1851 PyModule_AddIntConstant(m, "EPOLLWRBAND", EPOLLWRBAND); 2211 PyModule_AddIntConstant(m, "EPOLLWRBAND", EPOLLWRBAND);
1852 PyModule_AddIntConstant(m, "EPOLLMSG", EPOLLMSG); 2212 PyModule_AddIntConstant(m, "EPOLLMSG", EPOLLMSG);
2213
2214 #ifdef EPOLL_CLOEXEC
2215 PyModule_AddIntConstant(m, "EPOLL_CLOEXEC", EPOLL_CLOEXEC);
2216 #endif
1853 #endif /* HAVE_EPOLL */ 2217 #endif /* HAVE_EPOLL */
1854 2218
1855 #ifdef HAVE_KQUEUE 2219 #ifdef HAVE_KQUEUE
1856 kqueue_event_Type.tp_new = PyType_GenericNew; 2220 kqueue_event_Type.tp_new = PyType_GenericNew;
1857 Py_TYPE(&kqueue_event_Type) = &PyType_Type; 2221 Py_TYPE(&kqueue_event_Type) = &PyType_Type;
1858 if(PyType_Ready(&kqueue_event_Type) < 0) 2222 if(PyType_Ready(&kqueue_event_Type) < 0)
1859 return NULL; 2223 return NULL;
1860 2224
1861 Py_INCREF(&kqueue_event_Type); 2225 Py_INCREF(&kqueue_event_Type);
1862 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type); 2226 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1919 /* NETDEV filter flags */ 2283 /* NETDEV filter flags */
1920 #ifdef EVFILT_NETDEV 2284 #ifdef EVFILT_NETDEV
1921 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP); 2285 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
1922 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN); 2286 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
1923 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV); 2287 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
1924 #endif 2288 #endif
1925 2289
1926 #endif /* HAVE_KQUEUE */ 2290 #endif /* HAVE_KQUEUE */
1927 return m; 2291 return m;
1928 } 2292 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+