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/>.
29 #if PY_MAJOR_VERSION >= 3
30 #define PyLong_FromLong PyLong_FromLong
33 /* discard signature of 'func' in favour of 'target_sig' */
34 #define PY_DISCARD_FUNC_SIG(target_sig, func) (target_sig)(void(*)(void))func
36 void init_tevent(void);
40 struct tevent_context *ev;
41 } TeventContext_Object;
45 struct tevent_queue *queue;
50 struct tevent_req *req;
55 struct tevent_signal *signal;
56 } TeventSignal_Object;
60 struct tevent_timer *timer;
69 static PyTypeObject TeventContext_Type;
70 static PyTypeObject TeventReq_Type;
71 static PyTypeObject TeventQueue_Type;
72 static PyTypeObject TeventSignal_Type;
73 static PyTypeObject TeventTimer_Type;
74 static PyTypeObject TeventFd_Type;
76 static int py_context_init(struct tevent_context *ev)
82 static struct tevent_fd *py_add_fd(struct tevent_context *ev,
84 int fd, uint16_t flags,
85 tevent_fd_handler_t handler,
87 const char *handler_name,
94 static void py_set_fd_close_fn(struct tevent_fd *fde,
95 tevent_fd_close_fn_t close_fn)
100 static uint16_t py_get_fd_flags(struct tevent_fd *fde)
106 static void py_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
111 /* timed_event functions */
112 static struct tevent_timer *py_add_timer(struct tevent_context *ev,
114 struct timeval next_event,
115 tevent_timer_handler_t handler,
117 const char *handler_name,
118 const char *location)
124 /* immediate event functions */
125 static void py_schedule_immediate(struct tevent_immediate *im,
126 struct tevent_context *ev,
127 tevent_immediate_handler_t handler,
129 const char *handler_name,
130 const char *location)
135 /* signal functions */
136 static struct tevent_signal *py_add_signal(struct tevent_context *ev,
138 int signum, int sa_flags,
139 tevent_signal_handler_t handler,
141 const char *handler_name,
142 const char *location)
149 static int py_loop_once(struct tevent_context *ev, const char *location)
155 static int py_loop_wait(struct tevent_context *ev, const char *location)
161 const static struct tevent_ops py_tevent_ops = {
162 .context_init = py_context_init,
164 .set_fd_close_fn = py_set_fd_close_fn,
165 .get_fd_flags = py_get_fd_flags,
166 .set_fd_flags = py_set_fd_flags,
167 .add_timer = py_add_timer,
168 .schedule_immediate = py_schedule_immediate,
169 .add_signal = py_add_signal,
170 .loop_wait = py_loop_wait,
171 .loop_once = py_loop_once,
174 static PyObject *py_register_backend(PyObject *self, PyObject *args)
176 PyObject *name, *py_backend;
178 if (!PyArg_ParseTuple(args, "O", &py_backend))
181 name = PyObject_GetAttrString(py_backend, "name");
183 PyErr_SetNone(PyExc_AttributeError);
187 if (!PyUnicode_Check(name)) {
188 PyErr_SetNone(PyExc_TypeError);
193 if (!tevent_register_backend(PyUnicode_AsUTF8(name), &py_tevent_ops)) { /* FIXME: What to do with backend */
194 PyErr_SetNone(PyExc_RuntimeError);
204 static PyObject *py_tevent_context_reinitialise(TeventContext_Object *self,
205 PyObject *Py_UNUSED(ignored))
207 int ret = tevent_re_initialise(self->ev);
209 PyErr_SetNone(PyExc_RuntimeError);
215 static PyObject *py_tevent_queue_stop(TeventQueue_Object *self,
216 PyObject *Py_UNUSED(ignored))
218 tevent_queue_stop(self->queue);
222 static PyObject *py_tevent_queue_start(TeventQueue_Object *self,
223 PyObject *Py_UNUSED(ignored))
225 tevent_queue_start(self->queue);
229 static void py_queue_trigger(struct tevent_req *req, void *private_data)
231 PyObject *callback = private_data, *ret;
233 ret = PyObject_CallFunction(callback, discard_const_p(char, ""));
237 static PyObject *py_tevent_queue_add(TeventQueue_Object *self, PyObject *args)
239 TeventContext_Object *py_ev;
240 TeventReq_Object *py_req;
244 if (!PyArg_ParseTuple(args, "O!O!O",
245 &TeventContext_Type, &py_ev,
246 &TeventReq_Type, &py_req,
252 ret = tevent_queue_add(self->queue, py_ev->ev, py_req->req,
253 py_queue_trigger, trigger);
255 PyErr_SetString(PyExc_RuntimeError, "queue add failed");
263 static PyMethodDef py_tevent_queue_methods[] = {
264 { "stop", (PyCFunction)py_tevent_queue_stop,
267 { "start", (PyCFunction)py_tevent_queue_start,
270 { "add", (PyCFunction)py_tevent_queue_add, METH_VARARGS,
271 "S.add(ctx, req, trigger, baton)" },
275 static PyObject *py_tevent_context_wakeup_send(PyObject *self, PyObject *args)
282 static PyObject *py_tevent_context_loop_wait(TeventContext_Object *self,
283 PyObject *Py_UNUSED(ignored))
285 if (tevent_loop_wait(self->ev) != 0) {
286 PyErr_SetNone(PyExc_RuntimeError);
292 static PyObject *py_tevent_context_loop_once(TeventContext_Object *self,
293 PyObject *Py_UNUSED(ignored))
295 if (tevent_loop_once(self->ev) != 0) {
296 PyErr_SetNone(PyExc_RuntimeError);
302 static void py_tevent_signal_handler(struct tevent_context *ev,
303 struct tevent_signal *se,
309 PyObject *callback = (PyObject *)private_data, *ret;
311 ret = PyObject_CallFunction(callback, discard_const_p(char, "ii"), signum, count);
315 static void py_tevent_signal_dealloc(TeventSignal_Object *self)
317 talloc_free(self->signal);
321 static PyTypeObject TeventSignal_Type = {
322 .tp_name = "tevent.Signal",
323 .tp_basicsize = sizeof(TeventSignal_Object),
324 .tp_dealloc = (destructor)py_tevent_signal_dealloc,
325 .tp_flags = Py_TPFLAGS_DEFAULT,
328 static PyObject *py_tevent_context_add_signal(TeventContext_Object *self, PyObject *args)
330 int signum, sa_flags;
332 struct tevent_signal *sig;
333 TeventSignal_Object *ret;
335 if (!PyArg_ParseTuple(args, "iiO", &signum, &sa_flags, &handler))
339 sig = tevent_add_signal(self->ev, NULL, signum, sa_flags,
340 py_tevent_signal_handler, handler);
342 ret = PyObject_New(TeventSignal_Object, &TeventSignal_Type);
351 return (PyObject *)ret;
354 static void py_timer_handler(struct tevent_context *ev,
355 struct tevent_timer *te,
356 struct timeval current_time,
359 TeventTimer_Object *self = private_data;
362 ret = PyObject_CallFunction(self->callback, discard_const_p(char, "l"), te);
364 /* No Python stack to propagate exception to; just print traceback */
370 static void py_tevent_timer_dealloc(TeventTimer_Object *self)
373 talloc_free(self->timer);
375 Py_DECREF(self->callback);
379 static int py_tevent_timer_traverse(TeventTimer_Object *self, visitproc visit, void *arg)
381 Py_VISIT(self->callback);
385 static PyObject* py_tevent_timer_get_active(TeventTimer_Object *self,
386 PyObject *Py_UNUSED(ignored))
388 return PyBool_FromLong(self->timer != NULL);
391 struct PyGetSetDef py_tevent_timer_getset[] = {
393 .name = discard_const_p(char, "active"),
394 .get = (getter)py_tevent_timer_get_active,
395 .doc = discard_const_p(char, "true if the timer is scheduled to run"),
400 static PyTypeObject TeventTimer_Type = {
401 .tp_name = "tevent.Timer",
402 .tp_basicsize = sizeof(TeventTimer_Object),
403 .tp_dealloc = (destructor)py_tevent_timer_dealloc,
404 .tp_traverse = (traverseproc)py_tevent_timer_traverse,
405 .tp_getset = py_tevent_timer_getset,
406 .tp_flags = Py_TPFLAGS_DEFAULT,
409 struct TeventTimer_Object_ref {
410 TeventTimer_Object *obj;
413 static int TeventTimer_Object_ref_destructor(struct TeventTimer_Object_ref *ref)
415 ref->obj->timer = NULL;
420 static PyObject *py_tevent_context_add_timer_internal(TeventContext_Object *self,
421 struct timeval next_event,
426 * There are 5 pieces in play; two tevent contexts and 3 Python objects:
428 * - The tevent context
429 * - The Python context -- "self"
430 * - The Python timer (TeventTimer_Object) -- "ret"
431 * - The Python callback function -- "callback"
433 * We only use the Python context for getting the tevent context,
434 * afterwards it can be destroyed.
436 * The tevent context owns the tevent timer.
438 * The tevent timer holds a reference to the Python timer, so the Python
439 * timer must always outlive the tevent timer.
440 * The Python timer has a pointer to the tevent timer; a destructor is
441 * used to set this to NULL when the tevent timer is deallocated.
443 * The tevent timer can be deallocated in these cases:
444 * 1) when the context is destroyed
445 * 2) after the event fires
446 * Posssibly, API might be added to cancel (free the tevent timer).
448 * The Python timer holds a reference to the callback.
450 TeventTimer_Object *ret;
451 struct TeventTimer_Object_ref *ref;
453 ret = PyObject_New(TeventTimer_Object, &TeventTimer_Type);
459 ret->callback = callback;
460 ret->timer = tevent_add_timer(self->ev, NULL, next_event, py_timer_handler,
462 if (ret->timer == NULL) {
464 PyErr_SetString(PyExc_RuntimeError, "Could not initialize timer");
467 ref = talloc(ret->timer, struct TeventTimer_Object_ref);
469 talloc_free(ret->timer);
471 PyErr_SetString(PyExc_RuntimeError, "Could not initialize timer");
477 talloc_set_destructor(ref, TeventTimer_Object_ref_destructor);
479 return (PyObject *)ret;
482 static PyObject *py_tevent_context_add_timer(TeventContext_Object *self, PyObject *args)
484 struct timeval next_event;
487 if (!PyArg_ParseTuple(args, "dO", &secs, &callback)){
490 next_event.tv_sec = secs;
491 usecs = (secs - next_event.tv_sec) * 1000000.0;
492 next_event.tv_usec = usecs;
493 return py_tevent_context_add_timer_internal(self, next_event, callback);
496 static PyObject *py_tevent_context_add_timer_offset(TeventContext_Object *self, PyObject *args)
498 struct timeval next_event;
502 if (!PyArg_ParseTuple(args, "dO", &offset, &callback))
507 next_event = tevent_timeval_current_ofs(seconds, (int)(offset*1000000));
508 return py_tevent_context_add_timer_internal(self, next_event, callback);
511 static void py_fd_handler(struct tevent_context *ev,
512 struct tevent_fd *fde,
516 PyObject *callback = private_data, *ret;
518 ret = PyObject_CallFunction(callback, discard_const_p(char, "i"), flags);
522 static void py_tevent_fp_dealloc(TeventFd_Object *self)
524 talloc_free(self->fd);
528 static PyTypeObject TeventFd_Type = {
529 .tp_name = "tevent.Fd",
530 .tp_basicsize = sizeof(TeventFd_Object),
531 .tp_dealloc = (destructor)py_tevent_fp_dealloc,
532 .tp_flags = Py_TPFLAGS_DEFAULT,
535 static PyObject *py_tevent_context_add_fd(TeventContext_Object *self, PyObject *args)
539 struct tevent_fd *tfd;
540 TeventFd_Object *ret;
542 if (!PyArg_ParseTuple(args, "iiO", &fd, &flags, &handler))
545 tfd = tevent_add_fd(self->ev, NULL, fd, flags, py_fd_handler, handler);
547 PyErr_SetNone(PyExc_RuntimeError);
551 ret = PyObject_New(TeventFd_Object, &TeventFd_Type);
558 return (PyObject *)ret;
561 static PyMethodDef py_tevent_context_methods[] = {
562 { "reinitialise", (PyCFunction)py_tevent_context_reinitialise,
564 "S.reinitialise()" },
565 { "wakeup_send", (PyCFunction)py_tevent_context_wakeup_send,
566 METH_VARARGS, "S.wakeup_send(wakeup_time) -> req" },
567 { "loop_wait", (PyCFunction)py_tevent_context_loop_wait,
568 METH_NOARGS, "S.loop_wait()" },
569 { "loop_once", (PyCFunction)py_tevent_context_loop_once,
570 METH_NOARGS, "S.loop_once()" },
571 { "add_signal", (PyCFunction)py_tevent_context_add_signal,
572 METH_VARARGS, "S.add_signal(signum, sa_flags, handler) -> signal" },
573 { "add_timer", (PyCFunction)py_tevent_context_add_timer,
574 METH_VARARGS, "S.add_timer(next_event, handler) -> timer" },
575 { "add_timer_offset", (PyCFunction)py_tevent_context_add_timer_offset,
576 METH_VARARGS, "S.add_timer(offset_seconds, handler) -> timer" },
577 { "add_fd", (PyCFunction)py_tevent_context_add_fd,
578 METH_VARARGS, "S.add_fd(fd, flags, handler) -> fd" },
582 static PyObject *py_tevent_req_wakeup_recv(PyObject *self,
583 PyObject *Py_UNUSED(ignored))
589 static PyObject *py_tevent_req_received(PyObject *self,
590 PyObject *Py_UNUSED(ignored))
596 static PyObject *py_tevent_req_is_error(PyObject *self,
597 PyObject *Py_UNUSED(ignored))
603 static PyObject *py_tevent_req_poll(PyObject *self,
604 PyObject *Py_UNUSED(ignored))
610 static PyObject *py_tevent_req_is_in_progress(PyObject *self,
611 PyObject *Py_UNUSED(ignored))
617 static PyGetSetDef py_tevent_req_getsetters[] = {
619 .name = discard_const_p(char, "in_progress"),
620 .get = (getter)py_tevent_req_is_in_progress,
621 .doc = discard_const_p(char, "Whether the request is in progress"),
626 static PyObject *py_tevent_req_post(PyObject *self, PyObject *args)
632 static PyObject *py_tevent_req_set_error(PyObject *self, PyObject *args)
638 static PyObject *py_tevent_req_done(PyObject *self,
639 PyObject *Py_UNUSED(ignored))
645 static PyObject *py_tevent_req_notify_callback(PyObject *self,
646 PyObject *Py_UNUSED(ignored))
652 static PyObject *py_tevent_req_set_endtime(PyObject *self, PyObject *args)
658 static PyObject *py_tevent_req_cancel(TeventReq_Object *self,
659 PyObject *Py_UNUSED(ignored))
661 if (!tevent_req_cancel(self->req)) {
662 PyErr_SetNone(PyExc_RuntimeError);
668 static PyMethodDef py_tevent_req_methods[] = {
669 { "wakeup_recv", (PyCFunction)py_tevent_req_wakeup_recv,
672 { "received", (PyCFunction)py_tevent_req_received,
674 "Receive finished" },
675 { "is_error", (PyCFunction)py_tevent_req_is_error, METH_NOARGS,
676 "is_error() -> (error, state)" },
677 { "poll", (PyCFunction)py_tevent_req_poll, METH_VARARGS,
679 { "post", (PyCFunction)py_tevent_req_post, METH_VARARGS,
680 "post(ctx) -> req" },
681 { "set_error", (PyCFunction)py_tevent_req_set_error, METH_VARARGS,
682 "set_error(error)" },
683 { "done", (PyCFunction)py_tevent_req_done, METH_NOARGS,
685 { "notify_callback", (PyCFunction)py_tevent_req_notify_callback,
686 METH_NOARGS, "notify_callback()" },
687 { "set_endtime", (PyCFunction)py_tevent_req_set_endtime,
688 METH_VARARGS, "set_endtime(ctx, endtime)" },
689 { "cancel", (PyCFunction)py_tevent_req_cancel,
690 METH_NOARGS, "cancel()" },
694 static void py_tevent_req_dealloc(TeventReq_Object *self)
696 talloc_free(self->req);
700 static PyTypeObject TeventReq_Type = {
701 .tp_name = "tevent.Request",
702 .tp_basicsize = sizeof(TeventReq_Object),
703 .tp_methods = py_tevent_req_methods,
704 .tp_dealloc = (destructor)py_tevent_req_dealloc,
705 .tp_getset = py_tevent_req_getsetters,
706 /* FIXME: .tp_new = py_tevent_req_new, */
709 static PyObject *py_tevent_queue_get_length(TeventQueue_Object *self,
710 PyObject *Py_UNUSED(ignored))
712 return PyLong_FromLong(tevent_queue_length(self->queue));
715 static PyGetSetDef py_tevent_queue_getsetters[] = {
717 .name = discard_const_p(char, "length"),
718 .get = (getter)py_tevent_queue_get_length,
719 .doc = discard_const_p(char, "The number of elements in the queue."),
724 static void py_tevent_queue_dealloc(TeventQueue_Object *self)
726 talloc_free(self->queue);
730 static PyTypeObject TeventQueue_Type = {
731 .tp_name = "tevent.Queue",
732 .tp_basicsize = sizeof(TeventQueue_Object),
733 .tp_dealloc = (destructor)py_tevent_queue_dealloc,
734 .tp_flags = Py_TPFLAGS_DEFAULT,
735 .tp_getset = py_tevent_queue_getsetters,
736 .tp_methods = py_tevent_queue_methods,
739 static PyObject *py_tevent_context_signal_support(PyObject *_self,
740 PyObject *Py_UNUSED(ignored))
742 TeventContext_Object *self = (TeventContext_Object *)_self;
743 return PyBool_FromLong(tevent_signal_support(self->ev));
746 static PyGetSetDef py_tevent_context_getsetters[] = {
748 .name = discard_const_p(char, "signal_support"),
749 .get = PY_DISCARD_FUNC_SIG(getter,
750 py_tevent_context_signal_support),
751 .doc = discard_const_p(char, "if this platform and tevent context support signal handling"),
756 static void py_tevent_context_dealloc(TeventContext_Object *self)
758 talloc_free(self->ev);
762 static PyObject *py_tevent_context_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
764 const char * const kwnames[] = { "name", NULL };
766 struct tevent_context *ev;
767 TeventContext_Object *ret;
769 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &name))
773 ev = tevent_context_init(NULL);
775 ev = tevent_context_init_byname(NULL, name);
779 PyErr_SetNone(PyExc_RuntimeError);
783 ret = PyObject_New(TeventContext_Object, type);
791 return (PyObject *)ret;
794 static PyTypeObject TeventContext_Type = {
795 .tp_name = "tevent.Context",
796 .tp_new = py_tevent_context_new,
797 .tp_basicsize = sizeof(TeventContext_Object),
798 .tp_dealloc = (destructor)py_tevent_context_dealloc,
799 .tp_methods = py_tevent_context_methods,
800 .tp_getset = py_tevent_context_getsetters,
801 .tp_flags = Py_TPFLAGS_DEFAULT,
804 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
807 if (!PyArg_ParseTuple(args, "s", &backend_name))
810 tevent_set_default_backend(backend_name);
815 static PyObject *py_backend_list(PyObject *self,
816 PyObject *Py_UNUSED(ignored))
818 PyObject *ret = NULL;
819 PyObject *string = NULL;
821 const char **backends = NULL;
828 backends = tevent_backend_list(NULL);
829 if (backends == NULL) {
830 PyErr_SetNone(PyExc_RuntimeError);
833 for (i = 0; backends[i]; i++) {
834 string = PyUnicode_FromString(backends[i]);
838 result = PyList_Append(ret, string);
846 talloc_free(backends);
853 talloc_free(backends);
857 static PyMethodDef tevent_methods[] = {
858 { "register_backend", (PyCFunction)py_register_backend, METH_VARARGS,
859 "register_backend(backend)" },
860 { "set_default_backend", (PyCFunction)py_set_default_backend,
861 METH_VARARGS, "set_default_backend(backend)" },
862 { "backend_list", (PyCFunction)py_backend_list,
863 METH_NOARGS, "backend_list() -> list" },
867 #define MODULE_DOC PyDoc_STR("Python wrapping of talloc-maintained objects.")
869 #if PY_MAJOR_VERSION >= 3
870 static struct PyModuleDef moduledef = {
871 PyModuleDef_HEAD_INIT,
875 .m_methods = tevent_methods,
879 PyObject * module_init(void);
880 PyObject * module_init(void)
884 if (PyType_Ready(&TeventContext_Type) < 0)
887 if (PyType_Ready(&TeventQueue_Type) < 0)
890 if (PyType_Ready(&TeventReq_Type) < 0)
893 if (PyType_Ready(&TeventSignal_Type) < 0)
896 if (PyType_Ready(&TeventTimer_Type) < 0)
899 if (PyType_Ready(&TeventFd_Type) < 0)
902 #if PY_MAJOR_VERSION >= 3
903 m = PyModule_Create(&moduledef);
905 m = Py_InitModule3("_tevent", tevent_methods, MODULE_DOC);
910 Py_INCREF(&TeventContext_Type);
911 PyModule_AddObject(m, "Context", (PyObject *)&TeventContext_Type);
913 Py_INCREF(&TeventQueue_Type);
914 PyModule_AddObject(m, "Queue", (PyObject *)&TeventQueue_Type);
916 Py_INCREF(&TeventReq_Type);
917 PyModule_AddObject(m, "Request", (PyObject *)&TeventReq_Type);
919 Py_INCREF(&TeventSignal_Type);
920 PyModule_AddObject(m, "Signal", (PyObject *)&TeventSignal_Type);
922 Py_INCREF(&TeventTimer_Type);
923 PyModule_AddObject(m, "Timer", (PyObject *)&TeventTimer_Type);
925 Py_INCREF(&TeventFd_Type);
926 PyModule_AddObject(m, "Fd", (PyObject *)&TeventFd_Type);
928 PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
933 #if PY_MAJOR_VERSION >= 3
934 PyMODINIT_FUNC PyInit__tevent(void);
935 PyMODINIT_FUNC PyInit__tevent(void)
937 return module_init();
940 void init_tevent(void);
941 void init_tevent(void)