From bed062ac7102c63e194293bb8234a615a3e250a5 Mon Sep 17 00:00:00 2001 From: Sven Trenkel Date: Sat, 2 Jan 2010 20:51:22 +0100 Subject: [PATCH] python: Restored python2 compatibility. --- src/cpython.h | 24 ++++++++++++++++-- src/pyconfig.c | 6 ++--- src/python.c | 8 +++--- src/pyvalues.c | 66 ++++++++++++++++++-------------------------------- 4 files changed, 53 insertions(+), 51 deletions(-) diff --git a/src/cpython.h b/src/cpython.h index 8c7ad653..3e80cb0c 100644 --- a/src/cpython.h +++ b/src/cpython.h @@ -101,12 +101,26 @@ #define PyInt_FromLong PyLong_FromLong #define CPY_INIT_TYPE PyVarObject_HEAD_INIT(NULL, 0) #define IS_BYTES_OR_UNICODE(o) (PyUnicode_Check(o) || PyBytes_Check(o)) -#define CPY_STRCAT PyUnicode_Concat +#define CPY_STRCAT_AND_DEL(a, b) do {\ + CPY_STRCAT((a), (b));\ + Py_XDECREF((b));\ +} while (0) +static inline void CPY_STRCAT(PyObject **a, PyObject *b) { + PyObject *ret; + + if (!a || !*a) + return; + + ret = PyUnicode_Concat(*a, b); + Py_DECREF(*a); + *a = ret; +} #else #define CPY_INIT_TYPE PyObject_HEAD_INIT(NULL) 0, #define IS_BYTES_OR_UNICODE(o) (PyUnicode_Check(o) || PyString_Check(o)) +#define CPY_STRCAT_AND_DEL PyString_ConcatAndDel #define CPY_STRCAT PyString_Concat #endif @@ -120,7 +134,11 @@ static inline const char *cpy_unicode_or_bytes_to_string(PyObject **o) { Py_DECREF(*o); *o = tmp; } +#ifdef IS_PY3K return PyBytes_AsString(*o); +#else + return PyString_AsString(*o); +#endif } static inline PyObject *cpy_string_to_unicode_or_bytes(const char *buf) { @@ -131,8 +149,10 @@ static inline PyObject *cpy_string_to_unicode_or_bytes(const char *buf) { if (ret != NULL) return ret; PyErr_Clear(); -#endif return PyBytes_FromString(buf); +#else + return PyString_FromString(buf); +#endif } /* Python object declarations. */ diff --git a/src/pyconfig.c b/src/pyconfig.c index 258d31fb..b5c01aaf 100644 --- a/src/pyconfig.c +++ b/src/pyconfig.c @@ -136,10 +136,10 @@ static PyObject *Config_repr(PyObject *s) { ret = PyObject_Str(self->key); CPY_SUBSTITUTE(PyObject_Repr, ret, ret); if (self->parent == NULL || self->parent == Py_None) - CPY_SUBSTITUTE(CPY_STRCAT, ret, root_prefix, ret); + CPY_STRCAT(&ret, root_prefix); else - CPY_SUBSTITUTE(CPY_STRCAT, ret, node_prefix, ret); - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, ending); + CPY_STRCAT(&ret, node_prefix); + CPY_STRCAT(&ret, ending); return ret; } diff --git a/src/python.c b/src/python.c index 25681b66..5664b0c6 100644 --- a/src/python.c +++ b/src/python.c @@ -691,18 +691,18 @@ static PyObject *cpy_unregister_generic_userdata(cpy_unregister_function_t *unre PyErr_Clear(); if (!PyCallable_Check(arg)) { PyErr_SetString(PyExc_TypeError, "This function needs a string or a callable object as its only parameter."); - Py_DECREF(&arg); + Py_DECREF(arg); return NULL; } cpy_build_name(buf, sizeof(buf), arg, NULL); name = buf; } if (unreg(name) == 0) { - Py_DECREF(&arg); + Py_DECREF(arg); Py_RETURN_NONE; } PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name); - Py_DECREF(&arg); + Py_DECREF(arg); return NULL; } @@ -895,6 +895,7 @@ static PyObject *cpy_oconfig_to_pyconfig(oconfig_item_t *ci, PyObject *parent) { return item; } +#ifdef IS_PY3K static struct PyModuleDef collectdmodule = { PyModuleDef_HEAD_INIT, "collectd", /* name of module */ @@ -906,6 +907,7 @@ static struct PyModuleDef collectdmodule = { PyMODINIT_FUNC PyInit_collectd(void) { return PyModule_Create(&collectdmodule); } +#endif static int cpy_config(oconfig_item_t *ci) { int i; diff --git a/src/pyvalues.c b/src/pyvalues.c index 3fb68cf2..a632dc16 100644 --- a/src/pyvalues.c +++ b/src/pyvalues.c @@ -56,56 +56,44 @@ static PyObject *cpy_common_repr(PyObject *s) { ret = cpy_string_to_unicode_or_bytes(s->ob_type->tp_name); - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_type); + CPY_STRCAT(&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); + CPY_STRCAT_AND_DEL(&ret, tmp); if (self->type_instance[0] != 0) { - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_type_instance); + CPY_STRCAT(&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); + CPY_STRCAT_AND_DEL(&ret, tmp); } if (self->plugin[0] != 0) { - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_plugin); + CPY_STRCAT(&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); + CPY_STRCAT_AND_DEL(&ret, tmp); } if (self->plugin_instance[0] != 0) { - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_plugin_instance); + CPY_STRCAT(&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); + CPY_STRCAT_AND_DEL(&ret, tmp); } if (self->host[0] != 0) { - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_host); + CPY_STRCAT(&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); + CPY_STRCAT_AND_DEL(&ret, tmp); } if (self->time != 0) { - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_time); + CPY_STRCAT(&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_STRCAT_AND_DEL(&ret, tmp); } return ret; } @@ -189,7 +177,7 @@ static PyObject *PluginData_repr(PyObject *s) { return NULL; ret = cpy_common_repr(s); - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_closing); + CPY_STRCAT(&ret, l_closing); return ret; } @@ -609,21 +597,17 @@ static PyObject *Values_repr(PyObject *s) { ret = cpy_common_repr(s); if (self->interval != 0) { - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_interval); + CPY_STRCAT(&ret, l_interval); tmp = PyInt_FromLong(self->interval); CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp); - if (tmp) - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, tmp); - Py_XDECREF(tmp); + CPY_STRCAT_AND_DEL(&ret, tmp); } if (self->values != NULL && PySequence_Length(self->values) > 0) { - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_values); + CPY_STRCAT(&ret, l_values); tmp = PyObject_Repr(self->values); - if (tmp) - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, tmp); - Py_XDECREF(tmp); + CPY_STRCAT_AND_DEL(&ret, tmp); } - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_closing); + CPY_STRCAT(&ret, l_closing); return ret; } @@ -843,22 +827,18 @@ static PyObject *Notification_repr(PyObject *s) { ret = cpy_common_repr(s); if (self->severity != 0) { - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_severity); + CPY_STRCAT(&ret, l_severity); tmp = PyInt_FromLong(self->severity); CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp); - if (tmp) - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, tmp); - Py_XDECREF(tmp); + CPY_STRCAT_AND_DEL(&ret, tmp); } if (self->message[0] != 0) { - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_message); + CPY_STRCAT(&ret, l_message); tmp = cpy_string_to_unicode_or_bytes(self->message); CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp); - if (tmp) - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, tmp); - Py_XDECREF(tmp); + CPY_STRCAT_AND_DEL(&ret, tmp); } - CPY_SUBSTITUTE(CPY_STRCAT, ret, ret, l_closing); + CPY_STRCAT(&ret, l_closing); return ret; } -- 2.30.2