From: hstuart Date: Fri, 9 Oct 2009 12:02:58 +0000 (+0000) Subject: mod-python: add emb_timeq_add function X-Git-Tag: 1.9~155 X-Git-Url: https://jfr.im/git/irc/evilnet/x3.git/commitdiff_plain/8f206d2245d9f2d2dab7a79d3bf02bd6c5f08517?ds=sidebyside mod-python: add emb_timeq_add function --- diff --git a/ChangeLog b/ChangeLog index 9f0ed80..eb58351 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,11 @@ /*********************************************************************** X3 ChangeLog -1009-10-07 Henrik Stuart +2009-10-09 Henrik Stuart + + * src/mod-python.c: add emb_timeq_add function. + +2009-10-07 Henrik Stuart * src/mod-python.c: refactor dict_t construction logic. diff --git a/src/mod-python.c b/src/mod-python.c index 80c9401..231a9b1 100644 --- a/src/mod-python.c +++ b/src/mod-python.c @@ -669,6 +669,58 @@ emb_kill(UNUSED_ARG(PyObject* self), PyObject* args) { return Py_None; } +struct py_timeq_extra { + PyObject* func; + PyObject* arg; +}; + +static +void py_timeq_callback(void* data) { + struct py_timeq_extra* extra = (struct py_timeq_extra*)data; + + PyObject* retval = PyObject_Call(extra->func, extra->arg, NULL); + Py_XDECREF(retval); + + Py_DECREF(extra->func); + Py_DECREF(extra->arg); +} + +static PyObject* +emb_timeq_add(UNUSED_ARG(PyObject* self), PyObject* args) { + time_t when; + PyObject* func, *arg; + struct py_timeq_extra* extra; + + if (!PyArg_ParseTuple(args, "lOO", &when, &func, &arg)) + return NULL; + + if (!PyFunction_Check(func)) { + PyErr_SetString(PyExc_Exception, "first argument must be a function"); + return NULL; + } + + if (!PyTuple_Check(arg)) { + PyErr_SetString(PyExc_Exception, "second argument must be a tuple"); + return NULL; + } + + extra = malloc(sizeof(struct py_timeq_extra)); + if (extra == NULL) { + PyErr_SetString(PyExc_Exception, "out of memory"); + return NULL; + } + + Py_INCREF(func); + Py_INCREF(arg); + + extra->func = func; + extra->arg = arg; + + timeq_add(when, py_timeq_callback, (void*)extra); + + return Py_None; +} + static PyMethodDef EmbMethods[] = { /* Communication methods */ {"dump", emb_dump, METH_VARARGS, "Dump raw P10 line to server"}, @@ -689,7 +741,7 @@ static PyMethodDef EmbMethods[] = { //TODO: {"get_config", emb_get_config, METH_VARARGS, "get x3.conf settings into a nested dict"}, //TODO: {"config_set", emb_config_set, METH_VARARGS, "change a config setting 'on-the-fly'."}, // -//TODO: {"timeq_add", emb_timeq_new, METH_VARARGS, "some kind of interface to the timed event system."}, + {"timeq_add", emb_timeq_add, METH_VARARGS, "add function to callback to the event system"}, //TODO: {"timeq_del", emb_timeq_new, METH_VARARGS, "some kind of interface to the timed event system."}, /* Information gathering methods */ {"get_user", emb_get_user, METH_VARARGS, "Get details about a nickname"},