Code

python: Restored python2 compatibility.
authorSven Trenkel <collectd@semidefinite.de>
Sat, 2 Jan 2010 19:51:22 +0000 (20:51 +0100)
committerFlorian Forster <octo@noris.net>
Tue, 5 Jan 2010 10:03:15 +0000 (11:03 +0100)
src/cpython.h
src/pyconfig.c
src/python.c
src/pyvalues.c

index 8c7ad653f71cd5f8ec878274bd69dd4e2f5abae4..3e80cb0c4ee9223dc7a0959db571b6a503a973b3 100644 (file)
 #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. */
index 258d31fb75bff8b0209c162c15ebf83deb34c569..b5c01aaf1d5481256bf872e6d078c6e4b74dc15c 100644 (file)
@@ -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;
 }
index 25681b6649e049f723107dc67ee298ca7761ecde..5664b0c6390095072abcba72faf249e090a1b36d 100644 (file)
@@ -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;
index 3fb68cf21af49d12456b8299257c84c9fd2d7a37..a632dc16061d0005130cdff6f76a83bb5fe1beb8 100644 (file)
@@ -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;
 }