From: Sven Trenkel Date: Mon, 14 Dec 2009 18:40:26 +0000 (+0100) Subject: Somewhat workable python3 support. This breaks python2 support and the __repr__ funct... X-Git-Tag: collectd-4.10.0~76^2~10 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=1f17cc084351bd1170385927d22fc9bd10ecd8d7;p=collectd.git Somewhat workable python3 support. This breaks python2 support and the __repr__ functions. --- diff --git a/src/cpython.h b/src/cpython.h index 661bf6a2..88052970 100644 --- a/src/cpython.h +++ b/src/cpython.h @@ -74,6 +74,50 @@ # define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None #endif + +/* Python3 compatibility layer. To keep the actual code as clean as possible + * do a lot of defines here. */ + +#if PY_MAJOR_VERSION >= 3 +#define IS_PY3K +#endif + +#ifdef IS_PY3K +#define PyInt_FromLong PyLong_FromLong +//#define PyString_FromString PyBytes_FromString +#define CPY_INIT_TYPE PyVarObject_HEAD_INIT(NULL, 0) +#define IS_BYTES_OR_UNICODE(o) (PyUnicode_Check(o) || PyBytes_Check(o)) +#else +#define CPY_INIT_TYPE PyObject_HEAD_INIT(NULL) 0, +#define IS_BYTES_OR_UNICODE(o) (PyUnicode_Check(o) || PyString_Check(o)) +#endif + +static inline const char *cpy_unicode_or_bytes_to_string(PyObject **o) { + if (PyUnicode_Check(*o)) { + PyObject *tmp; + tmp = PyUnicode_AsEncodedString(*o, NULL, NULL); /* New reference. */ + if (tmp == NULL) + return NULL; + Py_DECREF(*o); + *o = tmp; + } + return PyBytes_AsString(*o); +} + +static inline PyObject *cpy_string_to_unicode_or_bytes(const char *buf) { +#ifdef IS_PY3K +/* Python3 preferrs unicode */ + PyObject *ret; + ret = PyUnicode_Decode(buf, strlen(buf), NULL, NULL); + if (ret != NULL) + return ret; + PyErr_Clear(); +#endif + return PyBytes_FromString(buf); +} + + /* Python object declarations. */ + typedef struct { PyObject_HEAD /* No semicolon! */ PyObject *parent; /* Config */ diff --git a/src/pyconfig.c b/src/pyconfig.c index bac39ae9..6f03da9b 100644 --- a/src/pyconfig.c +++ b/src/pyconfig.c @@ -74,10 +74,17 @@ static int Config_init(PyObject *s, PyObject *args, PyObject *kwds) { Config *self = (Config *) s; static char *kwlist[] = {"key", "parent", "values", "children", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|OOO", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist, &key, &parent, &values, &children)) return -1; + if (!IS_BYTES_OR_UNICODE(key)) { + PyErr_SetString(PyExc_TypeError, "argument 1 must be str"); + Py_XDECREF(parent); + Py_XDECREF(values); + Py_XDECREF(children); + return -1; + } if (values == NULL) { values = PyTuple_New(0); PyErr_Clear(); @@ -111,11 +118,11 @@ static int Config_init(PyObject *s, PyObject *args, PyObject *kwds) { return 0; } -static PyObject *Config_repr(PyObject *s) { +/*static PyObject *Config_repr(PyObject *s) { Config *self = (Config *) s; return PyString_FromFormat("", self->parent == Py_None ? "root " : "", PyString_AsString(PyObject_Str(self->key))); -} +}*/ static int Config_traverse(PyObject *self, visitproc visit, void *arg) { Config *c = (Config *) self; @@ -149,8 +156,7 @@ static PyMemberDef Config_members[] = { }; PyTypeObject ConfigType = { - PyObject_HEAD_INIT(NULL) - 0, /* Always 0 */ + CPY_INIT_TYPE "collectd.Config", /* tp_name */ sizeof(Config), /* tp_basicsize */ 0, /* Will be filled in later */ @@ -159,7 +165,7 @@ PyTypeObject ConfigType = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ - Config_repr, /* tp_repr */ + 0/*Config_repr*/, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ diff --git a/src/python.c b/src/python.c index a33bb477..93292f09 100644 --- a/src/python.c +++ b/src/python.c @@ -245,7 +245,7 @@ static void cpy_build_name(char *buf, size_t size, PyObject *callback, const cha mod = PyObject_GetAttrString(callback, "__module__"); /* New reference. */ if (mod != NULL) - module = PyString_AsString(mod); + module = cpy_unicode_or_bytes_to_string(&mod); if (module != NULL) { snprintf(buf, size, "python.%s", module); @@ -268,11 +268,11 @@ static void cpy_log_exception(const char *context) { PyErr_NormalizeException(&type, &value, &traceback); if (type == NULL) return; tn = PyObject_GetAttrString(type, "__name__"); /* New reference. */ - m = PyObject_GetAttrString(value, "message"); /* New reference. */ + m = PyObject_Str(value); /* New reference. */ if (tn != NULL) - typename = PyString_AsString(tn); + typename = cpy_unicode_or_bytes_to_string(&tn); if (m != NULL) - message = PyString_AsString(m); + message = cpy_unicode_or_bytes_to_string(&m); if (typename == NULL) typename = "NamelessException"; if (message == NULL) @@ -301,7 +301,8 @@ static void cpy_log_exception(const char *context) { PyObject *line; line = PyList_GET_ITEM(list, i); /* Borrowed reference. */ - s = strdup(PyString_AsString(line)); + Py_INCREF(line); + s = strdup(cpy_unicode_or_bytes_to_string(&line)); Py_DECREF(line); if (s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = 0; @@ -468,7 +469,7 @@ static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args c->next = *list_head; *list_head = c; Py_XDECREF(mod); - return PyString_FromString(buf); + return cpy_string_to_unicode_or_bytes(buf); } static PyObject *cpy_flush(cpy_callback_t **list_head, PyObject *args, PyObject *kwds) { @@ -520,7 +521,7 @@ static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObjec user_data->free_func = cpy_destroy_user_data; user_data->data = c; register_function(buf, handler, user_data); - return PyString_FromString(buf); + return cpy_string_to_unicode_or_bytes(buf); } static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwds) { @@ -553,7 +554,7 @@ static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwd ts.tv_sec = interval; ts.tv_nsec = (interval - ts.tv_sec) * 1000000000; plugin_register_complex_read(buf, cpy_read_callback, &ts, user_data); - return PyString_FromString(buf); + return cpy_string_to_unicode_or_bytes(buf); } static PyObject *cpy_register_log(PyObject *self, PyObject *args, PyObject *kwds) { @@ -632,17 +633,13 @@ static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *ar const char *name; cpy_callback_t *prev = NULL, *tmp; - if (PyUnicode_Check(arg)) { - arg = PyUnicode_AsEncodedString(arg, NULL, NULL); - if (arg == NULL) - return NULL; - name = PyString_AsString(arg); - Py_DECREF(arg); - } else if (PyString_Check(arg)) { - name = PyString_AsString(arg); - } else { + Py_INCREF(arg); + name = cpy_unicode_or_bytes_to_string(&arg); + if (name == NULL) { + 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); return NULL; } cpy_build_name(buf, sizeof(buf), arg, NULL); @@ -652,6 +649,7 @@ static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *ar if (strcmp(name, tmp->name) == 0) break; + Py_DECREF(arg); if (tmp == NULL) { PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name); return NULL; @@ -672,25 +670,24 @@ static PyObject *cpy_unregister_generic_userdata(cpy_unregister_function_t *unre char buf[512]; const char *name; - if (PyUnicode_Check(arg)) { - arg = PyUnicode_AsEncodedString(arg, NULL, NULL); - if (arg == NULL) - return NULL; - name = PyString_AsString(arg); - Py_DECREF(arg); - } else if (PyString_Check(arg)) { - name = PyString_AsString(arg); - } else { + Py_INCREF(arg); + name = cpy_unicode_or_bytes_to_string(&arg); + if (name == NULL) { + 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); return NULL; } cpy_build_name(buf, sizeof(buf), arg, NULL); name = buf; } - if (unreg(name) == 0) + if (unreg(name) == 0) { + Py_DECREF(&arg); Py_RETURN_NONE; + } PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name); + Py_DECREF(&arg); return NULL; } @@ -861,7 +858,7 @@ static PyObject *cpy_oconfig_to_pyconfig(oconfig_item_t *ci, PyObject *parent) { values = PyTuple_New(ci->values_num); /* New reference. */ for (i = 0; i < ci->values_num; ++i) { if (ci->values[i].type == OCONFIG_TYPE_STRING) { - PyTuple_SET_ITEM(values, i, PyString_FromString(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)); } else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN) { @@ -882,6 +879,18 @@ static PyObject *cpy_oconfig_to_pyconfig(oconfig_item_t *ci, PyObject *parent) { return item; } +static struct PyModuleDef collectdmodule = { + PyModuleDef_HEAD_INIT, + "collectd", /* name of module */ + "Where does this go?", /* module documentation, may be NULL */ + -1, + cpy_methods +}; + +PyMODINIT_FUNC PyInit_collectd(void) { + return PyModule_Create(&collectdmodule); +} + static int cpy_config(oconfig_item_t *ci) { int i; PyObject *sys, *tb; @@ -894,6 +903,12 @@ static int cpy_config(oconfig_item_t *ci) { * python code during the config callback so we have to start * the interpreter here. */ /* Do *not* use the python "thread" module at this point! */ + +#ifdef IS_PY3K + /* Add a builtin module, before Py_Initialize */ + PyImport_AppendInittab("collectd", PyInit_collectd); +#endif + Py_Initialize(); PyType_Ready(&ConfigType); @@ -913,7 +928,11 @@ static int cpy_config(oconfig_item_t *ci) { cpy_log_exception("python initialization"); return 1; } +#ifdef IS_PY3K + module = PyImport_ImportModule("collectd"); +#else module = Py_InitModule("collectd", cpy_methods); /* Borrowed reference. */ +#endif PyModule_AddObject(module, "Config", (void *) &ConfigType); /* Steals a reference. */ PyModule_AddObject(module, "Values", (void *) &ValuesType); /* Steals a reference. */ PyModule_AddObject(module, "Notification", (void *) &NotificationType); /* Steals a reference. */ @@ -963,7 +982,7 @@ static int cpy_config(oconfig_item_t *ci) { if (cf_util_get_string(item, &dir) != 0) continue; - dir_object = PyString_FromString(dir); /* New reference. */ + dir_object = cpy_string_to_unicode_or_bytes(dir); /* New reference. */ if (dir_object == NULL) { ERROR("python plugin: Unable to convert \"%s\" to " "a python object.", dir); @@ -988,7 +1007,6 @@ static int cpy_config(oconfig_item_t *ci) { if (module == NULL) { ERROR("python plugin: Error importing module \"%s\".", module_name); cpy_log_exception("importing module"); - PyErr_Print(); } free(module_name); Py_XDECREF(module); diff --git a/src/pyvalues.c b/src/pyvalues.c index d83f541b..1304d95c 100644 --- a/src/pyvalues.c +++ b/src/pyvalues.c @@ -100,7 +100,7 @@ static int PluginData_init(PyObject *s, PyObject *args, PyObject *kwds) { return 0; } -static PyObject *PluginData_repr(PyObject *s) { +/*static PyObject *PluginData_repr(PyObject *s) { PluginData *self = (PluginData *) s; return PyString_FromFormat("collectd.Values(type='%s%s%s%s%s%s%s%s%s',time=%lu)", self->type, @@ -109,7 +109,7 @@ static PyObject *PluginData_repr(PyObject *s) { *self->plugin_instance ? "',plugin_instance='" : "", self->plugin_instance, *self->host ? "',host='" : "", self->host, (long unsigned) self->time); -} +}*/ static PyMemberDef PluginData_members[] = { {"time", T_DOUBLE, offsetof(PluginData, time), 0, time_doc}, @@ -119,7 +119,7 @@ static PyMemberDef PluginData_members[] = { static PyObject *PluginData_getstring(PyObject *self, void *data) { const char *value = ((char *) self) + (intptr_t) data; - return PyString_FromString(value); + return cpy_string_to_unicode_or_bytes(value); } static int PluginData_setstring(PyObject *self, PyObject *value, void *data) { @@ -130,10 +130,15 @@ static int PluginData_setstring(PyObject *self, PyObject *value, void *data) { PyErr_SetString(PyExc_TypeError, "Cannot delete this attribute"); return -1; } - new = PyString_AsString(value); - if (new == NULL) return -1; + Py_INCREF(value); + new = cpy_unicode_or_bytes_to_string(&value); + if (new == NULL) { + Py_DECREF(value); + return -1; + } old = ((char *) self) + (intptr_t) data; sstrncpy(old, new, DATA_MAX_NAME_LEN); + Py_DECREF(value); return 0; } @@ -145,16 +150,22 @@ static int PluginData_settype(PyObject *self, PyObject *value, void *data) { PyErr_SetString(PyExc_TypeError, "Cannot delete this attribute"); return -1; } - new = PyString_AsString(value); - if (new == NULL) return -1; + Py_INCREF(value); + new = cpy_unicode_or_bytes_to_string(&value); + if (new == NULL) { + Py_DECREF(value); + return -1; + } if (plugin_get_ds(new) == NULL) { PyErr_Format(PyExc_TypeError, "Dataset %s not found", new); + Py_DECREF(value); return -1; } old = ((char *) self) + (intptr_t) data; sstrncpy(old, new, DATA_MAX_NAME_LEN); + Py_DECREF(value); return 0; } @@ -168,8 +179,7 @@ static PyGetSetDef PluginData_getseters[] = { }; PyTypeObject PluginDataType = { - PyObject_HEAD_INIT(NULL) - 0, /* Always 0 */ + CPY_INIT_TYPE "collectd.PluginData", /* tp_name */ sizeof(PluginData), /* tp_basicsize */ 0, /* Will be filled in later */ @@ -178,7 +188,7 @@ PyTypeObject PluginDataType = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ - PluginData_repr, /* tp_repr */ + 0/*PluginData_repr*/, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ @@ -496,7 +506,7 @@ static PyObject *Values_write(Values *self, PyObject *args, PyObject *kwds) { Py_RETURN_NONE; } -static PyObject *Values_repr(PyObject *s) { +/*static PyObject *Values_repr(PyObject *s) { PyObject *ret, *valuestring = NULL; Values *self = (Values *) s; @@ -511,10 +521,10 @@ static PyObject *Values_repr(PyObject *s) { *self->data.plugin_instance ? "',plugin_instance='" : "", self->data.plugin_instance, *self->data.host ? "',host='" : "", self->data.host, (long unsigned) self->data.time, self->interval, - valuestring ? PyString_AsString(valuestring) : "[]"); + valuestring ? cpy_unicode_or_bytes_to_string(valuestring) : "[]"); Py_XDECREF(valuestring); return ret; -} +}*/ static int Values_traverse(PyObject *self, visitproc visit, void *arg) { Values *v = (Values *) self; @@ -546,8 +556,7 @@ static PyMethodDef Values_methods[] = { }; PyTypeObject ValuesType = { - PyObject_HEAD_INIT(NULL) - 0, /* Always 0 */ + CPY_INIT_TYPE "collectd.Values", /* tp_name */ sizeof(Values), /* tp_basicsize */ 0, /* Will be filled in later */ @@ -556,7 +565,7 @@ PyTypeObject ValuesType = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ - Values_repr, /* tp_repr */ + 0/*Values_repr*/, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ @@ -701,14 +710,19 @@ static int Notification_setstring(PyObject *self, PyObject *value, void *data) { PyErr_SetString(PyExc_TypeError, "Cannot delete this attribute"); return -1; } - new = PyString_AsString(value); - if (new == NULL) return -1; + Py_INCREF(value); + new = cpy_unicode_or_bytes_to_string(&value); + if (new == NULL) { + Py_DECREF(value); + return -1; + } old = ((char *) self) + (intptr_t) data; sstrncpy(old, new, NOTIF_MAX_MSG_LEN); + Py_DECREF(value); return 0; } -static PyObject *Notification_repr(PyObject *s) { +/*static PyObject *Notification_repr(PyObject *s) { PyObject *ret; Notification *self = (Notification *) s; @@ -720,7 +734,7 @@ static PyObject *Notification_repr(PyObject *s) { *self->message ? "',message='" : "", self->message, (long unsigned) self->data.time, self->severity); return ret; -} +}*/ static PyMethodDef Notification_methods[] = { {"dispatch", (PyCFunction) Notification_dispatch, METH_VARARGS | METH_KEYWORDS, dispatch_doc}, @@ -738,8 +752,7 @@ static PyGetSetDef Notification_getseters[] = { }; PyTypeObject NotificationType = { - PyObject_HEAD_INIT(NULL) - 0, /* Always 0 */ + CPY_INIT_TYPE "collectd.Notification", /* tp_name */ sizeof(Notification), /* tp_basicsize */ 0, /* Will be filled in later */ @@ -748,7 +761,7 @@ PyTypeObject NotificationType = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ - Notification_repr, /* tp_repr */ + 0/*Notification_repr*/, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */