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