librpc: Remove unused RPC_NDR_SCERPC
[Samba.git] / lib / tevent / pytevent.c
blob73a9bd736e7473a264bd5dfb8f0d9f7d478d0d5a
1 /*
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
9 ** under the LGPL
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/>.
25 #include <Python.h>
26 #include "replace.h"
27 #include <tevent.h>
29 #if PY_MAJOR_VERSION >= 3
30 #define PyInt_FromLong PyLong_FromLong
31 #endif
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);
38 typedef struct {
39 PyObject_HEAD
40 struct tevent_context *ev;
41 } TeventContext_Object;
43 typedef struct {
44 PyObject_HEAD
45 struct tevent_queue *queue;
46 } TeventQueue_Object;
48 typedef struct {
49 PyObject_HEAD
50 struct tevent_req *req;
51 } TeventReq_Object;
53 typedef struct {
54 PyObject_HEAD
55 struct tevent_signal *signal;
56 } TeventSignal_Object;
58 typedef struct {
59 PyObject_HEAD
60 struct tevent_timer *timer;
61 PyObject *callback;
62 } TeventTimer_Object;
64 typedef struct {
65 PyObject_HEAD
66 struct tevent_fd *fd;
67 } TeventFd_Object;
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)
78 /* FIXME */
79 return 0;
82 static struct tevent_fd *py_add_fd(struct tevent_context *ev,
83 TALLOC_CTX *mem_ctx,
84 int fd, uint16_t flags,
85 tevent_fd_handler_t handler,
86 void *private_data,
87 const char *handler_name,
88 const char *location)
90 /* FIXME */
91 return NULL;
94 static void py_set_fd_close_fn(struct tevent_fd *fde,
95 tevent_fd_close_fn_t close_fn)
97 /* FIXME */
100 static uint16_t py_get_fd_flags(struct tevent_fd *fde)
102 /* FIXME */
103 return 0;
106 static void py_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
108 /* FIXME */
111 /* timed_event functions */
112 static struct tevent_timer *py_add_timer(struct tevent_context *ev,
113 TALLOC_CTX *mem_ctx,
114 struct timeval next_event,
115 tevent_timer_handler_t handler,
116 void *private_data,
117 const char *handler_name,
118 const char *location)
120 /* FIXME */
121 return NULL;
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,
128 void *private_data,
129 const char *handler_name,
130 const char *location)
132 /* FIXME */
135 /* signal functions */
136 static struct tevent_signal *py_add_signal(struct tevent_context *ev,
137 TALLOC_CTX *mem_ctx,
138 int signum, int sa_flags,
139 tevent_signal_handler_t handler,
140 void *private_data,
141 const char *handler_name,
142 const char *location)
144 /* FIXME */
145 return NULL;
148 /* loop functions */
149 static int py_loop_once(struct tevent_context *ev, const char *location)
151 /* FIXME */
152 return 0;
155 static int py_loop_wait(struct tevent_context *ev, const char *location)
157 /* FIXME */
158 return 0;
161 const static struct tevent_ops py_tevent_ops = {
162 .context_init = py_context_init,
163 .add_fd = py_add_fd,
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))
179 return NULL;
181 name = PyObject_GetAttrString(py_backend, "name");
182 if (name == NULL) {
183 PyErr_SetNone(PyExc_AttributeError);
184 return NULL;
187 if (!PyUnicode_Check(name)) {
188 PyErr_SetNone(PyExc_TypeError);
189 Py_DECREF(name);
190 return NULL;
193 if (!tevent_register_backend(PyUnicode_AsUTF8(name), &py_tevent_ops)) { /* FIXME: What to do with backend */
194 PyErr_SetNone(PyExc_RuntimeError);
195 Py_DECREF(name);
196 return NULL;
199 Py_DECREF(name);
201 Py_RETURN_NONE;
204 static PyObject *py_tevent_context_reinitialise(TeventContext_Object *self,
205 PyObject *Py_UNUSED(ignored))
207 int ret = tevent_re_initialise(self->ev);
208 if (ret != 0) {
209 PyErr_SetNone(PyExc_RuntimeError);
210 return NULL;
212 Py_RETURN_NONE;
215 static PyObject *py_tevent_queue_stop(TeventQueue_Object *self,
216 PyObject *Py_UNUSED(ignored))
218 tevent_queue_stop(self->queue);
219 Py_RETURN_NONE;
222 static PyObject *py_tevent_queue_start(TeventQueue_Object *self,
223 PyObject *Py_UNUSED(ignored))
225 tevent_queue_start(self->queue);
226 Py_RETURN_NONE;
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, ""));
234 Py_XDECREF(ret);
237 static PyObject *py_tevent_queue_add(TeventQueue_Object *self, PyObject *args)
239 TeventContext_Object *py_ev;
240 TeventReq_Object *py_req;
241 PyObject *trigger;
242 bool ret;
244 if (!PyArg_ParseTuple(args, "O!O!O",
245 &TeventContext_Type, &py_ev,
246 &TeventReq_Type, &py_req,
247 &trigger))
248 return NULL;
250 Py_INCREF(trigger);
252 ret = tevent_queue_add(self->queue, py_ev->ev, py_req->req,
253 py_queue_trigger, trigger);
254 if (!ret) {
255 PyErr_SetString(PyExc_RuntimeError, "queue add failed");
256 Py_DECREF(trigger);
257 return NULL;
260 Py_RETURN_NONE;
263 static PyMethodDef py_tevent_queue_methods[] = {
264 { "stop", (PyCFunction)py_tevent_queue_stop,
265 METH_NOARGS,
266 "S.stop()" },
267 { "start", (PyCFunction)py_tevent_queue_start,
268 METH_NOARGS,
269 "S.start()" },
270 { "add", (PyCFunction)py_tevent_queue_add, METH_VARARGS,
271 "S.add(ctx, req, trigger, baton)" },
272 { NULL },
275 static PyObject *py_tevent_context_wakeup_send(PyObject *self, PyObject *args)
277 /* FIXME */
279 Py_RETURN_NONE;
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);
287 return NULL;
289 Py_RETURN_NONE;
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);
297 return NULL;
299 Py_RETURN_NONE;
302 static void py_tevent_signal_handler(struct tevent_context *ev,
303 struct tevent_signal *se,
304 int signum,
305 int count,
306 void *siginfo,
307 void *private_data)
309 PyObject *callback = (PyObject *)private_data, *ret;
311 ret = PyObject_CallFunction(callback, discard_const_p(char, "ii"), signum, count);
312 Py_XDECREF(ret);
315 static void py_tevent_signal_dealloc(TeventSignal_Object *self)
317 talloc_free(self->signal);
318 PyObject_Del(self);
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;
331 PyObject *handler;
332 struct tevent_signal *sig;
333 TeventSignal_Object *ret;
335 if (!PyArg_ParseTuple(args, "iiO", &signum, &sa_flags, &handler))
336 return NULL;
338 Py_INCREF(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);
343 if (ret == NULL) {
344 PyErr_NoMemory();
345 talloc_free(sig);
346 return NULL;
349 ret->signal = sig;
351 return (PyObject *)ret;
354 static void py_timer_handler(struct tevent_context *ev,
355 struct tevent_timer *te,
356 struct timeval current_time,
357 void *private_data)
359 TeventTimer_Object *self = private_data;
360 PyObject *ret;
362 ret = PyObject_CallFunction(self->callback, discard_const_p(char, "l"), te);
363 if (ret == NULL) {
364 /* No Python stack to propagate exception to; just print traceback */
365 PyErr_PrintEx(0);
367 Py_XDECREF(ret);
370 static void py_tevent_timer_dealloc(TeventTimer_Object *self)
372 if (self->timer) {
373 talloc_free(self->timer);
375 Py_DECREF(self->callback);
376 PyObject_Del(self);
379 static int py_tevent_timer_traverse(TeventTimer_Object *self, visitproc visit, void *arg)
381 Py_VISIT(self->callback);
382 return 0;
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"),
397 {NULL},
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;
416 Py_DECREF(ref->obj);
417 return 0;
420 static PyObject *py_tevent_context_add_timer_internal(TeventContext_Object *self,
421 struct timeval next_event,
422 PyObject *callback)
424 /* Ownership notes:
426 * There are 5 pieces in play; two tevent contexts and 3 Python objects:
427 * - The tevent timer
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);
454 if (ret == NULL) {
455 PyErr_NoMemory();
456 return NULL;
458 Py_INCREF(callback);
459 ret->callback = callback;
460 ret->timer = tevent_add_timer(self->ev, NULL, next_event, py_timer_handler,
461 ret);
462 if (ret->timer == NULL) {
463 Py_DECREF(ret);
464 PyErr_SetString(PyExc_RuntimeError, "Could not initialize timer");
465 return NULL;
467 ref = talloc(ret->timer, struct TeventTimer_Object_ref);
468 if (ref == NULL) {
469 talloc_free(ret->timer);
470 Py_DECREF(ret);
471 PyErr_SetString(PyExc_RuntimeError, "Could not initialize timer");
472 return NULL;
474 Py_INCREF(ret);
475 ref->obj = ret;
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;
485 PyObject *callback;
486 double secs, usecs;
487 if (!PyArg_ParseTuple(args, "dO", &secs, &callback)){
488 return NULL;
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;
499 double offset;
500 int seconds;
501 PyObject *callback;
502 if (!PyArg_ParseTuple(args, "dO", &offset, &callback))
503 return NULL;
505 seconds = offset;
506 offset -= seconds;
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,
513 uint16_t flags,
514 void *private_data)
516 PyObject *callback = private_data, *ret;
518 ret = PyObject_CallFunction(callback, discard_const_p(char, "i"), flags);
519 Py_XDECREF(ret);
522 static void py_tevent_fp_dealloc(TeventFd_Object *self)
524 talloc_free(self->fd);
525 PyObject_Del(self);
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)
537 int fd, flags;
538 PyObject *handler;
539 struct tevent_fd *tfd;
540 TeventFd_Object *ret;
542 if (!PyArg_ParseTuple(args, "iiO", &fd, &flags, &handler))
543 return NULL;
545 tfd = tevent_add_fd(self->ev, NULL, fd, flags, py_fd_handler, handler);
546 if (tfd == NULL) {
547 PyErr_SetNone(PyExc_RuntimeError);
548 return NULL;
551 ret = PyObject_New(TeventFd_Object, &TeventFd_Type);
552 if (ret == NULL) {
553 talloc_free(tfd);
554 return NULL;
556 ret->fd = tfd;
558 return (PyObject *)ret;
561 static PyMethodDef py_tevent_context_methods[] = {
562 { "reinitialise", (PyCFunction)py_tevent_context_reinitialise,
563 METH_NOARGS,
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" },
579 { NULL },
582 static PyObject *py_tevent_req_wakeup_recv(PyObject *self,
583 PyObject *Py_UNUSED(ignored))
585 /* FIXME */
586 Py_RETURN_NONE;
589 static PyObject *py_tevent_req_received(PyObject *self,
590 PyObject *Py_UNUSED(ignored))
592 /* FIXME */
593 Py_RETURN_NONE;
596 static PyObject *py_tevent_req_is_error(PyObject *self,
597 PyObject *Py_UNUSED(ignored))
599 /* FIXME */
600 Py_RETURN_NONE;
603 static PyObject *py_tevent_req_poll(PyObject *self,
604 PyObject *Py_UNUSED(ignored))
606 /* FIXME */
607 Py_RETURN_NONE;
610 static PyObject *py_tevent_req_is_in_progress(PyObject *self,
611 PyObject *Py_UNUSED(ignored))
613 /* FIXME */
614 Py_RETURN_NONE;
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"),
623 { NULL }
626 static PyObject *py_tevent_req_post(PyObject *self, PyObject *args)
628 /* FIXME */
629 Py_RETURN_NONE;
632 static PyObject *py_tevent_req_set_error(PyObject *self, PyObject *args)
634 /* FIXME */
635 Py_RETURN_NONE;
638 static PyObject *py_tevent_req_done(PyObject *self,
639 PyObject *Py_UNUSED(ignored))
641 /* FIXME */
642 Py_RETURN_NONE;
645 static PyObject *py_tevent_req_notify_callback(PyObject *self,
646 PyObject *Py_UNUSED(ignored))
648 /* FIXME */
649 Py_RETURN_NONE;
652 static PyObject *py_tevent_req_set_endtime(PyObject *self, PyObject *args)
654 /* FIXME */
655 Py_RETURN_NONE;
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);
663 return NULL;
665 Py_RETURN_NONE;
668 static PyMethodDef py_tevent_req_methods[] = {
669 { "wakeup_recv", (PyCFunction)py_tevent_req_wakeup_recv,
670 METH_NOARGS,
671 "Wakeup received" },
672 { "received", (PyCFunction)py_tevent_req_received,
673 METH_NOARGS,
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,
678 "poll(ctx)" },
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,
684 "done()" },
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()" },
691 { NULL }
694 static void py_tevent_req_dealloc(TeventReq_Object *self)
696 talloc_free(self->req);
697 PyObject_DEL(self);
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 PyInt_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."),
721 { NULL },
724 static void py_tevent_queue_dealloc(TeventQueue_Object *self)
726 talloc_free(self->queue);
727 PyObject_Del(self);
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"),
753 { NULL }
756 static void py_tevent_context_dealloc(TeventContext_Object *self)
758 talloc_free(self->ev);
759 PyObject_Del(self);
762 static PyObject *py_tevent_context_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
764 const char * const kwnames[] = { "name", NULL };
765 char *name = NULL;
766 struct tevent_context *ev;
767 TeventContext_Object *ret;
769 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &name))
770 return NULL;
772 if (name == NULL) {
773 ev = tevent_context_init(NULL);
774 } else {
775 ev = tevent_context_init_byname(NULL, name);
778 if (ev == NULL) {
779 PyErr_SetNone(PyExc_RuntimeError);
780 return NULL;
783 ret = PyObject_New(TeventContext_Object, type);
784 if (ret == NULL) {
785 PyErr_NoMemory();
786 talloc_free(ev);
787 return NULL;
790 ret->ev = ev;
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)
806 char *backend_name;
807 if (!PyArg_ParseTuple(args, "s", &backend_name))
808 return NULL;
810 tevent_set_default_backend(backend_name);
812 Py_RETURN_NONE;
815 static PyObject *py_backend_list(PyObject *self,
816 PyObject *Py_UNUSED(ignored))
818 PyObject *ret = NULL;
819 PyObject *string = NULL;
820 int i, result;
821 const char **backends = NULL;
823 ret = PyList_New(0);
824 if (ret == NULL) {
825 return NULL;
828 backends = tevent_backend_list(NULL);
829 if (backends == NULL) {
830 PyErr_SetNone(PyExc_RuntimeError);
831 goto err;
833 for (i = 0; backends[i]; i++) {
834 string = PyUnicode_FromString(backends[i]);
835 if (!string) {
836 goto err;
838 result = PyList_Append(ret, string);
839 if (result) {
840 goto err;
842 Py_DECREF(string);
843 string = NULL;
846 talloc_free(backends);
848 return ret;
850 err:
851 Py_XDECREF(ret);
852 Py_XDECREF(string);
853 talloc_free(backends);
854 return NULL;
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" },
864 { NULL },
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,
872 .m_name = "_tevent",
873 .m_doc = MODULE_DOC,
874 .m_size = -1,
875 .m_methods = tevent_methods,
877 #endif
879 PyObject * module_init(void);
880 PyObject * module_init(void)
882 PyObject *m;
884 if (PyType_Ready(&TeventContext_Type) < 0)
885 return NULL;
887 if (PyType_Ready(&TeventQueue_Type) < 0)
888 return NULL;
890 if (PyType_Ready(&TeventReq_Type) < 0)
891 return NULL;
893 if (PyType_Ready(&TeventSignal_Type) < 0)
894 return NULL;
896 if (PyType_Ready(&TeventTimer_Type) < 0)
897 return NULL;
899 if (PyType_Ready(&TeventFd_Type) < 0)
900 return NULL;
902 #if PY_MAJOR_VERSION >= 3
903 m = PyModule_Create(&moduledef);
904 #else
905 m = Py_InitModule3("_tevent", tevent_methods, MODULE_DOC);
906 #endif
907 if (m == NULL)
908 return NULL;
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);
930 return m;
933 #if PY_MAJOR_VERSION >= 3
934 PyMODINIT_FUNC PyInit__tevent(void);
935 PyMODINIT_FUNC PyInit__tevent(void)
937 return module_init();
939 #else
940 void init_tevent(void);
941 void init_tevent(void)
943 module_init();
945 #endif