From 5c8efde84184739b26ad8d3bdf8ec113799bf34a Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Fri, 16 Nov 2018 11:07:21 +0100 Subject: [PATCH] allow foo.method symbols in python. this way you can create page handlers that reside inside of other objects. eg: static / restapi.index --- src/python.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/python.c b/src/python.c index 2b5f92d..5624f71 100644 --- a/src/python.c +++ b/src/python.c @@ -1310,17 +1310,40 @@ python_import(const char *path) static PyObject * python_callable(PyObject *module, const char *symbol) { - PyObject *obj; + char *base, *method; + PyObject *res, *obj, *meth; - if ((obj = PyObject_GetAttrString(module, symbol)) == NULL) - return (NULL); + res = NULL; + obj = NULL; + base = kore_strdup(symbol); + + if ((method = strchr(base, '.')) != NULL) + *(method)++ = '\0'; + + if ((obj = PyObject_GetAttrString(module, base)) == NULL) + goto out; + + if (method != NULL) { + if ((meth = PyObject_GetAttrString(obj, method)) == NULL) + goto out; - if (!PyCallable_Check(obj)) { Py_DECREF(obj); - return (NULL); + obj = meth; } - return (obj); + if (!PyCallable_Check(obj)) + goto out; + + res = obj; + obj = NULL; + +out: + if (obj != NULL) + Py_DECREF(obj); + + kore_free(base); + + return (res); } static PyObject *