Merge branch 'collectd-4.9' into collectd-4.10
[collectd.git] / src / python.c
index 00516fb..6be727e 100644 (file)
@@ -371,6 +371,7 @@ static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_li
                        }
                        if (PyErr_Occurred() != NULL) {
                                cpy_log_exception("value building for write callback");
+                               Py_DECREF(list);
                                CPY_RETURN_FROM_THREADS 0;
                        }
                }
@@ -427,7 +428,7 @@ static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_li
                        }
                        free(table);
                }
-               v = PyObject_New(Values, (void *) &ValuesType);
+               v = PyObject_New(Values, (void *) &ValuesType); /* New reference. */
                sstrncpy(v->data.host, value_list->host, sizeof(v->data.host));
                sstrncpy(v->data.type, value_list->type, sizeof(v->data.type));
                sstrncpy(v->data.type_instance, value_list->type_instance, sizeof(v->data.type_instance));
@@ -438,6 +439,7 @@ static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_li
                v->values = list;
                v->meta = dict;
                ret = PyObject_CallFunctionObjArgs(c->callback, v, c->data, (void *) 0); /* New reference. */
+               Py_XDECREF(v);
                if (ret == NULL) {
                        cpy_log_exception("write callback");
                } else {
@@ -453,7 +455,7 @@ static int cpy_notification_callback(const notification_t *notification, user_da
        Notification *n;
 
        CPY_LOCK_THREADS
-               n = PyObject_New(Notification, (void *) &NotificationType);
+               n = PyObject_New(Notification, (void *) &NotificationType); /* New reference. */
                sstrncpy(n->data.host, notification->host, sizeof(n->data.host));
                sstrncpy(n->data.type, notification->type, sizeof(n->data.type));
                sstrncpy(n->data.type_instance, notification->type_instance, sizeof(n->data.type_instance));
@@ -463,6 +465,7 @@ static int cpy_notification_callback(const notification_t *notification, user_da
                sstrncpy(n->message, notification->message, sizeof(n->message));
                n->severity = notification->severity;
                ret = PyObject_CallFunctionObjArgs(c->callback, n, c->data, (void *) 0); /* New reference. */
+               Py_XDECREF(n);
                if (ret == NULL) {
                        cpy_log_exception("notification callback");
                } else {
@@ -896,6 +899,11 @@ static int cpy_init(void) {
        static pthread_t thread;
        sigset_t sigset;
        
+       if (!Py_IsInitialized()) {
+               WARNING("python: Plugin loaded but not configured.");
+               plugin_unregister_shutdown("python");
+               return 0;
+       }
        PyEval_InitThreads();
        /* Now it's finally OK to use python threads. */
        for (c = cpy_init_callbacks; c; c = c->next) {