Add sorted subkey cache
[Samba.git] / lib / tevent / pytevent.c
blob3b45ba192875d6c994fd108e35788e21d84af1da
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007-2008
5 ** NOTE! The following LGPL license applies to the tevent
6 ** library. This does NOT imply that all of Samba is released
7 ** under the LGPL
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 3 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "replace.h"
24 #include <Python.h>
26 #ifndef Py_RETURN_NONE
27 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
28 #endif
30 #include <tevent.h>
31 #include <stdbool.h>
32 #include "tevent_util.h"
34 typedef struct {
35 PyObject_HEAD
36 struct tevent_context *ev_ctx;
37 } PyTEventContextObject;
39 PyAPI_DATA(PyTypeObject) PyTEventContext;
41 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
43 char *name;
45 if (!PyArg_ParseTuple(args, "s", &name))
46 return NULL;
47 tevent_set_default_backend(name);
48 Py_RETURN_NONE;
51 static PyObject *py_backend_list(PyObject *self)
53 const char **backends = tevent_backend_list(NULL);
54 PyObject *ret;
55 int i, len;
57 len = ev_str_list_length(backends);
58 ret = PyList_New(len);
59 for (i = 0; i < len; i++)
60 PyList_SetItem(ret, i, PyString_FromString(backends[i]));
61 talloc_free(backends);
63 return ret;
66 static PyMethodDef tevent_methods[] = {
67 { "set_default_backend", (PyCFunction)py_set_default_backend,
68 METH_VARARGS, "set_default_backend(name) -> None" },
69 { "backend_list", (PyCFunction)py_backend_list,
70 METH_NOARGS, "backend_list() -> list" },
71 { NULL },
74 static PyObject *py_event_ctx_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
76 const char *kwnames[] = { "name", NULL };
77 char *name = NULL;
78 struct tevent_context *ev_ctx;
79 PyTEventContextObject *ret;
80 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
81 discard_const_p(char *, kwnames),
82 &name))
83 return NULL;
85 if (name == NULL)
86 ev_ctx = tevent_context_init(NULL);
87 else
88 ev_ctx = tevent_context_init_byname(NULL, name);
90 ret = (PyTEventContextObject *)type->tp_alloc(type, 0);
91 ret->ev_ctx = ev_ctx;
92 return (PyObject *)ret;
95 static PyObject *py_event_ctx_loop_once(PyTEventContextObject *self)
97 return PyInt_FromLong(tevent_loop_once(self->ev_ctx));
100 static PyObject *py_event_ctx_loop_wait(PyTEventContextObject *self)
102 return PyInt_FromLong(tevent_loop_wait(self->ev_ctx));
105 static PyMethodDef py_event_ctx_methods[] = {
106 { "loop_once", (PyCFunction)py_event_ctx_loop_once, METH_NOARGS,
107 "S.loop_once() -> int" },
108 { "loop_wait", (PyCFunction)py_event_ctx_loop_wait, METH_NOARGS,
109 "S.loop_wait() -> int" },
110 { NULL }
113 static void py_event_ctx_dealloc(PyTEventContextObject * self)
115 talloc_free(self->ev_ctx);
116 self->ob_type->tp_free(self);
120 PyTypeObject PyTEventContext = {
121 .tp_name = "TEventContext",
122 .tp_methods = py_event_ctx_methods,
123 .tp_basicsize = sizeof(PyTEventContextObject),
124 .tp_dealloc = (destructor)py_event_ctx_dealloc,
125 .tp_flags = Py_TPFLAGS_DEFAULT,
126 .tp_new = py_event_ctx_new,
129 void inittevent(void)
131 PyObject *m;
133 if (PyType_Ready(&PyTEventContext) < 0)
134 return;
136 m = Py_InitModule3("tevent", tevent_methods, "Event management.");
137 if (m == NULL)
138 return;
140 Py_INCREF(&PyTEventContext);
141 PyModule_AddObject(m, "TEventContext", (PyObject *)&PyTEventContext);