Revert "Document talloc_zero_array()"
[Samba/aatanasov.git] / lib / tevent / pytevent.c
blobfe7e7e3e3826b7540fab7a9f54e80cfcfe9f942f
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>
33 typedef struct {
34 PyObject_HEAD
35 struct tevent_context *ev_ctx;
36 } PyTEventContextObject;
38 PyAPI_DATA(PyTypeObject) PyTEventContext;
40 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
42 char *name;
44 if (!PyArg_ParseTuple(args, "s", &name))
45 return NULL;
46 tevent_set_default_backend(name);
47 Py_RETURN_NONE;
50 static PyObject *py_backend_list(PyObject *self)
52 const char **backends = tevent_backend_list(NULL);
53 PyObject *ret;
54 int i, len;
56 for (len = 0; backends[len]; len++);
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);