Python: Add kore.app().

This method allows you to set a Python object and obtain it
by calling the method again without any arguments.

eg:

foo = SomeClass()

kore.app(foo)

foo = kore.app()
This commit is contained in:
Joris Vink 2020-07-05 21:47:22 +02:00
parent e38c6e5d30
commit 8235759bca
2 changed files with 26 additions and 0 deletions

View File

@ -34,6 +34,7 @@ struct python_coro {
TAILQ_HEAD(coro_list, python_coro);
static PyObject *python_kore_app(PyObject *, PyObject *);
static PyObject *python_kore_log(PyObject *, PyObject *);
static PyObject *python_kore_time(PyObject *, PyObject *);
static PyObject *python_kore_lock(PyObject *, PyObject *);
@ -80,6 +81,7 @@ static PyObject *python_websocket_broadcast(PyObject *, PyObject *);
#define GETSET(n, g, s) { n, (getter)g, (setter)s, NULL, NULL }
static struct PyMethodDef pykore_methods[] = {
METHOD("app", python_kore_app, METH_VARARGS),
METHOD("log", python_kore_log, METH_VARARGS),
METHOD("time", python_kore_time, METH_NOARGS),
METHOD("lock", python_kore_lock, METH_NOARGS),

View File

@ -266,6 +266,7 @@ static struct coro_list coro_suspended;
extern const char *__progname;
static PyObject *pickle = NULL;
static PyObject *kore_app = NULL;
static PyObject *pickle_dumps = NULL;
static PyObject *pickle_loads = NULL;
static PyObject *python_tracer = NULL;
@ -1641,6 +1642,29 @@ python_kore_pgsql_register(PyObject *self, PyObject *args)
}
#endif
static PyObject *
python_kore_app(PyObject *self, PyObject *args)
{
PyObject *obj;
if (!PyArg_ParseTuple(args, "O", &obj)) {
PyErr_Clear();
if (kore_app == NULL)
Py_RETURN_NONE;
Py_INCREF(kore_app);
return (kore_app);
}
Py_XDECREF(kore_app);
kore_app = obj;
Py_INCREF(kore_app);
Py_RETURN_TRUE;
}
static PyObject *
python_kore_log(PyObject *self, PyObject *args)
{