py_tevent: add_timer takes float argument
[kai/samba-autobuild/.git] / lib / tevent / pytevent.c
index 5725ae3d75201863c672a7470c3b1112d01a884a..97976df32d3810a29e3c2fcfecce01590233764c 100644 (file)
@@ -23,6 +23,7 @@
 */
 
 #include <Python.h>
+#include "replace.h"
 #include <tevent.h>
 
 #if PY_MAJOR_VERSION >= 3
@@ -187,7 +188,7 @@ static PyObject *py_register_backend(PyObject *self, PyObject *args)
                return NULL;
        }
 
-       if (!PyStr_Check(name)) {
+       if (!(PyStr_Check(name) || PyUnicode_Check(name))) {
                PyErr_SetNone(PyExc_TypeError);
                Py_DECREF(name);
                return NULL;
@@ -230,7 +231,7 @@ static void py_queue_trigger(struct tevent_req *req, void *private_data)
 {
        PyObject *callback = private_data, *ret;
 
-       ret = PyObject_CallFunction(callback, "");
+       ret = PyObject_CallFunction(callback, discard_const_p(char, ""));
        Py_XDECREF(ret);
 }
 
@@ -295,38 +296,6 @@ static PyObject *py_tevent_context_loop_once(TeventContext_Object *self)
        Py_RETURN_NONE;
 }
 
-#ifdef TEVENT_DEPRECATED
-static bool py_tevent_finished(PyObject *callback)
-{
-       PyObject *py_ret;
-       bool ret;
-
-       py_ret = PyObject_CallFunction(callback, "");
-       if (py_ret == NULL)
-               return true;
-       ret = PyObject_IsTrue(py_ret);
-       Py_DECREF(py_ret);
-       return ret;
-}
-
-static PyObject *py_tevent_context_loop_until(TeventContext_Object *self, PyObject *args)
-{
-       PyObject *callback;
-       if (!PyArg_ParseTuple(args, "O", &callback))
-               return NULL;
-
-       if (tevent_loop_until(self->ev, py_tevent_finished, callback) != 0) {
-               PyErr_SetNone(PyExc_RuntimeError);
-               return NULL;
-       }
-
-       if (PyErr_Occurred())
-               return NULL;
-
-       Py_RETURN_NONE;
-}
-#endif
-
 static void py_tevent_signal_handler(struct tevent_context *ev,
                                        struct tevent_signal *se,
                                        int signum,
@@ -336,7 +305,7 @@ static void py_tevent_signal_handler(struct tevent_context *ev,
 {
        PyObject *callback = (PyObject *)private_data, *ret;
 
-       ret = PyObject_CallFunction(callback, "ii", signum, count);
+       ret = PyObject_CallFunction(callback, discard_const_p(char, "ii"), signum, count);
        Py_XDECREF(ret);
 }
 
@@ -387,7 +356,7 @@ static void py_timer_handler(struct tevent_context *ev,
        TeventTimer_Object *self = private_data;
        PyObject *ret;
 
-       ret = PyObject_CallFunction(self->callback, "l", te);
+       ret = PyObject_CallFunction(self->callback, discard_const_p(char, "l"), te);
        if (ret == NULL) {
                /* No Python stack to propagate exception to; just print traceback */
                PyErr_PrintEx(0);
@@ -416,9 +385,9 @@ static PyObject* py_tevent_timer_get_active(TeventTimer_Object *self) {
 
 struct PyGetSetDef py_tevent_timer_getset[] = {
        {
-               .name = "active",
+               .name = discard_const_p(char, "active"),
                .get = (getter)py_tevent_timer_get_active,
-               .doc = "true if the timer is scheduled to run",
+               .doc = discard_const_p(char, "true if the timer is scheduled to run"),
        },
        {NULL},
 };
@@ -432,11 +401,14 @@ static PyTypeObject TeventTimer_Type = {
        .tp_flags = Py_TPFLAGS_DEFAULT,
 };
 
-static int timer_destructor(void* ptr)
+struct TeventTimer_Object_ref {
+       TeventTimer_Object *obj;
+};
+
+static int TeventTimer_Object_ref_destructor(struct TeventTimer_Object_ref *ref)
 {
-       TeventTimer_Object *obj = *(TeventTimer_Object **)ptr;
-       obj->timer = NULL;
-       Py_DECREF(obj);
+       ref->obj->timer = NULL;
+       Py_DECREF(ref->obj);
        return 0;
 }
 
@@ -471,7 +443,7 @@ static PyObject *py_tevent_context_add_timer_internal(TeventContext_Object *self
         * The Python timer holds a reference to the callback.
         */
        TeventTimer_Object *ret;
-       TeventTimer_Object **tmp_context;
+       struct TeventTimer_Object_ref *ref;
 
        ret = PyObject_New(TeventTimer_Object, &TeventTimer_Type);
        if (ret == NULL) {
@@ -487,16 +459,17 @@ static PyObject *py_tevent_context_add_timer_internal(TeventContext_Object *self
                PyErr_SetString(PyExc_RuntimeError, "Could not initialize timer");
                return NULL;
        }
-       tmp_context = talloc(ret->timer, TeventTimer_Object*);
-       if (tmp_context == NULL) {
+       ref = talloc(ret->timer, struct TeventTimer_Object_ref);
+       if (ref == NULL) {
                talloc_free(ret->timer);
                Py_DECREF(ret);
                PyErr_SetString(PyExc_RuntimeError, "Could not initialize timer");
                return NULL;
        }
        Py_INCREF(ret);
-       *tmp_context = ret;
-       talloc_set_destructor(tmp_context, timer_destructor);
+       ref->obj = ret;
+
+       talloc_set_destructor(ref, TeventTimer_Object_ref_destructor);
 
        return (PyObject *)ret;
 }
@@ -505,9 +478,13 @@ static PyObject *py_tevent_context_add_timer(TeventContext_Object *self, PyObjec
 {
        struct timeval next_event;
        PyObject *callback;
-       if (!PyArg_ParseTuple(args, "lO", &next_event, &callback))
+       double secs, usecs;
+       if (!PyArg_ParseTuple(args, "dO", &secs, &callback)){
                return NULL;
-
+       }
+       next_event.tv_sec = secs;
+       usecs = (secs - next_event.tv_sec) * 1000000.0;
+       next_event.tv_usec = usecs;
        return py_tevent_context_add_timer_internal(self, next_event, callback);
 }
 
@@ -533,7 +510,7 @@ static void py_fd_handler(struct tevent_context *ev,
 {
        PyObject *callback = private_data, *ret;
 
-       ret = PyObject_CallFunction(callback, "i", flags);
+       ret = PyObject_CallFunction(callback, discard_const_p(char, "i"), flags);
        Py_XDECREF(ret);
 }
 
@@ -576,14 +553,6 @@ static PyObject *py_tevent_context_add_fd(TeventContext_Object *self, PyObject *
        return (PyObject *)ret;
 }
 
-#ifdef TEVENT_DEPRECATED
-static PyObject *py_tevent_context_set_allow_nesting(TeventContext_Object *self)
-{
-       tevent_loop_allow_nesting(self->ev);
-       Py_RETURN_NONE;
-}
-#endif
-
 static PyMethodDef py_tevent_context_methods[] = {
        { "reinitialise", (PyCFunction)py_tevent_context_reinitialise, METH_NOARGS,
                "S.reinitialise()" },
@@ -593,10 +562,6 @@ static PyMethodDef py_tevent_context_methods[] = {
                METH_NOARGS, "S.loop_wait()" },
        { "loop_once", (PyCFunction)py_tevent_context_loop_once,
                METH_NOARGS, "S.loop_once()" },
-#ifdef TEVENT_DEPRECATED
-       { "loop_until", (PyCFunction)py_tevent_context_loop_until,
-               METH_VARARGS, "S.loop_until(callback)" },
-#endif
        { "add_signal", (PyCFunction)py_tevent_context_add_signal,
                METH_VARARGS, "S.add_signal(signum, sa_flags, handler) -> signal" },
        { "add_timer", (PyCFunction)py_tevent_context_add_timer,
@@ -605,10 +570,6 @@ static PyMethodDef py_tevent_context_methods[] = {
                METH_VARARGS, "S.add_timer(offset_seconds, handler) -> timer" },
        { "add_fd", (PyCFunction)py_tevent_context_add_fd, 
                METH_VARARGS, "S.add_fd(fd, flags, handler) -> fd" },
-#ifdef TEVENT_DEPRECATED
-       { "allow_nesting", (PyCFunction)py_tevent_context_set_allow_nesting, 
-               METH_NOARGS, "Whether to allow nested tevent loops." },
-#endif
        { NULL },
 };
 
@@ -643,8 +604,11 @@ static PyObject *py_tevent_req_is_in_progress(PyObject *self)
 }
 
 static PyGetSetDef py_tevent_req_getsetters[] = {
-       { "in_progress", (getter)py_tevent_req_is_in_progress, NULL,
-               "Whether the request is in progress" },
+       {
+               .name = discard_const_p(char, "in_progress"),
+               .get = (getter)py_tevent_req_is_in_progress,
+               .doc = discard_const_p(char, "Whether the request is in progress"),
+       },
        { NULL }
 };
 
@@ -732,8 +696,11 @@ static PyObject *py_tevent_queue_get_length(TeventQueue_Object *self)
 }
 
 static PyGetSetDef py_tevent_queue_getsetters[] = {
-       { "length", (getter)py_tevent_queue_get_length,
-               NULL, "The number of elements in the queue." },
+       {
+               .name = discard_const_p(char, "length"),
+               .get = (getter)py_tevent_queue_get_length,
+               .doc = discard_const_p(char, "The number of elements in the queue."),
+       },
        { NULL },
 };
 
@@ -759,8 +726,11 @@ static PyObject *py_tevent_context_signal_support(PyObject *_self)
 }
 
 static PyGetSetDef py_tevent_context_getsetters[] = {
-       { "signal_support", (getter)py_tevent_context_signal_support,
-               NULL, "if this platform and tevent context support signal handling" },
+       {
+               .name = discard_const_p(char, "signal_support"),
+               .get = (getter)py_tevent_context_signal_support,
+               .doc = discard_const_p(char, "if this platform and tevent context support signal handling"),
+       },
        { NULL }
 };
 
@@ -777,7 +747,7 @@ static PyObject *py_tevent_context_new(PyTypeObject *type, PyObject *args, PyObj
        struct tevent_context *ev;
        TeventContext_Object *ret;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwnames, &name))
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &name))
                return NULL;
 
        if (name == NULL) {