Avoid using a utility header for Python replacements included in Samba,
[jra/samba/.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
21 #ifndef Py_RETURN_NONE
22 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
23 #endif
24
25 #include <tevent.h>
26 #include <stdbool.h>
27 #include <tevent_util.h>
28
29 typedef struct {
30         PyObject_HEAD
31         struct tevent_context *ev_ctx;
32 } PyTEventContextObject;
33
34 PyAPI_DATA(PyTypeObject) PyTEventContext;
35
36 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
37 {
38     char *name;
39
40     if (!PyArg_ParseTuple(args, "s", &name))
41         return NULL;
42     tevent_set_default_backend(name);
43     Py_RETURN_NONE;
44 }
45
46 static PyObject *py_backend_list(PyObject *self)
47 {
48     const char **backends = tevent_backend_list(NULL);
49     PyObject *ret;
50     int i, len;
51
52     len = ev_str_list_length(backends);
53     ret = PyList_New(len);
54     for (i = 0; i < len; i++)
55         PyList_SetItem(ret, i, PyString_FromString(backends[i]));
56     talloc_free(backends);
57
58     return ret;
59 }
60
61 static PyMethodDef tevent_methods[] = {
62     { "set_default_backend", (PyCFunction)py_set_default_backend, 
63         METH_VARARGS, "set_default_backend(name) -> None" },
64     { "backend_list", (PyCFunction)py_backend_list,
65         METH_NOARGS, "backend_list() -> list" },
66     { NULL },
67 };
68
69 static PyObject *py_event_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
70 {
71     const char *kwnames[] = { "name", NULL };
72     char *name = NULL;
73     struct tevent_context *ev_ctx;
74     PyTEventContextObject *ret;
75     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", (char **)kwnames, &name))
76         return NULL;
77
78     if (name == NULL)
79         ev_ctx = tevent_context_init(NULL);
80     else
81         ev_ctx = tevent_context_init_byname(NULL, name);
82
83     ret = (PyTEventContextObject *)type->tp_alloc(type, 0);
84     ret->ev_ctx = ev_ctx;
85     return (PyObject *)ret;
86 }
87
88 static PyObject *py_event_ctx_loop_once(PyTEventContextObject *self)
89 {
90     return PyInt_FromLong(tevent_loop_once(self->ev_ctx));
91 }
92
93 static PyObject *py_event_ctx_loop_wait(PyTEventContextObject *self)
94 {
95     return PyInt_FromLong(tevent_loop_wait(self->ev_ctx));
96 }
97
98 static PyMethodDef py_event_ctx_methods[] = {
99     { "loop_once", (PyCFunction)py_event_ctx_loop_once, METH_NOARGS, 
100         "S.loop_once() -> int" },
101     { "loop_wait", (PyCFunction)py_event_ctx_loop_wait, METH_NOARGS, 
102         "S.loop_wait() -> int" },
103     { NULL }
104 };
105
106 static void py_event_ctx_dealloc(PyTEventContextObject * self)
107 {
108         talloc_free(self->ev_ctx);
109         self->ob_type->tp_free(self);
110 }
111
112
113 PyTypeObject PyTEventContext = {
114     .tp_name = "TEventContext",
115     .tp_methods = py_event_ctx_methods,
116     .tp_basicsize = sizeof(PyTEventContextObject),
117     .tp_dealloc = (destructor)py_event_ctx_dealloc,
118     .tp_flags = Py_TPFLAGS_DEFAULT,
119     .tp_new = py_event_ctx_new,
120 };
121
122 void inittevent(void)
123 {
124     PyObject *m;
125
126     if (PyType_Ready(&PyTEventContext) < 0)
127         return;
128
129     m = Py_InitModule3("tevent", tevent_methods, "Event management.");
130     if (m == NULL)
131         return;
132
133     Py_INCREF(&PyTEventContext);
134     PyModule_AddObject(m, "TEventContext", (PyObject *)&PyTEventContext);
135 }
136