pyldb: avoid segfault when adding an element with no name
[sfrench/samba-autobuild/.git] / lib / tevent / pytevent.c
1 /*
2    Unix SMB/CIFS implementation.
3    Python bindings for tevent
4
5    Copyright (C) Jelmer Vernooij 2010
6
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
10
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.
15
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.
20
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/>.
23 */
24
25 #include <Python.h>
26 #include "replace.h"
27 #include <tevent.h>
28
29 #if PY_MAJOR_VERSION >= 3
30 #define PyStr_Check PyUnicode_Check
31 #define PyStr_FromString PyUnicode_FromString
32 #define PyStr_AsUTF8 PyUnicode_AsUTF8
33 #define PyInt_FromLong PyLong_FromLong
34 #else
35 #define PyStr_Check PyString_Check
36 #define PyStr_FromString PyString_FromString
37 #define PyStr_AsUTF8 PyString_AsString
38 #endif
39
40 void init_tevent(void);
41
42 typedef struct {
43         PyObject_HEAD
44         struct tevent_context *ev;
45 } TeventContext_Object;
46
47 typedef struct {
48         PyObject_HEAD
49         struct tevent_queue *queue;
50 } TeventQueue_Object;
51
52 typedef struct {
53         PyObject_HEAD
54         struct tevent_req *req;
55 } TeventReq_Object;
56
57 typedef struct {
58         PyObject_HEAD
59         struct tevent_signal *signal;
60 } TeventSignal_Object;
61
62 typedef struct {
63         PyObject_HEAD
64         struct tevent_timer *timer;
65         PyObject *callback;
66 } TeventTimer_Object;
67
68 typedef struct {
69         PyObject_HEAD
70         struct tevent_fd *fd;
71 } TeventFd_Object;
72
73 static PyTypeObject TeventContext_Type;
74 static PyTypeObject TeventReq_Type;
75 static PyTypeObject TeventQueue_Type;
76 static PyTypeObject TeventSignal_Type;
77 static PyTypeObject TeventTimer_Type;
78 static PyTypeObject TeventFd_Type;
79
80 static int py_context_init(struct tevent_context *ev)
81 {
82         /* FIXME */
83         return 0;
84 }
85
86 static struct tevent_fd *py_add_fd(struct tevent_context *ev,
87                                     TALLOC_CTX *mem_ctx,
88                                     int fd, uint16_t flags,
89                                     tevent_fd_handler_t handler,
90                                     void *private_data,
91                                     const char *handler_name,
92                                     const char *location)
93 {
94         /* FIXME */
95         return NULL;
96 }
97
98 static void py_set_fd_close_fn(struct tevent_fd *fde,
99                                 tevent_fd_close_fn_t close_fn)
100 {
101         /* FIXME */
102 }
103
104 static uint16_t py_get_fd_flags(struct tevent_fd *fde)
105 {
106         /* FIXME */
107         return 0;
108 }
109
110 static void py_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
111 {
112         /* FIXME */
113 }
114
115 /* timed_event functions */
116 static struct tevent_timer *py_add_timer(struct tevent_context *ev,
117                                           TALLOC_CTX *mem_ctx,
118                                           struct timeval next_event,
119                                           tevent_timer_handler_t handler,
120                                           void *private_data,
121                                           const char *handler_name,
122                                           const char *location)
123 {
124         /* FIXME */
125         return NULL;
126 }
127
128 /* immediate event functions */
129 static void py_schedule_immediate(struct tevent_immediate *im,
130                                    struct tevent_context *ev,
131                                    tevent_immediate_handler_t handler,
132                                    void *private_data,
133                                    const char *handler_name,
134                                    const char *location)
135 {
136         /* FIXME */
137 }
138
139 /* signal functions */
140 static struct tevent_signal *py_add_signal(struct tevent_context *ev,
141                                             TALLOC_CTX *mem_ctx,
142                                             int signum, int sa_flags,
143                                             tevent_signal_handler_t handler,
144                                             void *private_data,
145                                             const char *handler_name,
146                                             const char *location)
147 {
148         /* FIXME */
149         return NULL;
150 }
151
152 /* loop functions */
153 static int py_loop_once(struct tevent_context *ev, const char *location)
154 {
155         /* FIXME */
156         return 0;
157 }
158
159 static int py_loop_wait(struct tevent_context *ev, const char *location)
160 {
161         /* FIXME */
162         return 0;
163 }
164
165 const static struct tevent_ops py_tevent_ops = {
166         .context_init = py_context_init,
167         .add_fd = py_add_fd,
168         .set_fd_close_fn = py_set_fd_close_fn,
169         .get_fd_flags = py_get_fd_flags,
170         .set_fd_flags = py_set_fd_flags,
171         .add_timer = py_add_timer,
172         .schedule_immediate = py_schedule_immediate,
173         .add_signal = py_add_signal,
174         .loop_wait = py_loop_wait,
175         .loop_once = py_loop_once,
176 };
177
178 static PyObject *py_register_backend(PyObject *self, PyObject *args)
179 {
180         PyObject *name, *py_backend;
181
182         if (!PyArg_ParseTuple(args, "O", &py_backend))
183                 return NULL;
184
185         name = PyObject_GetAttrString(py_backend, "name");
186         if (name == NULL) {
187                 PyErr_SetNone(PyExc_AttributeError);
188                 return NULL;
189         }
190
191         if (!(PyStr_Check(name) || PyUnicode_Check(name))) {
192                 PyErr_SetNone(PyExc_TypeError);
193                 Py_DECREF(name);
194                 return NULL;
195         }
196
197         if (!tevent_register_backend(PyStr_AsUTF8(name), &py_tevent_ops)) { /* FIXME: What to do with backend */
198                 PyErr_SetNone(PyExc_RuntimeError);
199                 Py_DECREF(name);
200                 return NULL;
201         }
202
203         Py_DECREF(name);
204
205         Py_RETURN_NONE;
206 }
207
208 static PyObject *py_tevent_context_reinitialise(TeventContext_Object *self)
209 {
210         int ret = tevent_re_initialise(self->ev);
211         if (ret != 0) {
212                 PyErr_SetNone(PyExc_RuntimeError);
213                 return NULL;
214         }
215         Py_RETURN_NONE;
216 }
217
218 static PyObject *py_tevent_queue_stop(TeventQueue_Object *self)
219 {
220         tevent_queue_stop(self->queue);
221         Py_RETURN_NONE;
222 }
223
224 static PyObject *py_tevent_queue_start(TeventQueue_Object *self)
225 {
226         tevent_queue_start(self->queue);
227         Py_RETURN_NONE;
228 }
229
230 static void py_queue_trigger(struct tevent_req *req, void *private_data)
231 {
232         PyObject *callback = private_data, *ret;
233
234         ret = PyObject_CallFunction(callback, discard_const_p(char, ""));
235         Py_XDECREF(ret);
236 }
237
238 static PyObject *py_tevent_queue_add(TeventQueue_Object *self, PyObject *args)
239 {
240         TeventContext_Object *py_ev;
241         TeventReq_Object *py_req;
242         PyObject *trigger;
243         bool ret;
244
245         if (!PyArg_ParseTuple(args, "O!O!O", 
246                                                   &TeventContext_Type, &py_ev,
247                                                   &TeventReq_Type, &py_req,
248                                                   &trigger))
249                 return NULL;
250
251         Py_INCREF(trigger);
252
253         ret = tevent_queue_add(self->queue, py_ev->ev, py_req->req,
254                                                    py_queue_trigger, trigger);
255         if (!ret) {
256                 PyErr_SetString(PyExc_RuntimeError, "queue add failed");
257                 Py_DECREF(trigger);
258                 return NULL;
259         }
260
261         Py_RETURN_NONE;
262 }
263
264 static PyMethodDef py_tevent_queue_methods[] = {
265         { "stop", (PyCFunction)py_tevent_queue_stop, METH_NOARGS,
266                 "S.stop()" },
267         { "start", (PyCFunction)py_tevent_queue_start, METH_NOARGS,
268                 "S.start()" },
269         { "add", (PyCFunction)py_tevent_queue_add, METH_VARARGS,
270                 "S.add(ctx, req, trigger, baton)" },
271         { NULL },
272 };
273
274 static PyObject *py_tevent_context_wakeup_send(PyObject *self, PyObject *args)
275 {
276         /* FIXME */
277
278         Py_RETURN_NONE;
279 }
280
281 static PyObject *py_tevent_context_loop_wait(TeventContext_Object *self)
282 {
283         if (tevent_loop_wait(self->ev) != 0) {
284                 PyErr_SetNone(PyExc_RuntimeError);
285                 return NULL;
286         }
287         Py_RETURN_NONE;
288 }
289
290 static PyObject *py_tevent_context_loop_once(TeventContext_Object *self)
291 {
292         if (tevent_loop_once(self->ev) != 0) {
293                 PyErr_SetNone(PyExc_RuntimeError);
294                 return NULL;
295         }
296         Py_RETURN_NONE;
297 }
298
299 static void py_tevent_signal_handler(struct tevent_context *ev,
300                                         struct tevent_signal *se,
301                                         int signum,
302                                         int count,
303                                         void *siginfo,
304                                         void *private_data)
305 {
306         PyObject *callback = (PyObject *)private_data, *ret;
307
308         ret = PyObject_CallFunction(callback, discard_const_p(char, "ii"), signum, count);
309         Py_XDECREF(ret);
310 }
311
312 static void py_tevent_signal_dealloc(TeventSignal_Object *self)
313 {
314         talloc_free(self->signal);
315         PyObject_Del(self);
316 }
317
318 static PyTypeObject TeventSignal_Type = {
319         .tp_name = "tevent.Signal",
320         .tp_basicsize = sizeof(TeventSignal_Object),
321         .tp_dealloc = (destructor)py_tevent_signal_dealloc,
322         .tp_flags = Py_TPFLAGS_DEFAULT,
323 };
324
325 static PyObject *py_tevent_context_add_signal(TeventContext_Object *self, PyObject *args)
326 {
327         int signum, sa_flags;
328         PyObject *handler;
329         struct tevent_signal *sig;
330         TeventSignal_Object *ret;
331
332         if (!PyArg_ParseTuple(args, "iiO", &signum, &sa_flags, &handler))
333                 return NULL;
334
335         Py_INCREF(handler);
336         sig = tevent_add_signal(self->ev, NULL, signum, sa_flags,
337                                                         py_tevent_signal_handler, handler);
338
339         ret = PyObject_New(TeventSignal_Object, &TeventSignal_Type);
340         if (ret == NULL) {
341                 PyErr_NoMemory();
342                 talloc_free(sig);
343                 return NULL;
344         }
345
346         ret->signal = sig;
347
348         return (PyObject *)ret;
349 }
350
351 static void py_timer_handler(struct tevent_context *ev,
352                                        struct tevent_timer *te,
353                                        struct timeval current_time,
354                                        void *private_data)
355 {
356         TeventTimer_Object *self = private_data;
357         PyObject *ret;
358
359         ret = PyObject_CallFunction(self->callback, discard_const_p(char, "l"), te);
360         if (ret == NULL) {
361                 /* No Python stack to propagate exception to; just print traceback */
362                 PyErr_PrintEx(0);
363         }
364         Py_XDECREF(ret);
365 }
366
367 static void py_tevent_timer_dealloc(TeventTimer_Object *self)
368 {
369         if (self->timer) {
370                 talloc_free(self->timer);
371         }
372         Py_DECREF(self->callback);
373         PyObject_Del(self);
374 }
375
376 static int py_tevent_timer_traverse(TeventTimer_Object *self, visitproc visit, void *arg)
377 {
378         Py_VISIT(self->callback);
379         return 0;
380 }
381
382 static PyObject* py_tevent_timer_get_active(TeventTimer_Object *self) {
383         return PyBool_FromLong(self->timer != NULL);
384 }
385
386 struct PyGetSetDef py_tevent_timer_getset[] = {
387         {
388                 .name = discard_const_p(char, "active"),
389                 .get = (getter)py_tevent_timer_get_active,
390                 .doc = discard_const_p(char, "true if the timer is scheduled to run"),
391         },
392         {NULL},
393 };
394
395 static PyTypeObject TeventTimer_Type = {
396         .tp_name = "tevent.Timer",
397         .tp_basicsize = sizeof(TeventTimer_Object),
398         .tp_dealloc = (destructor)py_tevent_timer_dealloc,
399         .tp_traverse = (traverseproc)py_tevent_timer_traverse,
400         .tp_getset = py_tevent_timer_getset,
401         .tp_flags = Py_TPFLAGS_DEFAULT,
402 };
403
404 struct TeventTimer_Object_ref {
405         TeventTimer_Object *obj;
406 };
407
408 static int TeventTimer_Object_ref_destructor(struct TeventTimer_Object_ref *ref)
409 {
410         ref->obj->timer = NULL;
411         Py_DECREF(ref->obj);
412         return 0;
413 }
414
415 static PyObject *py_tevent_context_add_timer_internal(TeventContext_Object *self,
416                                                       struct timeval next_event,
417                                                       PyObject *callback)
418 {
419         /* Ownership notes:
420          *
421          * There are 5 pieces in play; two tevent contexts and 3 Python objects:
422          * - The tevent timer
423          * - The tevent context
424          * - The Python context -- "self"
425          * - The Python timer (TeventTimer_Object) -- "ret"
426          * - The Python callback function -- "callback"
427          *
428          * We only use the Python context for getting the tevent context,
429          * afterwards it can be destroyed.
430          *
431          * The tevent context owns the tevent timer.
432          *
433          * The tevent timer holds a reference to the Python timer, so the Python
434          * timer must always outlive the tevent timer.
435          * The Python timer has a pointer to the tevent timer; a destructor is
436          * used to set this to NULL when the tevent timer is deallocated.
437          *
438          * The tevent timer can be deallocated in these cases:
439          *  1) when the context is destroyed
440          *  2) after the event fires
441          *  Posssibly, API might be added to cancel (free the tevent timer).
442          *
443          * The Python timer holds a reference to the callback.
444          */
445         TeventTimer_Object *ret;
446         struct TeventTimer_Object_ref *ref;
447
448         ret = PyObject_New(TeventTimer_Object, &TeventTimer_Type);
449         if (ret == NULL) {
450                 PyErr_NoMemory();
451                 return NULL;
452         }
453         Py_INCREF(callback);
454         ret->callback = callback;
455         ret->timer = tevent_add_timer(self->ev, NULL, next_event, py_timer_handler,
456                                       ret);
457         if (ret->timer == NULL) {
458                 Py_DECREF(ret);
459                 PyErr_SetString(PyExc_RuntimeError, "Could not initialize timer");
460                 return NULL;
461         }
462         ref = talloc(ret->timer, struct TeventTimer_Object_ref);
463         if (ref == NULL) {
464                 talloc_free(ret->timer);
465                 Py_DECREF(ret);
466                 PyErr_SetString(PyExc_RuntimeError, "Could not initialize timer");
467                 return NULL;
468         }
469         Py_INCREF(ret);
470         ref->obj = ret;
471
472         talloc_set_destructor(ref, TeventTimer_Object_ref_destructor);
473
474         return (PyObject *)ret;
475 }
476
477 static PyObject *py_tevent_context_add_timer(TeventContext_Object *self, PyObject *args)
478 {
479         struct timeval next_event;
480         PyObject *callback;
481         double secs, usecs;
482         if (!PyArg_ParseTuple(args, "dO", &secs, &callback)){
483                 return NULL;
484         }
485         next_event.tv_sec = secs;
486         usecs = (secs - next_event.tv_sec) * 1000000.0;
487         next_event.tv_usec = usecs;
488         return py_tevent_context_add_timer_internal(self, next_event, callback);
489 }
490
491 static PyObject *py_tevent_context_add_timer_offset(TeventContext_Object *self, PyObject *args)
492 {
493         struct timeval next_event;
494         double offset;
495         int seconds;
496         PyObject *callback;
497         if (!PyArg_ParseTuple(args, "dO", &offset, &callback))
498                 return NULL;
499
500         seconds = offset;
501         offset -= seconds;
502         next_event = tevent_timeval_current_ofs(seconds, (int)(offset*1000000));
503         return py_tevent_context_add_timer_internal(self, next_event, callback);
504 }
505
506 static void py_fd_handler(struct tevent_context *ev,
507                                     struct tevent_fd *fde,
508                                     uint16_t flags,
509                                     void *private_data)
510 {
511         PyObject *callback = private_data, *ret;
512
513         ret = PyObject_CallFunction(callback, discard_const_p(char, "i"), flags);
514         Py_XDECREF(ret);
515 }
516
517 static void py_tevent_fp_dealloc(TeventFd_Object *self)
518 {
519         talloc_free(self->fd);
520         PyObject_Del(self);
521 }
522
523 static PyTypeObject TeventFd_Type = {
524         .tp_name = "tevent.Fd",
525         .tp_basicsize = sizeof(TeventFd_Object),
526         .tp_dealloc = (destructor)py_tevent_fp_dealloc,
527         .tp_flags = Py_TPFLAGS_DEFAULT,
528 };
529
530 static PyObject *py_tevent_context_add_fd(TeventContext_Object *self, PyObject *args)
531 {
532         int fd, flags;
533         PyObject *handler;
534         struct tevent_fd *tfd;
535         TeventFd_Object *ret;
536
537         if (!PyArg_ParseTuple(args, "iiO", &fd, &flags, &handler))
538                 return NULL;
539
540         tfd = tevent_add_fd(self->ev, NULL, fd, flags, py_fd_handler, handler);
541         if (tfd == NULL) {
542                 PyErr_SetNone(PyExc_RuntimeError);
543                 return NULL;
544         }
545
546         ret = PyObject_New(TeventFd_Object, &TeventFd_Type);
547         if (ret == NULL) {
548                 talloc_free(tfd);
549                 return NULL;
550         }
551         ret->fd = tfd;
552
553         return (PyObject *)ret;
554 }
555
556 static PyMethodDef py_tevent_context_methods[] = {
557         { "reinitialise", (PyCFunction)py_tevent_context_reinitialise, METH_NOARGS,
558                 "S.reinitialise()" },
559         { "wakeup_send", (PyCFunction)py_tevent_context_wakeup_send, 
560                 METH_VARARGS, "S.wakeup_send(wakeup_time) -> req" },
561         { "loop_wait", (PyCFunction)py_tevent_context_loop_wait,
562                 METH_NOARGS, "S.loop_wait()" },
563         { "loop_once", (PyCFunction)py_tevent_context_loop_once,
564                 METH_NOARGS, "S.loop_once()" },
565         { "add_signal", (PyCFunction)py_tevent_context_add_signal,
566                 METH_VARARGS, "S.add_signal(signum, sa_flags, handler) -> signal" },
567         { "add_timer", (PyCFunction)py_tevent_context_add_timer,
568                 METH_VARARGS, "S.add_timer(next_event, handler) -> timer" },
569         { "add_timer_offset", (PyCFunction)py_tevent_context_add_timer_offset,
570                 METH_VARARGS, "S.add_timer(offset_seconds, handler) -> timer" },
571         { "add_fd", (PyCFunction)py_tevent_context_add_fd, 
572                 METH_VARARGS, "S.add_fd(fd, flags, handler) -> fd" },
573         { NULL },
574 };
575
576 static PyObject *py_tevent_req_wakeup_recv(PyObject *self)
577 {
578         /* FIXME */
579         Py_RETURN_NONE;
580 }
581
582 static PyObject *py_tevent_req_received(PyObject *self)
583 {
584         /* FIXME */
585         Py_RETURN_NONE;
586 }
587
588 static PyObject *py_tevent_req_is_error(PyObject *self)
589 {
590         /* FIXME */
591         Py_RETURN_NONE;
592 }
593
594 static PyObject *py_tevent_req_poll(PyObject *self)
595 {
596         /* FIXME */
597         Py_RETURN_NONE;
598 }
599
600 static PyObject *py_tevent_req_is_in_progress(PyObject *self)
601 {
602         /* FIXME */
603         Py_RETURN_NONE;
604 }
605
606 static PyGetSetDef py_tevent_req_getsetters[] = {
607         {
608                 .name = discard_const_p(char, "in_progress"),
609                 .get = (getter)py_tevent_req_is_in_progress,
610                 .doc = discard_const_p(char, "Whether the request is in progress"),
611         },
612         { NULL }
613 };
614
615 static PyObject *py_tevent_req_post(PyObject *self, PyObject *args)
616 {
617         /* FIXME */
618         Py_RETURN_NONE;
619 }
620
621 static PyObject *py_tevent_req_set_error(PyObject *self, PyObject *args)
622 {
623         /* FIXME */
624         Py_RETURN_NONE;
625 }
626
627 static PyObject *py_tevent_req_done(PyObject *self)
628 {
629         /* FIXME */
630         Py_RETURN_NONE;
631 }
632
633 static PyObject *py_tevent_req_notify_callback(PyObject *self)
634 {
635         /* FIXME */
636         Py_RETURN_NONE;
637 }
638
639 static PyObject *py_tevent_req_set_endtime(PyObject *self, PyObject *args)
640 {
641         /* FIXME */
642         Py_RETURN_NONE;
643 }
644
645 static PyObject *py_tevent_req_cancel(TeventReq_Object *self)
646 {
647         if (!tevent_req_cancel(self->req)) {
648                 PyErr_SetNone(PyExc_RuntimeError);
649                 return NULL;
650         }
651         Py_RETURN_NONE;
652 }
653
654 static PyMethodDef py_tevent_req_methods[] = {
655         { "wakeup_recv", (PyCFunction)py_tevent_req_wakeup_recv, METH_NOARGS,
656                 "Wakeup received" },
657         { "received", (PyCFunction)py_tevent_req_received, METH_NOARGS,
658                 "Receive finished" },
659         { "is_error", (PyCFunction)py_tevent_req_is_error, METH_NOARGS,
660                 "is_error() -> (error, state)" },
661         { "poll", (PyCFunction)py_tevent_req_poll, METH_VARARGS,
662                 "poll(ctx)" },
663         { "post", (PyCFunction)py_tevent_req_post, METH_VARARGS,
664                 "post(ctx) -> req" },
665         { "set_error", (PyCFunction)py_tevent_req_set_error, METH_VARARGS,
666                 "set_error(error)" },
667         { "done", (PyCFunction)py_tevent_req_done, METH_NOARGS,
668                 "done()" },
669         { "notify_callback", (PyCFunction)py_tevent_req_notify_callback,
670                 METH_NOARGS, "notify_callback()" },
671         { "set_endtime", (PyCFunction)py_tevent_req_set_endtime,
672                 METH_VARARGS, "set_endtime(ctx, endtime)" },
673         { "cancel", (PyCFunction)py_tevent_req_cancel,
674                 METH_NOARGS, "cancel()" },
675         { NULL }
676 };
677
678 static void py_tevent_req_dealloc(TeventReq_Object *self)
679 {
680         talloc_free(self->req);
681         PyObject_DEL(self);
682 }
683
684 static PyTypeObject TeventReq_Type = {
685         .tp_name = "tevent.Request",
686         .tp_basicsize = sizeof(TeventReq_Object),
687         .tp_methods = py_tevent_req_methods,
688         .tp_dealloc = (destructor)py_tevent_req_dealloc,
689         .tp_getset = py_tevent_req_getsetters,
690         /* FIXME: .tp_new = py_tevent_req_new, */
691 };
692
693 static PyObject *py_tevent_queue_get_length(TeventQueue_Object *self)
694 {
695         return PyInt_FromLong(tevent_queue_length(self->queue));
696 }
697
698 static PyGetSetDef py_tevent_queue_getsetters[] = {
699         {
700                 .name = discard_const_p(char, "length"),
701                 .get = (getter)py_tevent_queue_get_length,
702                 .doc = discard_const_p(char, "The number of elements in the queue."),
703         },
704         { NULL },
705 };
706
707 static void py_tevent_queue_dealloc(TeventQueue_Object *self)
708 {
709         talloc_free(self->queue);
710         PyObject_Del(self);
711 }
712
713 static PyTypeObject TeventQueue_Type = {
714         .tp_name = "tevent.Queue",
715         .tp_basicsize = sizeof(TeventQueue_Object),
716         .tp_dealloc = (destructor)py_tevent_queue_dealloc,
717         .tp_flags = Py_TPFLAGS_DEFAULT,
718         .tp_getset = py_tevent_queue_getsetters,
719         .tp_methods = py_tevent_queue_methods,
720 };
721
722 static PyObject *py_tevent_context_signal_support(PyObject *_self)
723 {
724         TeventContext_Object *self = (TeventContext_Object *)_self;
725         return PyBool_FromLong(tevent_signal_support(self->ev));
726 }
727
728 static PyGetSetDef py_tevent_context_getsetters[] = {
729         {
730                 .name = discard_const_p(char, "signal_support"),
731                 .get = (getter)py_tevent_context_signal_support,
732                 .doc = discard_const_p(char, "if this platform and tevent context support signal handling"),
733         },
734         { NULL }
735 };
736
737 static void py_tevent_context_dealloc(TeventContext_Object *self)
738 {
739         talloc_free(self->ev);
740         PyObject_Del(self);
741 }
742
743 static PyObject *py_tevent_context_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
744 {
745         const char * const kwnames[] = { "name", NULL };
746         char *name = NULL;
747         struct tevent_context *ev;
748         TeventContext_Object *ret;
749
750         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &name))
751                 return NULL;
752
753         if (name == NULL) {
754                 ev = tevent_context_init(NULL);
755         } else {
756                 ev = tevent_context_init_byname(NULL, name);
757         }
758
759         if (ev == NULL) {
760                 PyErr_SetNone(PyExc_RuntimeError);
761                 return NULL;
762         }
763
764         ret = PyObject_New(TeventContext_Object, type);
765         if (ret == NULL) {
766                 PyErr_NoMemory();
767                 talloc_free(ev);
768                 return NULL;
769         }
770
771         ret->ev = ev;
772         return (PyObject *)ret;
773 }
774
775 static PyTypeObject TeventContext_Type = {
776         .tp_name = "tevent.Context",
777         .tp_new = py_tevent_context_new,
778         .tp_basicsize = sizeof(TeventContext_Object),
779         .tp_dealloc = (destructor)py_tevent_context_dealloc,
780         .tp_methods = py_tevent_context_methods,
781         .tp_getset = py_tevent_context_getsetters,
782         .tp_flags = Py_TPFLAGS_DEFAULT,
783 };
784
785 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
786 {
787         char *backend_name;
788         if (!PyArg_ParseTuple(args, "s", &backend_name))
789                 return NULL;
790
791         tevent_set_default_backend(backend_name);
792
793         Py_RETURN_NONE;
794 }
795
796 static PyObject *py_backend_list(PyObject *self)
797 {
798         PyObject *ret = NULL;
799         PyObject *string = NULL;
800         int i, result;
801         const char **backends = NULL;
802
803         ret = PyList_New(0);
804         if (ret == NULL) {
805                 return NULL;
806         }
807
808         backends = tevent_backend_list(NULL);
809         if (backends == NULL) {
810                 PyErr_SetNone(PyExc_RuntimeError);
811                 goto err;
812         }
813         for (i = 0; backends[i]; i++) {
814                 string = PyStr_FromString(backends[i]);
815                 if (!string) {
816                         goto err;
817                 }
818                 result = PyList_Append(ret, string);
819                 if (result) {
820                         goto err;
821                 }
822                 Py_DECREF(string);
823                 string = NULL;
824         }
825
826         talloc_free(backends);
827
828         return ret;
829
830 err:
831         Py_XDECREF(ret);
832         Py_XDECREF(string);
833         talloc_free(backends);
834         return NULL;
835 }
836
837 static PyMethodDef tevent_methods[] = {
838         { "register_backend", (PyCFunction)py_register_backend, METH_VARARGS,
839                 "register_backend(backend)" },
840         { "set_default_backend", (PyCFunction)py_set_default_backend, 
841                 METH_VARARGS, "set_default_backend(backend)" },
842         { "backend_list", (PyCFunction)py_backend_list, 
843                 METH_NOARGS, "backend_list() -> list" },
844         { NULL },
845 };
846
847 #define MODULE_DOC PyDoc_STR("Python wrapping of talloc-maintained objects.")
848
849 #if PY_MAJOR_VERSION >= 3
850 static struct PyModuleDef moduledef = {
851         PyModuleDef_HEAD_INIT,
852         .m_name = "_tevent",
853         .m_doc = MODULE_DOC,
854         .m_size = -1,
855         .m_methods = tevent_methods,
856 };
857 #endif
858
859 PyObject * module_init(void);
860 PyObject * module_init(void)
861 {
862         PyObject *m;
863
864         if (PyType_Ready(&TeventContext_Type) < 0)
865                 return NULL;
866
867         if (PyType_Ready(&TeventQueue_Type) < 0)
868                 return NULL;
869
870         if (PyType_Ready(&TeventReq_Type) < 0)
871                 return NULL;
872
873         if (PyType_Ready(&TeventSignal_Type) < 0)
874                 return NULL;
875
876         if (PyType_Ready(&TeventTimer_Type) < 0)
877                 return NULL;
878
879         if (PyType_Ready(&TeventFd_Type) < 0)
880                 return NULL;
881
882 #if PY_MAJOR_VERSION >= 3
883         m = PyModule_Create(&moduledef);
884 #else
885         m = Py_InitModule3("_tevent", tevent_methods, MODULE_DOC);
886 #endif
887         if (m == NULL)
888                 return NULL;
889
890         Py_INCREF(&TeventContext_Type);
891         PyModule_AddObject(m, "Context", (PyObject *)&TeventContext_Type);
892
893         Py_INCREF(&TeventQueue_Type);
894         PyModule_AddObject(m, "Queue", (PyObject *)&TeventQueue_Type);
895
896         Py_INCREF(&TeventReq_Type);
897         PyModule_AddObject(m, "Request", (PyObject *)&TeventReq_Type);
898
899         Py_INCREF(&TeventSignal_Type);
900         PyModule_AddObject(m, "Signal", (PyObject *)&TeventSignal_Type);
901
902         Py_INCREF(&TeventTimer_Type);
903         PyModule_AddObject(m, "Timer", (PyObject *)&TeventTimer_Type);
904
905         Py_INCREF(&TeventFd_Type);
906         PyModule_AddObject(m, "Fd", (PyObject *)&TeventFd_Type);
907
908         PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
909
910         return m;
911 }
912
913 #if PY_MAJOR_VERSION >= 3
914 PyMODINIT_FUNC PyInit__tevent(void);
915 PyMODINIT_FUNC PyInit__tevent(void)
916 {
917         return module_init();
918 }
919 #else
920 void init_tevent(void);
921 void init_tevent(void)
922 {
923         module_init();
924 }
925 #endif