py: Properly increase the reference counter of Py_None.
[kai/samba-autobuild/.git] / lib / tevent / pytevent.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <Python.h>
20 #include <tevent.h>
21 #include <stdbool.h>
22 #include <tevent_util.h>
23
24 typedef struct {
25         PyObject_HEAD
26         struct tevent_context *ev_ctx;
27 } PyTEventContextObject;
28
29 PyAPI_DATA(PyTypeObject) PyTEventContext;
30
31 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
32 {
33     char *name;
34
35     if (!PyArg_ParseTuple(args, "s", &name))
36         return NULL;
37     tevent_set_default_backend(name);
38     Py_RETURN_NONE;
39 }
40
41 static PyObject *py_backend_list(PyObject *self)
42 {
43     const char **backends = tevent_backend_list(NULL);
44     PyObject *ret;
45     int i, len;
46
47     len = ev_str_list_length(backends);
48     ret = PyList_New(len);
49     for (i = 0; i < len; i++)
50         PyList_SetItem(ret, i, PyString_FromString(backends[i]));
51     talloc_free(backends);
52
53     return ret;
54 }
55
56 static PyMethodDef tevent_methods[] = {
57     { "set_default_backend", (PyCFunction)py_set_default_backend, 
58         METH_VARARGS, "set_default_backend(name) -> None" },
59     { "backend_list", (PyCFunction)py_backend_list,
60         METH_NOARGS, "backend_list() -> list" },
61     { NULL },
62 };
63
64 static PyObject *py_event_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
65 {
66     const char *kwnames[] = { "name", NULL };
67     char *name = NULL;
68     struct tevent_context *ev_ctx;
69     PyTEventContextObject *ret;
70     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", (char **)kwnames, &name))
71         return NULL;
72
73     if (name == NULL)
74         ev_ctx = tevent_context_init(NULL);
75     else
76         ev_ctx = tevent_context_init_byname(NULL, name);
77
78     ret = (PyTEventContextObject *)type->tp_alloc(type, 0);
79     ret->ev_ctx = ev_ctx;
80     return (PyObject *)ret;
81 }
82
83 static PyObject *py_event_ctx_loop_once(PyTEventContextObject *self)
84 {
85     return PyInt_FromLong(tevent_loop_once(self->ev_ctx));
86 }
87
88 static PyObject *py_event_ctx_loop_wait(PyTEventContextObject *self)
89 {
90     return PyInt_FromLong(tevent_loop_wait(self->ev_ctx));
91 }
92
93 static PyMethodDef py_event_ctx_methods[] = {
94     { "loop_once", (PyCFunction)py_event_ctx_loop_once, METH_NOARGS, 
95         "S.loop_once() -> int" },
96     { "loop_wait", (PyCFunction)py_event_ctx_loop_wait, METH_NOARGS, 
97         "S.loop_wait() -> int" },
98     { NULL }
99 };
100
101 static void py_event_ctx_dealloc(PyTEventContextObject * self)
102 {
103         talloc_free(self->ev_ctx);
104         self->ob_type->tp_free(self);
105 }
106
107
108 PyTypeObject PyTEventContext = {
109     .tp_name = "TEventContext",
110     .tp_methods = py_event_ctx_methods,
111     .tp_basicsize = sizeof(PyTEventContextObject),
112     .tp_dealloc = (destructor)py_event_ctx_dealloc,
113     .tp_flags = Py_TPFLAGS_DEFAULT,
114     .tp_new = py_event_ctx_new,
115 };
116
117 void inittevent(void)
118 {
119     PyObject *m;
120
121     if (PyType_Ready(&PyTEventContext) < 0)
122         return;
123
124     m = Py_InitModule3("tevent", tevent_methods, "Event management.");
125     if (m == NULL)
126         return;
127
128     Py_INCREF(&PyTEventContext);
129     PyModule_AddObject(m, "TEventContext", (PyObject *)&PyTEventContext);
130 }
131