X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fpython.c;h=5669f45a1806dce01871f6813e3d8e9cb1850818;hb=abaa1c8a24e8eff5632dd6052b1da5f6535caf19;hp=32c5847df6133e329b1d42b1996395c37f720c3a;hpb=07c09de5e28055a5a0f43067d51211e97039fa82;p=collectd.git diff --git a/src/python.c b/src/python.c index 32c5847d..5669f45a 100644 --- a/src/python.c +++ b/src/python.c @@ -31,7 +31,7 @@ #include "collectd.h" -#include "common.h" +#include "utils/common/common.h" #include "cpython.h" @@ -241,7 +241,7 @@ static char CollectdError_doc[] = static pthread_t main_thread; static PyOS_sighandler_t python_sigint_handler; -static _Bool do_interactive = 0; +static bool do_interactive; /* This is our global thread state. Python saves some stuff in thread-local * storage. So if we allow the interpreter to run in the background @@ -257,8 +257,8 @@ static cpy_callback_t *cpy_init_callbacks; static cpy_callback_t *cpy_shutdown_callbacks; /* Make sure to hold the GIL while modifying these. */ -static int cpy_shutdown_triggered = 0; -static int cpy_num_callbacks = 0; +static int cpy_shutdown_triggered; +static int cpy_num_callbacks; static void cpy_destroy_user_data(void *data) { cpy_callback_t *c = data; @@ -285,7 +285,7 @@ static void cpy_build_name(char *buf, size_t size, PyObject *callback, PyObject *mod = NULL; if (name != NULL) { - snprintf(buf, size, "python.%s", name); + ssnprintf(buf, size, "python.%s", name); return; } @@ -294,14 +294,14 @@ static void cpy_build_name(char *buf, size_t size, PyObject *callback, module = cpy_unicode_or_bytes_to_string(&mod); if (module != NULL) { - snprintf(buf, size, "python.%s", module); + ssnprintf(buf, size, "python.%s", module); Py_XDECREF(mod); PyErr_Clear(); return; } Py_XDECREF(mod); - snprintf(buf, size, "python.%p", callback); + ssnprintf(buf, size, "python.%p", callback); PyErr_Clear(); } @@ -367,7 +367,7 @@ void cpy_log_exception(const char *context) { continue; if (cpy[strlen(cpy) - 1] == '\n') - cpy[strlen(cpy) - 1] = 0; + cpy[strlen(cpy) - 1] = '\0'; Py_BEGIN_ALLOW_THREADS; ERROR("%s", cpy); @@ -438,7 +438,7 @@ static int cpy_write_callback(const data_set_t *ds, } dict = PyDict_New(); /* New reference. */ if (value_list->meta) { - char **table; + char **table = NULL; meta_data_t *meta = value_list->meta; int num = meta_data_toc(meta, &table); @@ -448,7 +448,7 @@ static int cpy_write_callback(const data_set_t *ds, int64_t si; uint64_t ui; double d; - _Bool b; + bool b; type = meta_data_type(meta, table[i]); if (type == MD_TYPE_STRING) { @@ -461,19 +461,21 @@ static int cpy_write_callback(const data_set_t *ds, } else if (type == MD_TYPE_SIGNED_INT) { if (meta_data_get_signed_int(meta, table[i], &si)) continue; - temp = PyObject_CallFunctionObjArgs((void *)&SignedType, - PyLong_FromLongLong(si), + PyObject *sival = PyLong_FromLongLong(si); /* New reference */ + temp = PyObject_CallFunctionObjArgs((void *)&SignedType, sival, (void *)0); /* New reference. */ PyDict_SetItemString(dict, table[i], temp); Py_XDECREF(temp); + Py_XDECREF(sival); } else if (type == MD_TYPE_UNSIGNED_INT) { if (meta_data_get_unsigned_int(meta, table[i], &ui)) continue; - temp = PyObject_CallFunctionObjArgs((void *)&UnsignedType, - PyLong_FromUnsignedLongLong(ui), + PyObject *uval = PyLong_FromUnsignedLongLong(ui); /* New reference */ + temp = PyObject_CallFunctionObjArgs((void *)&UnsignedType, uval, (void *)0); /* New reference. */ PyDict_SetItemString(dict, table[i], temp); Py_XDECREF(temp); + Py_XDECREF(uval); } else if (type == MD_TYPE_DOUBLE) { if (meta_data_get_double(meta, table[i], &d)) continue; @@ -525,6 +527,39 @@ static int cpy_notification_callback(const notification_t *notification, Notification *n; CPY_LOCK_THREADS + PyObject *dict = PyDict_New(); /* New reference. */ + for (notification_meta_t *meta = notification->meta; meta != NULL; + meta = meta->next) { + PyObject *temp = NULL; + if (meta->type == NM_TYPE_STRING) { + temp = cpy_string_to_unicode_or_bytes( + meta->nm_value.nm_string); /* New reference. */ + PyDict_SetItemString(dict, meta->name, temp); + Py_XDECREF(temp); + } else if (meta->type == NM_TYPE_SIGNED_INT) { + PyObject *sival = PyLong_FromLongLong(meta->nm_value.nm_signed_int); + temp = PyObject_CallFunctionObjArgs((void *)&SignedType, sival, + (void *)0); /* New reference. */ + PyDict_SetItemString(dict, meta->name, temp); + Py_XDECREF(temp); + Py_XDECREF(sival); + } else if (meta->type == NM_TYPE_UNSIGNED_INT) { + PyObject *uval = + PyLong_FromUnsignedLongLong(meta->nm_value.nm_unsigned_int); + temp = PyObject_CallFunctionObjArgs((void *)&UnsignedType, uval, + (void *)0); /* New reference. */ + PyDict_SetItemString(dict, meta->name, temp); + Py_XDECREF(temp); + Py_XDECREF(uval); + } else if (meta->type == NM_TYPE_DOUBLE) { + temp = PyFloat_FromDouble(meta->nm_value.nm_double); /* New reference. */ + PyDict_SetItemString(dict, meta->name, temp); + Py_XDECREF(temp); + } else if (meta->type == NM_TYPE_BOOLEAN) { + PyDict_SetItemString(dict, meta->name, + meta->nm_value.nm_boolean ? Py_True : Py_False); + } + } notify = Notification_New(); /* New reference. */ n = (Notification *)notify; sstrncpy(n->data.host, notification->host, sizeof(n->data.host)); @@ -537,6 +572,8 @@ static int cpy_notification_callback(const notification_t *notification, n->data.time = CDTIME_T_TO_DOUBLE(notification->time); sstrncpy(n->message, notification->message, sizeof(n->message)); n->severity = notification->severity; + Py_CLEAR(n->meta); + n->meta = dict; /* Steals a reference. */ ret = PyObject_CallFunctionObjArgs(c->callback, n, c->data, (void *)0); /* New reference. */ Py_XDECREF(notify); @@ -1096,7 +1133,11 @@ static void *cpy_interactive(void *pipefd) { cpy_log_exception("interactive session init"); } cur_sig = PyOS_setsig(SIGINT, python_sigint_handler); +#if PY_VERSION_HEX < 0x03070000 PyOS_AfterFork(); +#else + PyOS_AfterFork_Child(); +#endif PyEval_InitThreads(); close(*(int *)pipefd); PyRun_InteractiveLoop(stdin, ""); @@ -1325,7 +1366,7 @@ static int cpy_config(oconfig_item_t *ci) { #endif sfree(encoding); } else if (strcasecmp(item->key, "LogTraces") == 0) { - _Bool log_traces; + bool log_traces; if (cf_util_get_boolean(item, &log_traces) != 0) { status = 1; continue;