2 Unix SMB/CIFS implementation.
3 Python bindings for tevent
5 Copyright (C) Jelmer Vernooij 2010
7 ** NOTE! The following LGPL license applies to the tevent
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 void init_tevent(void);
32 struct tevent_context *ev;
33 } TeventContext_Object;
37 struct tevent_queue *queue;
42 struct tevent_req *req;
47 struct tevent_signal *signal;
48 } TeventSignal_Object;
52 struct tevent_timer *timer;
60 staticforward PyTypeObject TeventContext_Type;
61 staticforward PyTypeObject TeventReq_Type;
62 staticforward PyTypeObject TeventQueue_Type;
63 staticforward PyTypeObject TeventSignal_Type;
64 staticforward PyTypeObject TeventTimer_Type;
65 staticforward PyTypeObject TeventFd_Type;
67 static int py_context_init(struct tevent_context *ev)
73 static struct tevent_fd *py_add_fd(struct tevent_context *ev,
75 int fd, uint16_t flags,
76 tevent_fd_handler_t handler,
78 const char *handler_name,
85 static void py_set_fd_close_fn(struct tevent_fd *fde,
86 tevent_fd_close_fn_t close_fn)
91 static uint16_t py_get_fd_flags(struct tevent_fd *fde)
97 static void py_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
102 /* timed_event functions */
103 static struct tevent_timer *py_add_timer(struct tevent_context *ev,
105 struct timeval next_event,
106 tevent_timer_handler_t handler,
108 const char *handler_name,
109 const char *location)
115 /* immediate event functions */
116 static void py_schedule_immediate(struct tevent_immediate *im,
117 struct tevent_context *ev,
118 tevent_immediate_handler_t handler,
120 const char *handler_name,
121 const char *location)
126 /* signal functions */
127 static struct tevent_signal *py_add_signal(struct tevent_context *ev,
129 int signum, int sa_flags,
130 tevent_signal_handler_t handler,
132 const char *handler_name,
133 const char *location)
140 static int py_loop_once(struct tevent_context *ev, const char *location)
146 static int py_loop_wait(struct tevent_context *ev, const char *location)
152 const static struct tevent_ops py_tevent_ops = {
153 .context_init = py_context_init,
155 .set_fd_close_fn = py_set_fd_close_fn,
156 .get_fd_flags = py_get_fd_flags,
157 .set_fd_flags = py_set_fd_flags,
158 .add_timer = py_add_timer,
159 .schedule_immediate = py_schedule_immediate,
160 .add_signal = py_add_signal,
161 .loop_wait = py_loop_wait,
162 .loop_once = py_loop_once,
165 static PyObject *py_register_backend(PyObject *self, PyObject *args)
167 PyObject *name, *py_backend;
169 if (!PyArg_ParseTuple(args, "O", &py_backend))
172 name = PyObject_GetAttrString(py_backend, "name");
174 PyErr_SetNone(PyExc_AttributeError);
178 if (!PyString_Check(name)) {
179 PyErr_SetNone(PyExc_TypeError);
183 if (!tevent_register_backend(PyString_AsString(name), &py_tevent_ops)) { /* FIXME: What to do with backend */
184 PyErr_SetNone(PyExc_RuntimeError);
191 static PyObject *py_tevent_context_reinitialise(TeventContext_Object *self)
193 int ret = tevent_re_initialise(self->ev);
195 PyErr_SetNone(PyExc_RuntimeError);
201 static PyObject *py_tevent_queue_stop(TeventQueue_Object *self)
203 tevent_queue_stop(self->queue);
207 static PyObject *py_tevent_queue_start(TeventQueue_Object *self)
209 tevent_queue_start(self->queue);
213 static void py_queue_trigger(struct tevent_req *req, void *private_data)
215 PyObject *callback = private_data, *ret;
217 ret = PyObject_CallFunction(callback, "");
221 static PyObject *py_tevent_queue_add(TeventQueue_Object *self, PyObject *args)
223 TeventContext_Object *py_ev;
224 TeventReq_Object *py_req;
228 if (!PyArg_ParseTuple(args, "O!O!O",
229 &TeventContext_Type, &py_ev,
230 &TeventReq_Type, &py_req,
236 ret = tevent_queue_add(self->queue, py_ev->ev, py_req->req,
237 py_queue_trigger, trigger);
239 PyErr_SetString(PyExc_RuntimeError, "queue add failed");
247 static PyMethodDef py_tevent_queue_methods[] = {
248 { "stop", (PyCFunction)py_tevent_queue_stop, METH_NOARGS,
250 { "start", (PyCFunction)py_tevent_queue_start, METH_NOARGS,
252 { "add", (PyCFunction)py_tevent_queue_add, METH_VARARGS,
253 "S.add(ctx, req, trigger, baton)" },
257 static PyObject *py_tevent_context_wakeup_send(PyObject *self, PyObject *args)
264 static PyObject *py_tevent_context_loop_wait(TeventContext_Object *self)
266 if (tevent_loop_wait(self->ev) != 0) {
267 PyErr_SetNone(PyExc_RuntimeError);
273 static PyObject *py_tevent_context_loop_once(TeventContext_Object *self)
275 if (tevent_loop_once(self->ev) != 0) {
276 PyErr_SetNone(PyExc_RuntimeError);
282 #ifdef TEVENT_DEPRECATED
283 static bool py_tevent_finished(PyObject *callback)
288 py_ret = PyObject_CallFunction(callback, "");
291 ret = PyObject_IsTrue(py_ret);
296 static PyObject *py_tevent_context_loop_until(TeventContext_Object *self, PyObject *args)
299 if (!PyArg_ParseTuple(args, "O", &callback))
302 if (tevent_loop_until(self->ev, py_tevent_finished, callback) != 0) {
303 PyErr_SetNone(PyExc_RuntimeError);
307 if (PyErr_Occurred())
314 static void py_tevent_signal_handler(struct tevent_context *ev,
315 struct tevent_signal *se,
321 PyObject *callback = (PyObject *)private_data, *ret;
323 ret = PyObject_CallFunction(callback, "ii", signum, count);
327 static void py_tevent_signal_dealloc(TeventSignal_Object *self)
329 talloc_free(self->signal);
333 static PyTypeObject TeventSignal_Type = {
334 .tp_name = "tevent.Signal",
335 .tp_basicsize = sizeof(TeventSignal_Object),
336 .tp_dealloc = (destructor)py_tevent_signal_dealloc,
337 .tp_flags = Py_TPFLAGS_DEFAULT,
340 static PyObject *py_tevent_context_add_signal(TeventContext_Object *self, PyObject *args)
342 int signum, sa_flags;
344 struct tevent_signal *sig;
345 TeventSignal_Object *ret;
347 if (!PyArg_ParseTuple(args, "iiO", &signum, &sa_flags, &handler))
351 sig = tevent_add_signal(self->ev, NULL, signum, sa_flags,
352 py_tevent_signal_handler, handler);
354 ret = PyObject_New(TeventSignal_Object, &TeventSignal_Type);
363 return (PyObject *)ret;
366 static void py_timer_handler(struct tevent_context *ev,
367 struct tevent_timer *te,
368 struct timeval current_time,
371 PyObject *callback = private_data, *ret;
372 ret = PyObject_CallFunction(callback, "l", te);
376 static PyObject *py_tevent_context_add_timer(TeventContext_Object *self, PyObject *args)
378 TeventTimer_Object *ret;
379 struct timeval next_event;
380 struct tevent_timer *timer;
382 if (!PyArg_ParseTuple(args, "lO", &next_event, &handler))
385 timer = tevent_add_timer(self->ev, NULL, next_event, py_timer_handler,
388 PyErr_SetNone(PyExc_RuntimeError);
392 ret = PyObject_New(TeventTimer_Object, &TeventTimer_Type);
400 return (PyObject *)ret;
403 static void py_fd_handler(struct tevent_context *ev,
404 struct tevent_fd *fde,
408 PyObject *callback = private_data, *ret;
410 ret = PyObject_CallFunction(callback, "i", flags);
414 static PyObject *py_tevent_context_add_fd(TeventContext_Object *self, PyObject *args)
418 struct tevent_fd *tfd;
419 TeventFd_Object *ret;
421 if (!PyArg_ParseTuple(args, "iiO", &fd, &flags, &handler))
424 tfd = tevent_add_fd(self->ev, NULL, fd, flags, py_fd_handler, handler);
426 PyErr_SetNone(PyExc_RuntimeError);
430 ret = PyObject_New(TeventFd_Object, &TeventFd_Type);
437 return (PyObject *)ret;
440 #ifdef TEVENT_DEPRECATED
441 static PyObject *py_tevent_context_set_allow_nesting(TeventContext_Object *self)
443 tevent_loop_allow_nesting(self->ev);
448 static PyMethodDef py_tevent_context_methods[] = {
449 { "reinitialise", (PyCFunction)py_tevent_context_reinitialise, METH_NOARGS,
450 "S.reinitialise()" },
451 { "wakeup_send", (PyCFunction)py_tevent_context_wakeup_send,
452 METH_VARARGS, "S.wakeup_send(wakeup_time) -> req" },
453 { "loop_wait", (PyCFunction)py_tevent_context_loop_wait,
454 METH_NOARGS, "S.loop_wait()" },
455 { "loop_once", (PyCFunction)py_tevent_context_loop_once,
456 METH_NOARGS, "S.loop_once()" },
457 #ifdef TEVENT_DEPRECATED
458 { "loop_until", (PyCFunction)py_tevent_context_loop_until,
459 METH_VARARGS, "S.loop_until(callback)" },
461 { "add_signal", (PyCFunction)py_tevent_context_add_signal,
462 METH_VARARGS, "S.add_signal(signum, sa_flags, handler) -> signal" },
463 { "add_timer", (PyCFunction)py_tevent_context_add_timer,
464 METH_VARARGS, "S.add_timer(next_event, handler) -> timer" },
465 { "add_fd", (PyCFunction)py_tevent_context_add_fd,
466 METH_VARARGS, "S.add_fd(fd, flags, handler) -> fd" },
467 #ifdef TEVENT_DEPRECATED
468 { "allow_nesting", (PyCFunction)py_tevent_context_set_allow_nesting,
469 METH_NOARGS, "Whether to allow nested tevent loops." },
474 static PyObject *py_tevent_req_wakeup_recv(PyObject *self)
480 static PyObject *py_tevent_req_received(PyObject *self)
486 static PyObject *py_tevent_req_is_error(PyObject *self)
492 static PyObject *py_tevent_req_poll(PyObject *self)
498 static PyObject *py_tevent_req_is_in_progress(PyObject *self)
504 static PyGetSetDef py_tevent_req_getsetters[] = {
505 { "in_progress", (getter)py_tevent_req_is_in_progress, NULL,
506 "Whether the request is in progress" },
510 static PyObject *py_tevent_req_post(PyObject *self, PyObject *args)
516 static PyObject *py_tevent_req_set_error(PyObject *self, PyObject *args)
522 static PyObject *py_tevent_req_done(PyObject *self)
528 static PyObject *py_tevent_req_notify_callback(PyObject *self)
534 static PyObject *py_tevent_req_set_endtime(PyObject *self, PyObject *args)
540 static PyObject *py_tevent_req_cancel(TeventReq_Object *self)
542 if (!tevent_req_cancel(self->req)) {
543 PyErr_SetNone(PyExc_RuntimeError);
549 static PyMethodDef py_tevent_req_methods[] = {
550 { "wakeup_recv", (PyCFunction)py_tevent_req_wakeup_recv, METH_NOARGS,
552 { "received", (PyCFunction)py_tevent_req_received, METH_NOARGS,
553 "Receive finished" },
554 { "is_error", (PyCFunction)py_tevent_req_is_error, METH_NOARGS,
555 "is_error() -> (error, state)" },
556 { "poll", (PyCFunction)py_tevent_req_poll, METH_VARARGS,
558 { "post", (PyCFunction)py_tevent_req_post, METH_VARARGS,
559 "post(ctx) -> req" },
560 { "set_error", (PyCFunction)py_tevent_req_set_error, METH_VARARGS,
561 "set_error(error)" },
562 { "done", (PyCFunction)py_tevent_req_done, METH_NOARGS,
564 { "notify_callback", (PyCFunction)py_tevent_req_notify_callback,
565 METH_NOARGS, "notify_callback()" },
566 { "set_endtime", (PyCFunction)py_tevent_req_set_endtime,
567 METH_VARARGS, "set_endtime(ctx, endtime)" },
568 { "cancel", (PyCFunction)py_tevent_req_cancel,
569 METH_NOARGS, "cancel()" },
573 static void py_tevent_req_dealloc(TeventReq_Object *self)
575 talloc_free(self->req);
579 static PyTypeObject TeventReq_Type = {
580 .tp_name = "tevent.Request",
581 .tp_basicsize = sizeof(TeventReq_Object),
582 .tp_methods = py_tevent_req_methods,
583 .tp_dealloc = (destructor)py_tevent_req_dealloc,
584 .tp_getset = py_tevent_req_getsetters,
585 /* FIXME: .tp_new = py_tevent_req_new, */
588 static PyObject *py_tevent_queue_get_length(TeventQueue_Object *self)
590 return PyInt_FromLong(tevent_queue_length(self->queue));
593 static PyGetSetDef py_tevent_queue_getsetters[] = {
594 { "length", (getter)py_tevent_queue_get_length,
595 NULL, "The number of elements in the queue." },
599 static void py_tevent_queue_dealloc(TeventQueue_Object *self)
601 talloc_free(self->queue);
605 static PyTypeObject TeventQueue_Type = {
606 .tp_name = "tevent.Queue",
607 .tp_basicsize = sizeof(TeventQueue_Object),
608 .tp_dealloc = (destructor)py_tevent_queue_dealloc,
609 .tp_flags = Py_TPFLAGS_DEFAULT,
610 .tp_getset = py_tevent_queue_getsetters,
611 .tp_methods = py_tevent_queue_methods,
614 static PyObject *py_tevent_context_signal_support(PyObject *_self)
616 TeventContext_Object *self = (TeventContext_Object *)_self;
617 return PyBool_FromLong(tevent_signal_support(self->ev));
620 static PyGetSetDef py_tevent_context_getsetters[] = {
621 { "signal_support", (getter)py_tevent_context_signal_support,
622 NULL, "if this platform and tevent context support signal handling" },
626 static void py_tevent_context_dealloc(TeventContext_Object *self)
628 talloc_free(self->ev);
632 static PyObject *py_tevent_context_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
634 const char * const kwnames[] = { "name", NULL };
636 struct tevent_context *ev;
637 TeventContext_Object *ret;
639 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwnames, &name))
643 ev = tevent_context_init(NULL);
645 ev = tevent_context_init_byname(NULL, name);
649 PyErr_SetNone(PyExc_RuntimeError);
653 ret = PyObject_New(TeventContext_Object, type);
661 return (PyObject *)ret;
664 static PyTypeObject TeventContext_Type = {
665 .tp_name = "tevent.Context",
666 .tp_new = py_tevent_context_new,
667 .tp_basicsize = sizeof(TeventContext_Object),
668 .tp_dealloc = (destructor)py_tevent_context_dealloc,
669 .tp_methods = py_tevent_context_methods,
670 .tp_getset = py_tevent_context_getsetters,
671 .tp_flags = Py_TPFLAGS_DEFAULT,
674 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
677 if (!PyArg_ParseTuple(args, "s", &backend_name))
680 tevent_set_default_backend(backend_name);
685 static PyObject *py_backend_list(PyObject *self)
689 const char **backends;
696 backends = tevent_backend_list(NULL);
697 if (backends == NULL) {
698 PyErr_SetNone(PyExc_RuntimeError);
702 for (i = 0; backends[i]; i++) {
703 PyList_Append(ret, PyString_FromString(backends[i]));
706 talloc_free(backends);
711 static PyMethodDef tevent_methods[] = {
712 { "register_backend", (PyCFunction)py_register_backend, METH_VARARGS,
713 "register_backend(backend)" },
714 { "set_default_backend", (PyCFunction)py_set_default_backend,
715 METH_VARARGS, "set_default_backend(backend)" },
716 { "backend_list", (PyCFunction)py_backend_list,
717 METH_NOARGS, "backend_list() -> list" },
721 void init_tevent(void)
725 if (PyType_Ready(&TeventContext_Type) < 0)
728 if (PyType_Ready(&TeventQueue_Type) < 0)
731 if (PyType_Ready(&TeventReq_Type) < 0)
734 if (PyType_Ready(&TeventSignal_Type) < 0)
737 if (PyType_Ready(&TeventTimer_Type) < 0)
740 if (PyType_Ready(&TeventFd_Type) < 0)
743 m = Py_InitModule3("_tevent", tevent_methods, "Tevent integration for twisted.");
747 Py_INCREF(&TeventContext_Type);
748 PyModule_AddObject(m, "Context", (PyObject *)&TeventContext_Type);
750 Py_INCREF(&TeventQueue_Type);
751 PyModule_AddObject(m, "Queue", (PyObject *)&TeventQueue_Type);
753 Py_INCREF(&TeventReq_Type);
754 PyModule_AddObject(m, "Request", (PyObject *)&TeventReq_Type);
756 Py_INCREF(&TeventSignal_Type);
757 PyModule_AddObject(m, "Signal", (PyObject *)&TeventSignal_Type);
759 Py_INCREF(&TeventTimer_Type);
760 PyModule_AddObject(m, "Timer", (PyObject *)&TeventTimer_Type);
762 Py_INCREF(&TeventFd_Type);
763 PyModule_AddObject(m, "Fd", (PyObject *)&TeventFd_Type);
765 PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));