From cb262c0e23692b6374d57ad3b3decb0d6c397949 Mon Sep 17 00:00:00 2001 From: "Mytnyk, Volodymyr" Date: Tue, 10 Oct 2017 16:13:46 +0100 Subject: [PATCH] python: Addressed review comments - Run clang-format Signed-off-by: Mytnyk, Volodymyr --- src/python.c | 40 ++++++++----- src/pyvalues.c | 151 ++++++++++++++++++++++++------------------------- 2 files changed, 100 insertions(+), 91 deletions(-) diff --git a/src/python.c b/src/python.c index a8f5177f..cb6698d8 100644 --- a/src/python.c +++ b/src/python.c @@ -446,19 +446,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; @@ -511,11 +513,12 @@ static int cpy_notification_callback(const notification_t *notification, CPY_LOCK_THREADS PyObject *dict = PyDict_New(); /* New reference. */ - for (notification_meta_t *meta = notification->meta; - meta != NULL; meta = meta->next) { + 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. */ + 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) { @@ -524,19 +527,22 @@ static int cpy_notification_callback(const notification_t *notification, (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); + 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); + meta->nm_value.nm_boolean ? Py_True : Py_False); } } notify = Notification_New(); /* New reference. */ @@ -682,8 +688,9 @@ static PyObject *cpy_get_dataset(PyObject *self, PyObject *args) { for (size_t i = 0; i < ds->ds_num; ++i) { tuple = PyTuple_New(4); PyTuple_SET_ITEM(tuple, 0, cpy_string_to_unicode_or_bytes(ds->ds[i].name)); - PyTuple_SET_ITEM(tuple, 1, cpy_string_to_unicode_or_bytes( - DS_TYPE_TO_STRING(ds->ds[i].type))); + PyTuple_SET_ITEM( + tuple, 1, + cpy_string_to_unicode_or_bytes(DS_TYPE_TO_STRING(ds->ds[i].type))); PyTuple_SET_ITEM(tuple, 2, float_or_none(ds->ds[i].min)); PyTuple_SET_ITEM(tuple, 3, float_or_none(ds->ds[i].max)); PyList_SET_ITEM(list, i, tuple); @@ -751,7 +758,8 @@ static PyObject *cpy_register_generic_userdata(void *reg, void *handler, register_function(buf, handler, &(user_data_t){ - .data = c, .free_func = cpy_destroy_user_data, + .data = c, + .free_func = cpy_destroy_user_data, }); ++cpy_num_callbacks; @@ -794,7 +802,8 @@ static PyObject *cpy_register_read(PyObject *self, PyObject *args, /* group = */ "python", buf, cpy_read_callback, DOUBLE_TO_CDTIME_T(interval), &(user_data_t){ - .data = c, .free_func = cpy_destroy_user_data, + .data = c, + .free_func = cpy_destroy_user_data, }); ++cpy_num_callbacks; return cpy_string_to_unicode_or_bytes(buf); @@ -1163,8 +1172,9 @@ static PyObject *cpy_oconfig_to_pyconfig(oconfig_item_t *ci, PyObject *parent) { values = PyTuple_New(ci->values_num); /* New reference. */ for (int i = 0; i < ci->values_num; ++i) { if (ci->values[i].type == OCONFIG_TYPE_STRING) { - PyTuple_SET_ITEM(values, i, cpy_string_to_unicode_or_bytes( - ci->values[i].value.string)); + PyTuple_SET_ITEM( + values, i, + cpy_string_to_unicode_or_bytes(ci->values[i].value.string)); } else if (ci->values[i].type == OCONFIG_TYPE_NUMBER) { PyTuple_SET_ITEM(values, i, PyFloat_FromDouble(ci->values[i].value.number)); diff --git a/src/pyvalues.c b/src/pyvalues.c index 5dcdf6a2..d1bdeddd 100644 --- a/src/pyvalues.c +++ b/src/pyvalues.c @@ -50,26 +50,6 @@ typedef struct { PyMem_Free(host); \ } while (0) -#define CPY_BUILD_META_FUNC(meta_type, func, val_type) \ -static int cpy_ ## func(void *meta, const char *key, val_type val) { \ - return func((meta_type *)meta, key, val); \ -} - -#define CPY_BUILD_META_HANDLER(func_prefix, meta_type) \ -CPY_BUILD_META_FUNC(meta_type, func_prefix ## _add_string, const char *) \ -CPY_BUILD_META_FUNC(meta_type, func_prefix ## _add_signed_int, int64_t) \ -CPY_BUILD_META_FUNC(meta_type, func_prefix ## _add_unsigned_int, uint64_t) \ -CPY_BUILD_META_FUNC(meta_type, func_prefix ## _add_double, double) \ -CPY_BUILD_META_FUNC(meta_type, func_prefix ## _add_boolean, _Bool) \ - \ -static cpy_build_meta_handler_t cpy_ ## func_prefix = { \ - .add_string = cpy_ ## func_prefix ## _add_string, \ - .add_signed_int = cpy_ ## func_prefix ## _add_signed_int, \ - .add_unsigned_int = cpy_ ## func_prefix ## _add_unsigned_int, \ - .add_double = cpy_ ## func_prefix ## _add_double, \ - .add_boolean = cpy_ ## func_prefix ## _add_boolean \ -} - static PyObject *cpy_common_repr(PyObject *s) { PyObject *ret, *tmp; static PyObject *l_type = NULL, *l_type_instance = NULL, *l_plugin = NULL, @@ -485,8 +465,9 @@ static int Values_init(PyObject *s, PyObject *args, PyObject *kwds) { return 0; } -static int cpy_build_meta(PyObject *meta, cpy_build_meta_handler_t *meta_func, - void *m) { +static int cpy_build_meta_generic(PyObject *meta, + cpy_build_meta_handler_t *meta_func, + void *m) { int s; PyObject *l; @@ -571,12 +552,31 @@ static int cpy_build_meta(PyObject *meta, cpy_build_meta_handler_t *meta_func, return 0; } +#define CPY_BUILD_META_FUNC(meta_type, func, val_type) \ + static int cpy_##func(void *meta, const char *key, val_type val) { \ + return func((meta_type *)meta, key, val); \ + } + +#define CPY_BUILD_META_HANDLER(func_prefix, meta_type) \ + CPY_BUILD_META_FUNC(meta_type, func_prefix##_add_string, const char *) \ + CPY_BUILD_META_FUNC(meta_type, func_prefix##_add_signed_int, int64_t) \ + CPY_BUILD_META_FUNC(meta_type, func_prefix##_add_unsigned_int, uint64_t) \ + CPY_BUILD_META_FUNC(meta_type, func_prefix##_add_double, double) \ + CPY_BUILD_META_FUNC(meta_type, func_prefix##_add_boolean, _Bool) \ + \ + static cpy_build_meta_handler_t cpy_##func_prefix = { \ + .add_string = cpy_##func_prefix##_add_string, \ + .add_signed_int = cpy_##func_prefix##_add_signed_int, \ + .add_unsigned_int = cpy_##func_prefix##_add_unsigned_int, \ + .add_double = cpy_##func_prefix##_add_double, \ + .add_boolean = cpy_##func_prefix##_add_boolean} + CPY_BUILD_META_HANDLER(meta_data, meta_data_t); CPY_BUILD_META_HANDLER(plugin_notification_meta, notification_t); -static meta_data_t *cpy_build_value_meta(PyObject *meta) { +static meta_data_t *cpy_build_meta(PyObject *meta) { meta_data_t *m = meta_data_create(); - if (cpy_build_meta(meta, &cpy_meta_data, (void *)m) < 0) { + if (cpy_build_meta_generic(meta, &cpy_meta_data, (void *)m) < 0) { meta_data_destroy(m); return NULL; } @@ -584,7 +584,7 @@ static meta_data_t *cpy_build_value_meta(PyObject *meta) { } static void cpy_build_notification_meta(notification_t *n, PyObject *meta) { - cpy_build_meta(meta, &cpy_plugin_notification_meta, (void *)n); + cpy_build_meta_generic(meta, &cpy_plugin_notification_meta, (void *)n); } static PyObject *Values_dispatch(Values *self, PyObject *args, PyObject *kwds) { @@ -694,7 +694,7 @@ static PyObject *Values_dispatch(Values *self, PyObject *args, PyObject *kwds) { } } value_list.values = value; - value_list.meta = cpy_build_value_meta(meta); + value_list.meta = cpy_build_meta(meta); value_list.values_len = size; value_list.time = DOUBLE_TO_CDTIME_T(time); value_list.interval = DOUBLE_TO_CDTIME_T(interval); @@ -821,7 +821,7 @@ static PyObject *Values_write(Values *self, PyObject *args, PyObject *kwds) { value_list.values_len = size; value_list.time = DOUBLE_TO_CDTIME_T(time); value_list.interval = DOUBLE_TO_CDTIME_T(interval); - value_list.meta = cpy_build_value_meta(meta); + value_list.meta = cpy_build_meta(meta); if (value_list.host[0] == 0) sstrncpy(value_list.host, hostname_g, sizeof(value_list.host)); if (value_list.plugin[0] == 0) @@ -988,15 +988,14 @@ static int Notification_init(PyObject *s, PyObject *args, PyObject *kwds) { PyObject *meta = NULL; char *type = NULL, *plugin_instance = NULL, *type_instance = NULL, *plugin = NULL, *host = NULL; - static char *kwlist[] = {"type", "message", "plugin_instance", - "type_instance", "plugin", "host", - "time", "severity", "meta", - NULL}; + static char *kwlist[] = { + "type", "message", "plugin_instance", "type_instance", "plugin", + "host", "time", "severity", "meta", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etetetetetetdiO", kwlist, NULL, - &type, NULL, &message, NULL, - &plugin_instance, NULL, &type_instance, NULL, - &plugin, NULL, &host, &time, &severity, &meta)) + if (!PyArg_ParseTupleAndKeywords( + args, kwds, "|etetetetetetdiO", kwlist, NULL, &type, NULL, &message, + NULL, &plugin_instance, NULL, &type_instance, NULL, &plugin, NULL, + &host, &time, &severity, &meta)) return -1; if (type && plugin_get_ds(type) == NULL) { @@ -1046,10 +1045,9 @@ static PyObject *Notification_dispatch(Notification *self, PyObject *args, *type_instance = NULL; char *message = NULL; - static char *kwlist[] = {"type", "message", "plugin_instance", - "type_instance", "plugin", "host", - "time", "severity", "meta", - NULL}; + static char *kwlist[] = { + "type", "message", "plugin_instance", "type_instance", "plugin", + "host", "time", "severity", "meta", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etetetetetetdiO", kwlist, NULL, &type, NULL, &message, NULL, &plugin_instance, NULL, &type_instance, NULL, @@ -1210,7 +1208,8 @@ static PyMethodDef Notification_methods[] = { static PyMemberDef Notification_members[] = { {"severity", T_INT, offsetof(Notification, severity), 0, severity_doc}, - {"meta", T_OBJECT_EX, offsetof(Notification, meta), 0, notification_meta_doc}, + {"meta", T_OBJECT_EX, offsetof(Notification, meta), 0, + notification_meta_doc}, {NULL}}; static PyGetSetDef Notification_getseters[] = { @@ -1219,43 +1218,43 @@ static PyGetSetDef Notification_getseters[] = { {NULL}}; PyTypeObject NotificationType = { - CPY_INIT_TYPE "collectd.Notification", /* tp_name */ - sizeof(Notification), /* tp_basicsize */ - 0, /* Will be filled in later */ - Notification_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_compare */ - Notification_repr, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ + CPY_INIT_TYPE "collectd.Notification", /* tp_name */ + sizeof(Notification), /* tp_basicsize */ + 0, /* Will be filled in later */ + Notification_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + Notification_repr, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ - Notification_doc, /* tp_doc */ - Notification_traverse, /* tp_traverse */ - Notification_clear, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - Notification_methods, /* tp_methods */ - Notification_members, /* tp_members */ - Notification_getseters, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - Notification_init, /* tp_init */ - 0, /* tp_alloc */ - Notification_new /* tp_new */ + Notification_doc, /* tp_doc */ + Notification_traverse, /* tp_traverse */ + Notification_clear, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Notification_methods, /* tp_methods */ + Notification_members, /* tp_members */ + Notification_getseters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + Notification_init, /* tp_init */ + 0, /* tp_alloc */ + Notification_new /* tp_new */ }; static char Signed_doc[] = -- 2.30.2