summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 8fdf425)
raw | patch | inline | side by side (parent: 8fdf425)
author | Sven Trenkel <collectd@semidefinite.de> | |
Mon, 14 Dec 2009 18:40:26 +0000 (19:40 +0100) | ||
committer | Florian Forster <octo@noris.net> | |
Tue, 5 Jan 2010 10:02:09 +0000 (11:02 +0100) |
src/cpython.h | patch | blob | history | |
src/pyconfig.c | patch | blob | history | |
src/python.c | patch | blob | history | |
src/pyvalues.c | patch | blob | history |
diff --git a/src/cpython.h b/src/cpython.h
index 661bf6a20c0e8fce3e8c47a9ee50a2b216cbe82e..880529705fab535d11b892a3f1a4967355af9ee9 100644 (file)
--- a/src/cpython.h
+++ b/src/cpython.h
# 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 bac39ae90b7d1eccb537e5765d5b41648ffad9ff..6f03da9b35fe05d44397378d35d612b4a51c7041 100644 (file)
--- a/src/pyconfig.c
+++ b/src/pyconfig.c
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();
return 0;
}
-static PyObject *Config_repr(PyObject *s) {
+/*static PyObject *Config_repr(PyObject *s) {
Config *self = (Config *) s;
return PyString_FromFormat("<collectd.Config %snode %s>", 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;
};
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 */
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 a33bb4778dcd607ff287e6316e45171284f2da42..93292f0919fcb97fa317b2586304506f99d1e1b1 100644 (file)
--- 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);
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)
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;
* 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);
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. */
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);
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 d83f541bb4a32420a11d516608494c795172acb6..1304d95cd05555a1a8fedee254b1f3f941f04e96 100644 (file)
--- a/src/pyvalues.c
+++ b/src/pyvalues.c
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,
*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},
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;
}
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;
}
};
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 */
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 */
Py_RETURN_NONE;
}
-static PyObject *Values_repr(PyObject *s) {
+/*static PyObject *Values_repr(PyObject *s) {
PyObject *ret, *valuestring = NULL;
Values *self = (Values *) 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;
};
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 */
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;
*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},
};
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 */
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 */