X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fpyvalues.c;h=301df4429f0b96fb816c6bbed65b7c070a70ab08;hp=9c8809da497b704d0fcb66c4361e0d556c420910;hb=06a86a60a7dabc685bdbd81ce3d36ea5f7e2c2d4;hpb=acb5a3393b5ffcd73eeaa06c91e98e4ba9a58469 diff --git a/src/pyvalues.c b/src/pyvalues.c index 9c8809da..301df442 100644 --- a/src/pyvalues.c +++ b/src/pyvalues.c @@ -38,7 +38,7 @@ typedef struct { int (*add_signed_int)(void *, const char *, int64_t); int (*add_unsigned_int)(void *, const char *, uint64_t); int (*add_double)(void *, const char *, double); - int (*add_boolean)(void *, const char *, _Bool); + int (*add_boolean)(void *, const char *, bool); } cpy_build_meta_handler_t; #define FreeAll() \ @@ -50,31 +50,10 @@ 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, - *l_plugin_instance = NULL; - static PyObject *l_host = NULL, *l_time = NULL; + static PyObject *l_type, *l_type_instance, *l_plugin, *l_plugin_instance; + static PyObject *l_host, *l_time; PluginData *self = (PluginData *)s; if (l_type == NULL) @@ -223,7 +202,7 @@ static int PluginData_init(PyObject *s, PyObject *args, PyObject *kwds) { static PyObject *PluginData_repr(PyObject *s) { PyObject *ret; - static PyObject *l_closing = NULL; + static PyObject *l_closing; if (l_closing == NULL) l_closing = cpy_string_to_unicode_or_bytes(")"); @@ -485,8 +464,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,17 +551,39 @@ 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(); - cpy_build_meta(meta, &cpy_meta_data, (void *)m); + if (cpy_build_meta_generic(meta, &cpy_meta_data, (void *)m) < 0) { + meta_data_destroy(m); + return NULL; + } return m; } 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) { @@ -638,7 +640,8 @@ static PyObject *Values_dispatch(Values *self, PyObject *args, PyObject *kwds) { } size = (size_t)PySequence_Length(values); if (size != ds->ds_num) { - PyErr_Format(PyExc_RuntimeError, "type %s needs %zu values, got %zu", + PyErr_Format(PyExc_RuntimeError, + "type %s needs %" PRIsz " values, got %" PRIsz, value_list.type, ds->ds_num, size); return NULL; } @@ -691,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); @@ -762,7 +765,8 @@ static PyObject *Values_write(Values *self, PyObject *args, PyObject *kwds) { } size = (size_t)PySequence_Length(values); if (size != ds->ds_num) { - PyErr_Format(PyExc_RuntimeError, "type %s needs %zu values, got %zu", + PyErr_Format(PyExc_RuntimeError, + "type %s needs %" PRIsz " values, got %" PRIsz, value_list.type, ds->ds_num, size); return NULL; } @@ -818,7 +822,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) @@ -838,8 +842,7 @@ static PyObject *Values_write(Values *self, PyObject *args, PyObject *kwds) { static PyObject *Values_repr(PyObject *s) { PyObject *ret, *tmp; - static PyObject *l_interval = NULL, *l_values = NULL, *l_meta = NULL, - *l_closing = NULL; + static PyObject *l_interval, *l_values, *l_meta, *l_closing; Values *self = (Values *)s; if (l_interval == NULL) @@ -985,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) { @@ -1043,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, @@ -1143,8 +1144,7 @@ static int Notification_setstring(PyObject *self, PyObject *value, void *data) { static PyObject *Notification_repr(PyObject *s) { PyObject *ret, *tmp; - static PyObject *l_severity = NULL, *l_message = NULL, *l_meta = NULL, - *l_closing = NULL; + static PyObject *l_severity, *l_message, *l_meta, *l_closing; Notification *self = (Notification *)s; if (l_severity == NULL) @@ -1207,7 +1207,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[] = { @@ -1216,43 +1217,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[] = @@ -1278,7 +1279,7 @@ PyTypeObject SignedType = { 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ Signed_doc /* tp_doc */ }; @@ -1305,6 +1306,6 @@ PyTypeObject UnsignedType = { 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ Unsigned_doc /* tp_doc */ };