From: Sven Trenkel Date: Fri, 25 Dec 2009 19:33:49 +0000 (+0100) Subject: Better and unicode compatible repr for PluginData. X-Git-Tag: collectd-4.10.0~76^2~6 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=4f1a8b5cab1feba96da5138ae26a97572cf8e6ca;p=collectd.git Better and unicode compatible repr for PluginData. --- diff --git a/src/cpython.h b/src/cpython.h index 10a1a54c..8c7ad653 100644 --- a/src/cpython.h +++ b/src/cpython.h @@ -83,9 +83,9 @@ #define CPY_SUBSTITUTE(func, a, ...) do {\ if ((a) != NULL) {\ - PyObject *tmp = (a);\ + PyObject *__tmp = (a);\ (a) = func(__VA_ARGS__);\ - Py_DECREF(tmp);\ + Py_DECREF(__tmp);\ }\ } while(0) diff --git a/src/pyvalues.c b/src/pyvalues.c index 1304d95c..d459ea93 100644 --- a/src/pyvalues.c +++ b/src/pyvalues.c @@ -100,16 +100,86 @@ static int PluginData_init(PyObject *s, PyObject *args, PyObject *kwds) { return 0; } -/*static PyObject *PluginData_repr(PyObject *s) { +static PyObject *PluginData_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, *l_closing = NULL; PluginData *self = (PluginData *) s; - return PyString_FromFormat("collectd.Values(type='%s%s%s%s%s%s%s%s%s',time=%lu)", self->type, - *self->type_instance ? "',type_instance='" : "", self->type_instance, - *self->plugin ? "',plugin='" : "", self->plugin, - *self->plugin_instance ? "',plugin_instance='" : "", self->plugin_instance, - *self->host ? "',host='" : "", self->host, - (long unsigned) self->time); -}*/ + if (l_type == NULL) + l_type = cpy_string_to_unicode_or_bytes("(type="); + if (l_type_instance == NULL) + l_type_instance = cpy_string_to_unicode_or_bytes(",type_instance="); + if (l_plugin == NULL) + l_plugin = cpy_string_to_unicode_or_bytes(",plugin="); + if (l_plugin_instance == NULL) + l_plugin_instance = cpy_string_to_unicode_or_bytes(",plugin_instance="); + if (l_host == NULL) + l_host = cpy_string_to_unicode_or_bytes(",host="); + if (l_time == NULL) + l_time = cpy_string_to_unicode_or_bytes(",time="); + if (l_closing == NULL) + l_closing = cpy_string_to_unicode_or_bytes(")"); + + if (!l_type || !l_type_instance || !l_plugin || !l_plugin_instance || !l_host || !l_time) + return NULL; + + ret = cpy_string_to_unicode_or_bytes(s->ob_type->tp_name); + + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_type); + tmp = cpy_string_to_unicode_or_bytes(self->type); + CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp); + if (tmp) + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, tmp); + Py_XDECREF(tmp); + + if (self->type_instance[0] != 0) { + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_type_instance); + tmp = cpy_string_to_unicode_or_bytes(self->type_instance); + CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp); + if (tmp) + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, tmp); + Py_XDECREF(tmp); + } + + if (self->plugin[0] != 0) { + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_plugin); + tmp = cpy_string_to_unicode_or_bytes(self->plugin); + CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp); + if (tmp) + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, tmp); + Py_XDECREF(tmp); + } + + if (self->plugin_instance[0] != 0) { + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_plugin_instance); + tmp = cpy_string_to_unicode_or_bytes(self->plugin_instance); + CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp); + if (tmp) + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, tmp); + Py_XDECREF(tmp); + } + + if (self->host[0] != 0) { + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_host); + tmp = cpy_string_to_unicode_or_bytes(self->host); + CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp); + if (tmp) + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, tmp); + Py_XDECREF(tmp); + } + + if (self->time != 0) { + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_time); + tmp = PyInt_FromLong(self->time); + CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp); + if (tmp) + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, tmp); + Py_XDECREF(tmp); + } + CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_closing); + return ret; +} static PyMemberDef PluginData_members[] = { {"time", T_DOUBLE, offsetof(PluginData, time), 0, time_doc}, @@ -188,7 +258,7 @@ PyTypeObject PluginDataType = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ - 0/*PluginData_repr*/, /* tp_repr */ + PluginData_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ @@ -324,7 +394,7 @@ static PyObject *Values_dispatch(Values *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = {"type", "values", "plugin_instance", "type_instance", "plugin", "host", "time", "interval", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sOssssdi", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sOzsssdi", kwlist, &type, &values, &plugin_instance, &type_instance, &plugin, &host, &time, &interval)) return NULL;