s3/s4 build: Fix Py_RETURN_NONE to work with python versions < 2.4
[bbaumbach/samba-autobuild/.git] / lib / tevent / pytevent.c
index b379911b9c9c739e237d1ae2efb54ec971fb4ab5..756d013ede83b771aee82e6c5d2535ca1c2f0e0d 100644 (file)
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <Python.h>
+#include "../lib/util/python_util.h"
 #include <tevent.h>
 #include <stdbool.h>
-#include <../talloc/pytalloc.h>
 #include <tevent_util.h>
 
-PyAPI_DATA(PyTypeObject) PyEventContext;
+typedef struct {
+       PyObject_HEAD
+       struct tevent_context *ev_ctx;
+} PyTEventContextObject;
+
+PyAPI_DATA(PyTypeObject) PyTEventContext;
 
 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
 {
@@ -30,13 +34,13 @@ static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
 
     if (!PyArg_ParseTuple(args, "s", &name))
         return NULL;
-    event_set_default_backend(name);
-    return Py_None;
+    tevent_set_default_backend(name);
+    Py_RETURN_NONE;
 }
 
 static PyObject *py_backend_list(PyObject *self)
 {
-    const char **backends = event_backend_list(NULL);
+    const char **backends = tevent_backend_list(NULL);
     PyObject *ret;
     int i, len;
 
@@ -61,26 +65,29 @@ static PyObject *py_event_ctx_new(PyTypeObject *type, PyObject *args, PyObject *
 {
     const char *kwnames[] = { "name", NULL };
     char *name = NULL;
-    struct event_context *ev_ctx;
+    struct tevent_context *ev_ctx;
+    PyTEventContextObject *ret;
     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", (char **)kwnames, &name))
         return NULL;
 
     if (name == NULL)
-        ev_ctx = event_context_init(NULL);
+        ev_ctx = tevent_context_init(NULL);
     else
-        ev_ctx = event_context_init_byname(NULL, name);
+        ev_ctx = tevent_context_init_byname(NULL, name);
 
-    return py_talloc_import(&PyEventContext, ev_ctx);
+    ret = (PyTEventContextObject *)type->tp_alloc(type, 0);
+    ret->ev_ctx = ev_ctx;
+    return (PyObject *)ret;
 }
 
-static PyObject *py_event_ctx_loop_once(py_talloc_Object *self)
+static PyObject *py_event_ctx_loop_once(PyTEventContextObject *self)
 {
-    return PyInt_FromLong(event_loop_once(self->ptr));
+    return PyInt_FromLong(tevent_loop_once(self->ev_ctx));
 }
 
-static PyObject *py_event_ctx_loop_wait(py_talloc_Object *self)
+static PyObject *py_event_ctx_loop_wait(PyTEventContextObject *self)
 {
-    return PyInt_FromLong(event_loop_wait(self->ptr));
+    return PyInt_FromLong(tevent_loop_wait(self->ev_ctx));
 }
 
 static PyMethodDef py_event_ctx_methods[] = {
@@ -91,11 +98,19 @@ static PyMethodDef py_event_ctx_methods[] = {
     { NULL }
 };
 
-PyTypeObject PyEventContext = {
-    .tp_name = "EventContext",
+static void py_event_ctx_dealloc(PyTEventContextObject * self)
+{
+       talloc_free(self->ev_ctx);
+       self->ob_type->tp_free(self);
+}
+
+
+PyTypeObject PyTEventContext = {
+    .tp_name = "TEventContext",
     .tp_methods = py_event_ctx_methods,
-    .tp_basicsize = sizeof(py_talloc_Object),
-    .tp_dealloc = py_talloc_dealloc,
+    .tp_basicsize = sizeof(PyTEventContextObject),
+    .tp_dealloc = (destructor)py_event_ctx_dealloc,
+    .tp_flags = Py_TPFLAGS_DEFAULT,
     .tp_new = py_event_ctx_new,
 };
 
@@ -103,8 +118,14 @@ void inittevent(void)
 {
     PyObject *m;
 
+    if (PyType_Ready(&PyTEventContext) < 0)
+       return;
+
     m = Py_InitModule3("tevent", tevent_methods, "Event management.");
     if (m == NULL)
         return;
+
+    Py_INCREF(&PyTEventContext);
+    PyModule_AddObject(m, "TEventContext", (PyObject *)&PyTEventContext);
 }