1/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(select_select__doc__,
6"select($module, rlist, wlist, xlist, timeout=None, /)\n"
7"--\n"
8"\n"
9"Wait until one or more file descriptors are ready for some kind of I/O.\n"
10"\n"
11"The first three arguments are iterables of file descriptors to be waited for:\n"
12"rlist -- wait until ready for reading\n"
13"wlist -- wait until ready for writing\n"
14"xlist -- wait for an \"exceptional condition\"\n"
15"If only one kind of condition is required, pass [] for the other lists.\n"
16"\n"
17"A file descriptor is either a socket or file object, or a small integer\n"
18"gotten from a fileno() method call on one of those.\n"
19"\n"
20"The optional 4th argument specifies a timeout in seconds; it may be\n"
21"a floating point number to specify fractions of seconds. If it is absent\n"
22"or None, the call will never time out.\n"
23"\n"
24"The return value is a tuple of three lists corresponding to the first three\n"
25"arguments; each contains the subset of the corresponding file descriptors\n"
26"that are ready.\n"
27"\n"
28"*** IMPORTANT NOTICE ***\n"
29"On Windows, only sockets are supported; on Unix, all file\n"
30"descriptors can be used.");
31
32#define SELECT_SELECT_METHODDEF \
33 {"select", (PyCFunction)(void(*)(void))select_select, METH_FASTCALL, select_select__doc__},
34
35static PyObject *
36select_select_impl(PyObject *module, PyObject *rlist, PyObject *wlist,
37 PyObject *xlist, PyObject *timeout_obj);
38
39static PyObject *
40select_select(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
41{
42 PyObject *return_value = NULL;
43 PyObject *rlist;
44 PyObject *wlist;
45 PyObject *xlist;
46 PyObject *timeout_obj = Py_None;
47
48 if (!_PyArg_CheckPositional("select", nargs, 3, 4)) {
49 goto exit;
50 }
51 rlist = args[0];
52 wlist = args[1];
53 xlist = args[2];
54 if (nargs < 4) {
55 goto skip_optional;
56 }
57 timeout_obj = args[3];
58skip_optional:
59 return_value = select_select_impl(module, rlist, wlist, xlist, timeout_obj);
60
61exit:
62 return return_value;
63}
64
65#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL))
66
67PyDoc_STRVAR(select_poll_register__doc__,
68"register($self, fd,\n"
69" eventmask=select.POLLIN | select.POLLPRI | select.POLLOUT, /)\n"
70"--\n"
71"\n"
72"Register a file descriptor with the polling object.\n"
73"\n"
74" fd\n"
75" either an integer, or an object with a fileno() method returning an int\n"
76" eventmask\n"
77" an optional bitmask describing the type of events to check for");
78
79#define SELECT_POLL_REGISTER_METHODDEF \
80 {"register", (PyCFunction)(void(*)(void))select_poll_register, METH_FASTCALL, select_poll_register__doc__},
81
82static PyObject *
83select_poll_register_impl(pollObject *self, int fd, unsigned short eventmask);
84
85static PyObject *
86select_poll_register(pollObject *self, PyObject *const *args, Py_ssize_t nargs)
87{
88 PyObject *return_value = NULL;
89 int fd;
90 unsigned short eventmask = POLLIN | POLLPRI | POLLOUT;
91
92 if (!_PyArg_CheckPositional("register", nargs, 1, 2)) {
93 goto exit;
94 }
95 if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
96 goto exit;
97 }
98 if (nargs < 2) {
99 goto skip_optional;
100 }
101 if (!_PyLong_UnsignedShort_Converter(args[1], &eventmask)) {
102 goto exit;
103 }
104skip_optional:
105 return_value = select_poll_register_impl(self, fd, eventmask);
106
107exit:
108 return return_value;
109}
110
111#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) */
112
113#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL))
114
115PyDoc_STRVAR(select_poll_modify__doc__,
116"modify($self, fd, eventmask, /)\n"
117"--\n"
118"\n"
119"Modify an already registered file descriptor.\n"
120"\n"
121" fd\n"
122" either an integer, or an object with a fileno() method returning\n"
123" an int\n"
124" eventmask\n"
125" a bitmask describing the type of events to check for");
126
127#define SELECT_POLL_MODIFY_METHODDEF \
128 {"modify", (PyCFunction)(void(*)(void))select_poll_modify, METH_FASTCALL, select_poll_modify__doc__},
129
130static PyObject *
131select_poll_modify_impl(pollObject *self, int fd, unsigned short eventmask);
132
133static PyObject *
134select_poll_modify(pollObject *self, PyObject *const *args, Py_ssize_t nargs)
135{
136 PyObject *return_value = NULL;
137 int fd;
138 unsigned short eventmask;
139
140 if (!_PyArg_CheckPositional("modify", nargs, 2, 2)) {
141 goto exit;
142 }
143 if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
144 goto exit;
145 }
146 if (!_PyLong_UnsignedShort_Converter(args[1], &eventmask)) {
147 goto exit;
148 }
149 return_value = select_poll_modify_impl(self, fd, eventmask);
150
151exit:
152 return return_value;
153}
154
155#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) */
156
157#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL))
158
159PyDoc_STRVAR(select_poll_unregister__doc__,
160"unregister($self, fd, /)\n"
161"--\n"
162"\n"
163"Remove a file descriptor being tracked by the polling object.");
164
165#define SELECT_POLL_UNREGISTER_METHODDEF \
166 {"unregister", (PyCFunction)select_poll_unregister, METH_O, select_poll_unregister__doc__},
167
168static PyObject *
169select_poll_unregister_impl(pollObject *self, int fd);
170
171static PyObject *
172select_poll_unregister(pollObject *self, PyObject *arg)
173{
174 PyObject *return_value = NULL;
175 int fd;
176
177 if (!_PyLong_FileDescriptor_Converter(arg, &fd)) {
178 goto exit;
179 }
180 return_value = select_poll_unregister_impl(self, fd);
181
182exit:
183 return return_value;
184}
185
186#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) */
187
188#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL))
189
190PyDoc_STRVAR(select_poll_poll__doc__,
191"poll($self, timeout=None, /)\n"
192"--\n"
193"\n"
194"Polls the set of registered file descriptors.\n"
195"\n"
196" timeout\n"
197" The maximum time to wait in milliseconds, or else None (or a negative\n"
198" value) to wait indefinitely.\n"
199"\n"
200"Returns a list containing any descriptors that have events or errors to\n"
201"report, as a list of (fd, event) 2-tuples.");
202
203#define SELECT_POLL_POLL_METHODDEF \
204 {"poll", (PyCFunction)(void(*)(void))select_poll_poll, METH_FASTCALL, select_poll_poll__doc__},
205
206static PyObject *
207select_poll_poll_impl(pollObject *self, PyObject *timeout_obj);
208
209static PyObject *
210select_poll_poll(pollObject *self, PyObject *const *args, Py_ssize_t nargs)
211{
212 PyObject *return_value = NULL;
213 PyObject *timeout_obj = Py_None;
214
215 if (!_PyArg_CheckPositional("poll", nargs, 0, 1)) {
216 goto exit;
217 }
218 if (nargs < 1) {
219 goto skip_optional;
220 }
221 timeout_obj = args[0];
222skip_optional:
223 return_value = select_poll_poll_impl(self, timeout_obj);
224
225exit:
226 return return_value;
227}
228
229#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) */
230
231#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H)
232
233PyDoc_STRVAR(select_devpoll_register__doc__,
234"register($self, fd,\n"
235" eventmask=select.POLLIN | select.POLLPRI | select.POLLOUT, /)\n"
236"--\n"
237"\n"
238"Register a file descriptor with the polling object.\n"
239"\n"
240" fd\n"
241" either an integer, or an object with a fileno() method returning\n"
242" an int\n"
243" eventmask\n"
244" an optional bitmask describing the type of events to check for");
245
246#define SELECT_DEVPOLL_REGISTER_METHODDEF \
247 {"register", (PyCFunction)(void(*)(void))select_devpoll_register, METH_FASTCALL, select_devpoll_register__doc__},
248
249static PyObject *
250select_devpoll_register_impl(devpollObject *self, int fd,
251 unsigned short eventmask);
252
253static PyObject *
254select_devpoll_register(devpollObject *self, PyObject *const *args, Py_ssize_t nargs)
255{
256 PyObject *return_value = NULL;
257 int fd;
258 unsigned short eventmask = POLLIN | POLLPRI | POLLOUT;
259
260 if (!_PyArg_CheckPositional("register", nargs, 1, 2)) {
261 goto exit;
262 }
263 if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
264 goto exit;
265 }
266 if (nargs < 2) {
267 goto skip_optional;
268 }
269 if (!_PyLong_UnsignedShort_Converter(args[1], &eventmask)) {
270 goto exit;
271 }
272skip_optional:
273 return_value = select_devpoll_register_impl(self, fd, eventmask);
274
275exit:
276 return return_value;
277}
278
279#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H) */
280
281#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H)
282
283PyDoc_STRVAR(select_devpoll_modify__doc__,
284"modify($self, fd,\n"
285" eventmask=select.POLLIN | select.POLLPRI | select.POLLOUT, /)\n"
286"--\n"
287"\n"
288"Modify a possible already registered file descriptor.\n"
289"\n"
290" fd\n"
291" either an integer, or an object with a fileno() method returning\n"
292" an int\n"
293" eventmask\n"
294" an optional bitmask describing the type of events to check for");
295
296#define SELECT_DEVPOLL_MODIFY_METHODDEF \
297 {"modify", (PyCFunction)(void(*)(void))select_devpoll_modify, METH_FASTCALL, select_devpoll_modify__doc__},
298
299static PyObject *
300select_devpoll_modify_impl(devpollObject *self, int fd,
301 unsigned short eventmask);
302
303static PyObject *
304select_devpoll_modify(devpollObject *self, PyObject *const *args, Py_ssize_t nargs)
305{
306 PyObject *return_value = NULL;
307 int fd;
308 unsigned short eventmask = POLLIN | POLLPRI | POLLOUT;
309
310 if (!_PyArg_CheckPositional("modify", nargs, 1, 2)) {
311 goto exit;
312 }
313 if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
314 goto exit;
315 }
316 if (nargs < 2) {
317 goto skip_optional;
318 }
319 if (!_PyLong_UnsignedShort_Converter(args[1], &eventmask)) {
320 goto exit;
321 }
322skip_optional:
323 return_value = select_devpoll_modify_impl(self, fd, eventmask);
324
325exit:
326 return return_value;
327}
328
329#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H) */
330
331#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H)
332
333PyDoc_STRVAR(select_devpoll_unregister__doc__,
334"unregister($self, fd, /)\n"
335"--\n"
336"\n"
337"Remove a file descriptor being tracked by the polling object.");
338
339#define SELECT_DEVPOLL_UNREGISTER_METHODDEF \
340 {"unregister", (PyCFunction)select_devpoll_unregister, METH_O, select_devpoll_unregister__doc__},
341
342static PyObject *
343select_devpoll_unregister_impl(devpollObject *self, int fd);
344
345static PyObject *
346select_devpoll_unregister(devpollObject *self, PyObject *arg)
347{
348 PyObject *return_value = NULL;
349 int fd;
350
351 if (!_PyLong_FileDescriptor_Converter(arg, &fd)) {
352 goto exit;
353 }
354 return_value = select_devpoll_unregister_impl(self, fd);
355
356exit:
357 return return_value;
358}
359
360#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H) */
361
362#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H)
363
364PyDoc_STRVAR(select_devpoll_poll__doc__,
365"poll($self, timeout=None, /)\n"
366"--\n"
367"\n"
368"Polls the set of registered file descriptors.\n"
369"\n"
370" timeout\n"
371" The maximum time to wait in milliseconds, or else None (or a negative\n"
372" value) to wait indefinitely.\n"
373"\n"
374"Returns a list containing any descriptors that have events or errors to\n"
375"report, as a list of (fd, event) 2-tuples.");
376
377#define SELECT_DEVPOLL_POLL_METHODDEF \
378 {"poll", (PyCFunction)(void(*)(void))select_devpoll_poll, METH_FASTCALL, select_devpoll_poll__doc__},
379
380static PyObject *
381select_devpoll_poll_impl(devpollObject *self, PyObject *timeout_obj);
382
383static PyObject *
384select_devpoll_poll(devpollObject *self, PyObject *const *args, Py_ssize_t nargs)
385{
386 PyObject *return_value = NULL;
387 PyObject *timeout_obj = Py_None;
388
389 if (!_PyArg_CheckPositional("poll", nargs, 0, 1)) {
390 goto exit;
391 }
392 if (nargs < 1) {
393 goto skip_optional;
394 }
395 timeout_obj = args[0];
396skip_optional:
397 return_value = select_devpoll_poll_impl(self, timeout_obj);
398
399exit:
400 return return_value;
401}
402
403#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H) */
404
405#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H)
406
407PyDoc_STRVAR(select_devpoll_close__doc__,
408"close($self, /)\n"
409"--\n"
410"\n"
411"Close the devpoll file descriptor.\n"
412"\n"
413"Further operations on the devpoll object will raise an exception.");
414
415#define SELECT_DEVPOLL_CLOSE_METHODDEF \
416 {"close", (PyCFunction)select_devpoll_close, METH_NOARGS, select_devpoll_close__doc__},
417
418static PyObject *
419select_devpoll_close_impl(devpollObject *self);
420
421static PyObject *
422select_devpoll_close(devpollObject *self, PyObject *Py_UNUSED(ignored))
423{
424 return select_devpoll_close_impl(self);
425}
426
427#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H) */
428
429#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H)
430
431PyDoc_STRVAR(select_devpoll_fileno__doc__,
432"fileno($self, /)\n"
433"--\n"
434"\n"
435"Return the file descriptor.");
436
437#define SELECT_DEVPOLL_FILENO_METHODDEF \
438 {"fileno", (PyCFunction)select_devpoll_fileno, METH_NOARGS, select_devpoll_fileno__doc__},
439
440static PyObject *
441select_devpoll_fileno_impl(devpollObject *self);
442
443static PyObject *
444select_devpoll_fileno(devpollObject *self, PyObject *Py_UNUSED(ignored))
445{
446 return select_devpoll_fileno_impl(self);
447}
448
449#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H) */
450
451#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL))
452
453PyDoc_STRVAR(select_poll__doc__,
454"poll($module, /)\n"
455"--\n"
456"\n"
457"Returns a polling object.\n"
458"\n"
459"This object supports registering and unregistering file descriptors, and then\n"
460"polling them for I/O events.");
461
462#define SELECT_POLL_METHODDEF \
463 {"poll", (PyCFunction)select_poll, METH_NOARGS, select_poll__doc__},
464
465static PyObject *
466select_poll_impl(PyObject *module);
467
468static PyObject *
469select_poll(PyObject *module, PyObject *Py_UNUSED(ignored))
470{
471 return select_poll_impl(module);
472}
473
474#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) */
475
476#if (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H)
477
478PyDoc_STRVAR(select_devpoll__doc__,
479"devpoll($module, /)\n"
480"--\n"
481"\n"
482"Returns a polling object.\n"
483"\n"
484"This object supports registering and unregistering file descriptors, and then\n"
485"polling them for I/O events.");
486
487#define SELECT_DEVPOLL_METHODDEF \
488 {"devpoll", (PyCFunction)select_devpoll, METH_NOARGS, select_devpoll__doc__},
489
490static PyObject *
491select_devpoll_impl(PyObject *module);
492
493static PyObject *
494select_devpoll(PyObject *module, PyObject *Py_UNUSED(ignored))
495{
496 return select_devpoll_impl(module);
497}
498
499#endif /* (defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)) && defined(HAVE_SYS_DEVPOLL_H) */
500
501#if defined(HAVE_EPOLL)
502
503PyDoc_STRVAR(select_epoll__doc__,
504"epoll(sizehint=-1, flags=0)\n"
505"--\n"
506"\n"
507"Returns an epolling object.\n"
508"\n"
509" sizehint\n"
510" The expected number of events to be registered. It must be positive,\n"
511" or -1 to use the default. It is only used on older systems where\n"
512" epoll_create1() is not available; otherwise it has no effect (though its\n"
513" value is still checked).\n"
514" flags\n"
515" Deprecated and completely ignored. However, when supplied, its value\n"
516" must be 0 or select.EPOLL_CLOEXEC, otherwise OSError is raised.");
517
518static PyObject *
519select_epoll_impl(PyTypeObject *type, int sizehint, int flags);
520
521static PyObject *
522select_epoll(PyTypeObject *type, PyObject *args, PyObject *kwargs)
523{
524 PyObject *return_value = NULL;
525 static const char * const _keywords[] = {"sizehint", "flags", NULL};
526 static _PyArg_Parser _parser = {NULL, _keywords, "epoll", 0};
527 PyObject *argsbuf[2];
528 PyObject * const *fastargs;
529 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
530 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
531 int sizehint = -1;
532 int flags = 0;
533
534 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
535 if (!fastargs) {
536 goto exit;
537 }
538 if (!noptargs) {
539 goto skip_optional_pos;
540 }
541 if (fastargs[0]) {
542 sizehint = _PyLong_AsInt(fastargs[0]);
543 if (sizehint == -1 && PyErr_Occurred()) {
544 goto exit;
545 }
546 if (!--noptargs) {
547 goto skip_optional_pos;
548 }
549 }
550 flags = _PyLong_AsInt(fastargs[1]);
551 if (flags == -1 && PyErr_Occurred()) {
552 goto exit;
553 }
554skip_optional_pos:
555 return_value = select_epoll_impl(type, sizehint, flags);
556
557exit:
558 return return_value;
559}
560
561#endif /* defined(HAVE_EPOLL) */
562
563#if defined(HAVE_EPOLL)
564
565PyDoc_STRVAR(select_epoll_close__doc__,
566"close($self, /)\n"
567"--\n"
568"\n"
569"Close the epoll control file descriptor.\n"
570"\n"
571"Further operations on the epoll object will raise an exception.");
572
573#define SELECT_EPOLL_CLOSE_METHODDEF \
574 {"close", (PyCFunction)select_epoll_close, METH_NOARGS, select_epoll_close__doc__},
575
576static PyObject *
577select_epoll_close_impl(pyEpoll_Object *self);
578
579static PyObject *
580select_epoll_close(pyEpoll_Object *self, PyObject *Py_UNUSED(ignored))
581{
582 return select_epoll_close_impl(self);
583}
584
585#endif /* defined(HAVE_EPOLL) */
586
587#if defined(HAVE_EPOLL)
588
589PyDoc_STRVAR(select_epoll_fileno__doc__,
590"fileno($self, /)\n"
591"--\n"
592"\n"
593"Return the epoll control file descriptor.");
594
595#define SELECT_EPOLL_FILENO_METHODDEF \
596 {"fileno", (PyCFunction)select_epoll_fileno, METH_NOARGS, select_epoll_fileno__doc__},
597
598static PyObject *
599select_epoll_fileno_impl(pyEpoll_Object *self);
600
601static PyObject *
602select_epoll_fileno(pyEpoll_Object *self, PyObject *Py_UNUSED(ignored))
603{
604 return select_epoll_fileno_impl(self);
605}
606
607#endif /* defined(HAVE_EPOLL) */
608
609#if defined(HAVE_EPOLL)
610
611PyDoc_STRVAR(select_epoll_fromfd__doc__,
612"fromfd($type, fd, /)\n"
613"--\n"
614"\n"
615"Create an epoll object from a given control fd.");
616
617#define SELECT_EPOLL_FROMFD_METHODDEF \
618 {"fromfd", (PyCFunction)select_epoll_fromfd, METH_O|METH_CLASS, select_epoll_fromfd__doc__},
619
620static PyObject *
621select_epoll_fromfd_impl(PyTypeObject *type, int fd);
622
623static PyObject *
624select_epoll_fromfd(PyTypeObject *type, PyObject *arg)
625{
626 PyObject *return_value = NULL;
627 int fd;
628
629 fd = _PyLong_AsInt(arg);
630 if (fd == -1 && PyErr_Occurred()) {
631 goto exit;
632 }
633 return_value = select_epoll_fromfd_impl(type, fd);
634
635exit:
636 return return_value;
637}
638
639#endif /* defined(HAVE_EPOLL) */
640
641#if defined(HAVE_EPOLL)
642
643PyDoc_STRVAR(select_epoll_register__doc__,
644"register($self, /, fd,\n"
645" eventmask=select.EPOLLIN | select.EPOLLPRI | select.EPOLLOUT)\n"
646"--\n"
647"\n"
648"Registers a new fd or raises an OSError if the fd is already registered.\n"
649"\n"
650" fd\n"
651" the target file descriptor of the operation\n"
652" eventmask\n"
653" a bit set composed of the various EPOLL constants\n"
654"\n"
655"The epoll interface supports all file descriptors that support poll.");
656
657#define SELECT_EPOLL_REGISTER_METHODDEF \
658 {"register", (PyCFunction)(void(*)(void))select_epoll_register, METH_FASTCALL|METH_KEYWORDS, select_epoll_register__doc__},
659
660static PyObject *
661select_epoll_register_impl(pyEpoll_Object *self, int fd,
662 unsigned int eventmask);
663
664static PyObject *
665select_epoll_register(pyEpoll_Object *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
666{
667 PyObject *return_value = NULL;
668 static const char * const _keywords[] = {"fd", "eventmask", NULL};
669 static _PyArg_Parser _parser = {NULL, _keywords, "register", 0};
670 PyObject *argsbuf[2];
671 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
672 int fd;
673 unsigned int eventmask = EPOLLIN | EPOLLPRI | EPOLLOUT;
674
675 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
676 if (!args) {
677 goto exit;
678 }
679 if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
680 goto exit;
681 }
682 if (!noptargs) {
683 goto skip_optional_pos;
684 }
685 eventmask = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
686 if (eventmask == (unsigned int)-1 && PyErr_Occurred()) {
687 goto exit;
688 }
689skip_optional_pos:
690 return_value = select_epoll_register_impl(self, fd, eventmask);
691
692exit:
693 return return_value;
694}
695
696#endif /* defined(HAVE_EPOLL) */
697
698#if defined(HAVE_EPOLL)
699
700PyDoc_STRVAR(select_epoll_modify__doc__,
701"modify($self, /, fd, eventmask)\n"
702"--\n"
703"\n"
704"Modify event mask for a registered file descriptor.\n"
705"\n"
706" fd\n"
707" the target file descriptor of the operation\n"
708" eventmask\n"
709" a bit set composed of the various EPOLL constants");
710
711#define SELECT_EPOLL_MODIFY_METHODDEF \
712 {"modify", (PyCFunction)(void(*)(void))select_epoll_modify, METH_FASTCALL|METH_KEYWORDS, select_epoll_modify__doc__},
713
714static PyObject *
715select_epoll_modify_impl(pyEpoll_Object *self, int fd,
716 unsigned int eventmask);
717
718static PyObject *
719select_epoll_modify(pyEpoll_Object *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
720{
721 PyObject *return_value = NULL;
722 static const char * const _keywords[] = {"fd", "eventmask", NULL};
723 static _PyArg_Parser _parser = {NULL, _keywords, "modify", 0};
724 PyObject *argsbuf[2];
725 int fd;
726 unsigned int eventmask;
727
728 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
729 if (!args) {
730 goto exit;
731 }
732 if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
733 goto exit;
734 }
735 eventmask = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
736 if (eventmask == (unsigned int)-1 && PyErr_Occurred()) {
737 goto exit;
738 }
739 return_value = select_epoll_modify_impl(self, fd, eventmask);
740
741exit:
742 return return_value;
743}
744
745#endif /* defined(HAVE_EPOLL) */
746
747#if defined(HAVE_EPOLL)
748
749PyDoc_STRVAR(select_epoll_unregister__doc__,
750"unregister($self, /, fd)\n"
751"--\n"
752"\n"
753"Remove a registered file descriptor from the epoll object.\n"
754"\n"
755" fd\n"
756" the target file descriptor of the operation");
757
758#define SELECT_EPOLL_UNREGISTER_METHODDEF \
759 {"unregister", (PyCFunction)(void(*)(void))select_epoll_unregister, METH_FASTCALL|METH_KEYWORDS, select_epoll_unregister__doc__},
760
761static PyObject *
762select_epoll_unregister_impl(pyEpoll_Object *self, int fd);
763
764static PyObject *
765select_epoll_unregister(pyEpoll_Object *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
766{
767 PyObject *return_value = NULL;
768 static const char * const _keywords[] = {"fd", NULL};
769 static _PyArg_Parser _parser = {NULL, _keywords, "unregister", 0};
770 PyObject *argsbuf[1];
771 int fd;
772
773 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
774 if (!args) {
775 goto exit;
776 }
777 if (!_PyLong_FileDescriptor_Converter(args[0], &fd)) {
778 goto exit;
779 }
780 return_value = select_epoll_unregister_impl(self, fd);
781
782exit:
783 return return_value;
784}
785
786#endif /* defined(HAVE_EPOLL) */
787
788#if defined(HAVE_EPOLL)
789
790PyDoc_STRVAR(select_epoll_poll__doc__,
791"poll($self, /, timeout=None, maxevents=-1)\n"
792"--\n"
793"\n"
794"Wait for events on the epoll file descriptor.\n"
795"\n"
796" timeout\n"
797" the maximum time to wait in seconds (as float);\n"
798" a timeout of None or -1 makes poll wait indefinitely\n"
799" maxevents\n"
800" the maximum number of events returned; -1 means no limit\n"
801"\n"
802"Returns a list containing any descriptors that have events to report,\n"
803"as a list of (fd, events) 2-tuples.");
804
805#define SELECT_EPOLL_POLL_METHODDEF \
806 {"poll", (PyCFunction)(void(*)(void))select_epoll_poll, METH_FASTCALL|METH_KEYWORDS, select_epoll_poll__doc__},
807
808static PyObject *
809select_epoll_poll_impl(pyEpoll_Object *self, PyObject *timeout_obj,
810 int maxevents);
811
812static PyObject *
813select_epoll_poll(pyEpoll_Object *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
814{
815 PyObject *return_value = NULL;
816 static const char * const _keywords[] = {"timeout", "maxevents", NULL};
817 static _PyArg_Parser _parser = {NULL, _keywords, "poll", 0};
818 PyObject *argsbuf[2];
819 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
820 PyObject *timeout_obj = Py_None;
821 int maxevents = -1;
822
823 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
824 if (!args) {
825 goto exit;
826 }
827 if (!noptargs) {
828 goto skip_optional_pos;
829 }
830 if (args[0]) {
831 timeout_obj = args[0];
832 if (!--noptargs) {
833 goto skip_optional_pos;
834 }
835 }
836 maxevents = _PyLong_AsInt(args[1]);
837 if (maxevents == -1 && PyErr_Occurred()) {
838 goto exit;
839 }
840skip_optional_pos:
841 return_value = select_epoll_poll_impl(self, timeout_obj, maxevents);
842
843exit:
844 return return_value;
845}
846
847#endif /* defined(HAVE_EPOLL) */
848
849#if defined(HAVE_EPOLL)
850
851PyDoc_STRVAR(select_epoll___enter____doc__,
852"__enter__($self, /)\n"
853"--\n"
854"\n");
855
856#define SELECT_EPOLL___ENTER___METHODDEF \
857 {"__enter__", (PyCFunction)select_epoll___enter__, METH_NOARGS, select_epoll___enter____doc__},
858
859static PyObject *
860select_epoll___enter___impl(pyEpoll_Object *self);
861
862static PyObject *
863select_epoll___enter__(pyEpoll_Object *self, PyObject *Py_UNUSED(ignored))
864{
865 return select_epoll___enter___impl(self);
866}
867
868#endif /* defined(HAVE_EPOLL) */
869
870#if defined(HAVE_EPOLL)
871
872PyDoc_STRVAR(select_epoll___exit____doc__,
873"__exit__($self, exc_type=None, exc_value=None, exc_tb=None, /)\n"
874"--\n"
875"\n");
876
877#define SELECT_EPOLL___EXIT___METHODDEF \
878 {"__exit__", (PyCFunction)(void(*)(void))select_epoll___exit__, METH_FASTCALL, select_epoll___exit____doc__},
879
880static PyObject *
881select_epoll___exit___impl(pyEpoll_Object *self, PyObject *exc_type,
882 PyObject *exc_value, PyObject *exc_tb);
883
884static PyObject *
885select_epoll___exit__(pyEpoll_Object *self, PyObject *const *args, Py_ssize_t nargs)
886{
887 PyObject *return_value = NULL;
888 PyObject *exc_type = Py_None;
889 PyObject *exc_value = Py_None;
890 PyObject *exc_tb = Py_None;
891
892 if (!_PyArg_CheckPositional("__exit__", nargs, 0, 3)) {
893 goto exit;
894 }
895 if (nargs < 1) {
896 goto skip_optional;
897 }
898 exc_type = args[0];
899 if (nargs < 2) {
900 goto skip_optional;
901 }
902 exc_value = args[1];
903 if (nargs < 3) {
904 goto skip_optional;
905 }
906 exc_tb = args[2];
907skip_optional:
908 return_value = select_epoll___exit___impl(self, exc_type, exc_value, exc_tb);
909
910exit:
911 return return_value;
912}
913
914#endif /* defined(HAVE_EPOLL) */
915
916#if defined(HAVE_KQUEUE)
917
918PyDoc_STRVAR(select_kqueue__doc__,
919"kqueue()\n"
920"--\n"
921"\n"
922"Kqueue syscall wrapper.\n"
923"\n"
924"For example, to start watching a socket for input:\n"
925">>> kq = kqueue()\n"
926">>> sock = socket()\n"
927">>> sock.connect((host, port))\n"
928">>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n"
929"\n"
930"To wait one second for it to become writeable:\n"
931">>> kq.control(None, 1, 1000)\n"
932"\n"
933"To stop listening:\n"
934">>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
935
936static PyObject *
937select_kqueue_impl(PyTypeObject *type);
938
939static PyObject *
940select_kqueue(PyTypeObject *type, PyObject *args, PyObject *kwargs)
941{
942 PyObject *return_value = NULL;
943
944 if ((type == _selectstate_by_type(type)->kqueue_queue_Type) &&
945 !_PyArg_NoPositional("kqueue", args)) {
946 goto exit;
947 }
948 if ((type == _selectstate_by_type(type)->kqueue_queue_Type) &&
949 !_PyArg_NoKeywords("kqueue", kwargs)) {
950 goto exit;
951 }
952 return_value = select_kqueue_impl(type);
953
954exit:
955 return return_value;
956}
957
958#endif /* defined(HAVE_KQUEUE) */
959
960#if defined(HAVE_KQUEUE)
961
962PyDoc_STRVAR(select_kqueue_close__doc__,
963"close($self, /)\n"
964"--\n"
965"\n"
966"Close the kqueue control file descriptor.\n"
967"\n"
968"Further operations on the kqueue object will raise an exception.");
969
970#define SELECT_KQUEUE_CLOSE_METHODDEF \
971 {"close", (PyCFunction)select_kqueue_close, METH_NOARGS, select_kqueue_close__doc__},
972
973static PyObject *
974select_kqueue_close_impl(kqueue_queue_Object *self);
975
976static PyObject *
977select_kqueue_close(kqueue_queue_Object *self, PyObject *Py_UNUSED(ignored))
978{
979 return select_kqueue_close_impl(self);
980}
981
982#endif /* defined(HAVE_KQUEUE) */
983
984#if defined(HAVE_KQUEUE)
985
986PyDoc_STRVAR(select_kqueue_fileno__doc__,
987"fileno($self, /)\n"
988"--\n"
989"\n"
990"Return the kqueue control file descriptor.");
991
992#define SELECT_KQUEUE_FILENO_METHODDEF \
993 {"fileno", (PyCFunction)select_kqueue_fileno, METH_NOARGS, select_kqueue_fileno__doc__},
994
995static PyObject *
996select_kqueue_fileno_impl(kqueue_queue_Object *self);
997
998static PyObject *
999select_kqueue_fileno(kqueue_queue_Object *self, PyObject *Py_UNUSED(ignored))
1000{
1001 return select_kqueue_fileno_impl(self);
1002}
1003
1004#endif /* defined(HAVE_KQUEUE) */
1005
1006#if defined(HAVE_KQUEUE)
1007
1008PyDoc_STRVAR(select_kqueue_fromfd__doc__,
1009"fromfd($type, fd, /)\n"
1010"--\n"
1011"\n"
1012"Create a kqueue object from a given control fd.");
1013
1014#define SELECT_KQUEUE_FROMFD_METHODDEF \
1015 {"fromfd", (PyCFunction)select_kqueue_fromfd, METH_O|METH_CLASS, select_kqueue_fromfd__doc__},
1016
1017static PyObject *
1018select_kqueue_fromfd_impl(PyTypeObject *type, int fd);
1019
1020static PyObject *
1021select_kqueue_fromfd(PyTypeObject *type, PyObject *arg)
1022{
1023 PyObject *return_value = NULL;
1024 int fd;
1025
1026 fd = _PyLong_AsInt(arg);
1027 if (fd == -1 && PyErr_Occurred()) {
1028 goto exit;
1029 }
1030 return_value = select_kqueue_fromfd_impl(type, fd);
1031
1032exit:
1033 return return_value;
1034}
1035
1036#endif /* defined(HAVE_KQUEUE) */
1037
1038#if defined(HAVE_KQUEUE)
1039
1040PyDoc_STRVAR(select_kqueue_control__doc__,
1041"control($self, changelist, maxevents, timeout=None, /)\n"
1042"--\n"
1043"\n"
1044"Calls the kernel kevent function.\n"
1045"\n"
1046" changelist\n"
1047" Must be an iterable of kevent objects describing the changes to be made\n"
1048" to the kernel\'s watch list or None.\n"
1049" maxevents\n"
1050" The maximum number of events that the kernel will return.\n"
1051" timeout\n"
1052" The maximum time to wait in seconds, or else None to wait forever.\n"
1053" This accepts floats for smaller timeouts, too.");
1054
1055#define SELECT_KQUEUE_CONTROL_METHODDEF \
1056 {"control", (PyCFunction)(void(*)(void))select_kqueue_control, METH_FASTCALL, select_kqueue_control__doc__},
1057
1058static PyObject *
1059select_kqueue_control_impl(kqueue_queue_Object *self, PyObject *changelist,
1060 int maxevents, PyObject *otimeout);
1061
1062static PyObject *
1063select_kqueue_control(kqueue_queue_Object *self, PyObject *const *args, Py_ssize_t nargs)
1064{
1065 PyObject *return_value = NULL;
1066 PyObject *changelist;
1067 int maxevents;
1068 PyObject *otimeout = Py_None;
1069
1070 if (!_PyArg_CheckPositional("control", nargs, 2, 3)) {
1071 goto exit;
1072 }
1073 changelist = args[0];
1074 maxevents = _PyLong_AsInt(args[1]);
1075 if (maxevents == -1 && PyErr_Occurred()) {
1076 goto exit;
1077 }
1078 if (nargs < 3) {
1079 goto skip_optional;
1080 }
1081 otimeout = args[2];
1082skip_optional:
1083 return_value = select_kqueue_control_impl(self, changelist, maxevents, otimeout);
1084
1085exit:
1086 return return_value;
1087}
1088
1089#endif /* defined(HAVE_KQUEUE) */
1090
1091#ifndef SELECT_POLL_REGISTER_METHODDEF
1092 #define SELECT_POLL_REGISTER_METHODDEF
1093#endif /* !defined(SELECT_POLL_REGISTER_METHODDEF) */
1094
1095#ifndef SELECT_POLL_MODIFY_METHODDEF
1096 #define SELECT_POLL_MODIFY_METHODDEF
1097#endif /* !defined(SELECT_POLL_MODIFY_METHODDEF) */
1098
1099#ifndef SELECT_POLL_UNREGISTER_METHODDEF
1100 #define SELECT_POLL_UNREGISTER_METHODDEF
1101#endif /* !defined(SELECT_POLL_UNREGISTER_METHODDEF) */
1102
1103#ifndef SELECT_POLL_POLL_METHODDEF
1104 #define SELECT_POLL_POLL_METHODDEF
1105#endif /* !defined(SELECT_POLL_POLL_METHODDEF) */
1106
1107#ifndef SELECT_DEVPOLL_REGISTER_METHODDEF
1108 #define SELECT_DEVPOLL_REGISTER_METHODDEF
1109#endif /* !defined(SELECT_DEVPOLL_REGISTER_METHODDEF) */
1110
1111#ifndef SELECT_DEVPOLL_MODIFY_METHODDEF
1112 #define SELECT_DEVPOLL_MODIFY_METHODDEF
1113#endif /* !defined(SELECT_DEVPOLL_MODIFY_METHODDEF) */
1114
1115#ifndef SELECT_DEVPOLL_UNREGISTER_METHODDEF
1116 #define SELECT_DEVPOLL_UNREGISTER_METHODDEF
1117#endif /* !defined(SELECT_DEVPOLL_UNREGISTER_METHODDEF) */
1118
1119#ifndef SELECT_DEVPOLL_POLL_METHODDEF
1120 #define SELECT_DEVPOLL_POLL_METHODDEF
1121#endif /* !defined(SELECT_DEVPOLL_POLL_METHODDEF) */
1122
1123#ifndef SELECT_DEVPOLL_CLOSE_METHODDEF
1124 #define SELECT_DEVPOLL_CLOSE_METHODDEF
1125#endif /* !defined(SELECT_DEVPOLL_CLOSE_METHODDEF) */
1126
1127#ifndef SELECT_DEVPOLL_FILENO_METHODDEF
1128 #define SELECT_DEVPOLL_FILENO_METHODDEF
1129#endif /* !defined(SELECT_DEVPOLL_FILENO_METHODDEF) */
1130
1131#ifndef SELECT_POLL_METHODDEF
1132 #define SELECT_POLL_METHODDEF
1133#endif /* !defined(SELECT_POLL_METHODDEF) */
1134
1135#ifndef SELECT_DEVPOLL_METHODDEF
1136 #define SELECT_DEVPOLL_METHODDEF
1137#endif /* !defined(SELECT_DEVPOLL_METHODDEF) */
1138
1139#ifndef SELECT_EPOLL_CLOSE_METHODDEF
1140 #define SELECT_EPOLL_CLOSE_METHODDEF
1141#endif /* !defined(SELECT_EPOLL_CLOSE_METHODDEF) */
1142
1143#ifndef SELECT_EPOLL_FILENO_METHODDEF
1144 #define SELECT_EPOLL_FILENO_METHODDEF
1145#endif /* !defined(SELECT_EPOLL_FILENO_METHODDEF) */
1146
1147#ifndef SELECT_EPOLL_FROMFD_METHODDEF
1148 #define SELECT_EPOLL_FROMFD_METHODDEF
1149#endif /* !defined(SELECT_EPOLL_FROMFD_METHODDEF) */
1150
1151#ifndef SELECT_EPOLL_REGISTER_METHODDEF
1152 #define SELECT_EPOLL_REGISTER_METHODDEF
1153#endif /* !defined(SELECT_EPOLL_REGISTER_METHODDEF) */
1154
1155#ifndef SELECT_EPOLL_MODIFY_METHODDEF
1156 #define SELECT_EPOLL_MODIFY_METHODDEF
1157#endif /* !defined(SELECT_EPOLL_MODIFY_METHODDEF) */
1158
1159#ifndef SELECT_EPOLL_UNREGISTER_METHODDEF
1160 #define SELECT_EPOLL_UNREGISTER_METHODDEF
1161#endif /* !defined(SELECT_EPOLL_UNREGISTER_METHODDEF) */
1162
1163#ifndef SELECT_EPOLL_POLL_METHODDEF
1164 #define SELECT_EPOLL_POLL_METHODDEF
1165#endif /* !defined(SELECT_EPOLL_POLL_METHODDEF) */
1166
1167#ifndef SELECT_EPOLL___ENTER___METHODDEF
1168 #define SELECT_EPOLL___ENTER___METHODDEF
1169#endif /* !defined(SELECT_EPOLL___ENTER___METHODDEF) */
1170
1171#ifndef SELECT_EPOLL___EXIT___METHODDEF
1172 #define SELECT_EPOLL___EXIT___METHODDEF
1173#endif /* !defined(SELECT_EPOLL___EXIT___METHODDEF) */
1174
1175#ifndef SELECT_KQUEUE_CLOSE_METHODDEF
1176 #define SELECT_KQUEUE_CLOSE_METHODDEF
1177#endif /* !defined(SELECT_KQUEUE_CLOSE_METHODDEF) */
1178
1179#ifndef SELECT_KQUEUE_FILENO_METHODDEF
1180 #define SELECT_KQUEUE_FILENO_METHODDEF
1181#endif /* !defined(SELECT_KQUEUE_FILENO_METHODDEF) */
1182
1183#ifndef SELECT_KQUEUE_FROMFD_METHODDEF
1184 #define SELECT_KQUEUE_FROMFD_METHODDEF
1185#endif /* !defined(SELECT_KQUEUE_FROMFD_METHODDEF) */
1186
1187#ifndef SELECT_KQUEUE_CONTROL_METHODDEF
1188 #define SELECT_KQUEUE_CONTROL_METHODDEF
1189#endif /* !defined(SELECT_KQUEUE_CONTROL_METHODDEF) */
1190/*[clinic end generated code: output=a8fc031269d28454 input=a9049054013a1b77]*/
1191