projects
/
collectd.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge branch 'collectd-5.5' into collectd-5.6
[collectd.git]
/
src
/
python.c
diff --git
a/src/python.c
b/src/python.c
index
05a44aa
..
1a7359c
100644
(file)
--- a/
src/python.c
+++ b/
src/python.c
@@
-197,7
+197,7
@@
static char reg_flush_doc[] = "register_flush(callback[, data][, name]) -> ident
"The callback function will be called with two or three parameters:\n"
"timeout: Indicates that only data older than 'timeout' seconds is to\n"
" be flushed.\n"
"The callback function will be called with two or three parameters:\n"
"timeout: Indicates that only data older than 'timeout' seconds is to\n"
" be flushed.\n"
- "id: Specifies which values are to be flushed.\n"
+ "id: Specifies which values are to be flushed.
Might be None.
\n"
"data: The optional data parameter passed to the register function.\n"
" If the parameter was omitted it will be omitted here, too.";
"data: The optional data parameter passed to the register function.\n"
" If the parameter was omitted it will be omitted here, too.";
@@
-236,12
+236,23
@@
static cpy_callback_t *cpy_config_callbacks;
static cpy_callback_t *cpy_init_callbacks;
static cpy_callback_t *cpy_shutdown_callbacks;
static cpy_callback_t *cpy_init_callbacks;
static cpy_callback_t *cpy_shutdown_callbacks;
+/* Make sure to hold the GIL while modifying these. */
+static int cpy_shutdown_triggered = 0;
+static int cpy_num_callbacks = 0;
+
static void cpy_destroy_user_data(void *data) {
cpy_callback_t *c = data;
free(c->name);
static void cpy_destroy_user_data(void *data) {
cpy_callback_t *c = data;
free(c->name);
+ CPY_LOCK_THREADS
Py_DECREF(c->callback);
Py_XDECREF(c->data);
free(c);
Py_DECREF(c->callback);
Py_XDECREF(c->data);
free(c);
+ --cpy_num_callbacks;
+ if (!cpy_num_callbacks && cpy_shutdown_triggered) {
+ Py_Finalize();
+ return;
+ }
+ CPY_RELEASE_THREADS
}
/* You must hold the GIL to call this function!
}
/* You must hold the GIL to call this function!
@@
-518,7
+529,12
@@
static void cpy_flush_callback(int timeout, const char *id, user_data_t *data) {
PyObject *ret, *text;
CPY_LOCK_THREADS
PyObject *ret, *text;
CPY_LOCK_THREADS
- text = cpy_string_to_unicode_or_bytes(id);
+ if (id) {
+ text = cpy_string_to_unicode_or_bytes(id);
+ } else {
+ text = Py_None;
+ Py_INCREF(text);
+ }
if (c->data == NULL)
ret = PyObject_CallFunction(c->callback, "iN", timeout, text); /* New reference. */
else
if (c->data == NULL)
ret = PyObject_CallFunction(c->callback, "iN", timeout, text); /* New reference. */
else
@@
-558,6
+574,7
@@
static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args
c->callback = callback;
c->data = data;
c->next = *list_head;
c->callback = callback;
c->data = data;
c->next = *list_head;
+ ++cpy_num_callbacks;
*list_head = c;
Py_XDECREF(mod);
PyMem_Free(name);
*list_head = c;
Py_XDECREF(mod);
PyMem_Free(name);
@@
-654,6
+671,7
@@
static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObjec
};
register_function(buf, handler, &user_data);
};
register_function(buf, handler, &user_data);
+ ++cpy_num_callbacks;
return cpy_string_to_unicode_or_bytes(buf);
}
return cpy_string_to_unicode_or_bytes(buf);
}
@@
-693,6
+711,7
@@
static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwd
plugin_register_complex_read(/* group = */ "python", buf,
cpy_read_callback, DOUBLE_TO_CDTIME_T (interval), &user_data);
plugin_register_complex_read(/* group = */ "python", buf,
cpy_read_callback, DOUBLE_TO_CDTIME_T (interval), &user_data);
+ ++cpy_num_callbacks;
return cpy_string_to_unicode_or_bytes(buf);
}
return cpy_string_to_unicode_or_bytes(buf);
}
@@
-798,8
+817,8
@@
static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *ar
PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
return NULL;
}
PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
return NULL;
}
- /* Yes, this is actually sa
v
e. To call this function the caller has to
- * hold the GIL. Well, sa
v
e as long as there is only one GIL anyway ... */
+ /* Yes, this is actually sa
f
e. To call this function the caller has to
+ * hold the GIL. Well, sa
f
e as long as there is only one GIL anyway ... */
if (prev == NULL)
*list_head = tmp->next;
else
if (prev == NULL)
*list_head = tmp->next;
else
@@
-808,6
+827,15
@@
static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *ar
Py_RETURN_NONE;
}
Py_RETURN_NONE;
}
+static void cpy_unregister_list(cpy_callback_t **list_head) {
+ cpy_callback_t *cur, *next;
+ for (cur = *list_head; cur; cur = next) {
+ next = cur->next;
+ cpy_destroy_user_data(cur);
+ }
+ *list_head = NULL;
+}
+
typedef int cpy_unregister_function_t(const char *name);
static PyObject *cpy_unregister_generic_userdata(cpy_unregister_function_t *unreg, PyObject *arg, const char *desc) {
typedef int cpy_unregister_function_t(const char *name);
static PyObject *cpy_unregister_generic_userdata(cpy_unregister_function_t *unreg, PyObject *arg, const char *desc) {
@@
-897,9
+925,7
@@
static PyMethodDef cpy_methods[] = {
static int cpy_shutdown(void) {
PyObject *ret;
static int cpy_shutdown(void) {
PyObject *ret;
- /* This can happen if the module was loaded but not configured. */
- if (state != NULL)
- PyEval_RestoreThread(state);
+ PyEval_RestoreThread(state);
for (cpy_callback_t *c = cpy_shutdown_callbacks; c; c = c->next) {
ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
for (cpy_callback_t *c = cpy_shutdown_callbacks; c; c = c->next) {
ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
@@
-909,7
+935,15
@@
static int cpy_shutdown(void) {
Py_DECREF(ret);
}
PyErr_Print();
Py_DECREF(ret);
}
PyErr_Print();
- Py_Finalize();
+
+ cpy_unregister_list(&cpy_config_callbacks);
+ cpy_unregister_list(&cpy_init_callbacks);
+ cpy_unregister_list(&cpy_shutdown_callbacks);
+ cpy_shutdown_triggered = 1;
+
+ if (!cpy_num_callbacks)
+ Py_Finalize();
+
return 0;
}
return 0;
}
@@
-942,7
+976,7
@@
static void *cpy_interactive(void *data) {
if (PyImport_ImportModule("readline") == NULL) {
/* This interactive session will suck. */
cpy_log_exception("interactive session init");
if (PyImport_ImportModule("readline") == NULL) {
/* This interactive session will suck. */
cpy_log_exception("interactive session init");
- }
+ }
cur_sig = PyOS_setsig(SIGINT, python_sigint_handler);
/* We totally forked just now. Everyone saw that, right? */
PyOS_AfterFork();
cur_sig = PyOS_setsig(SIGINT, python_sigint_handler);
/* We totally forked just now. Everyone saw that, right? */
PyOS_AfterFork();
@@
-962,6
+996,7
@@
static int cpy_init(void) {
if (!Py_IsInitialized()) {
WARNING("python: Plugin loaded but not configured.");
plugin_unregister_shutdown("python");
if (!Py_IsInitialized()) {
WARNING("python: Plugin loaded but not configured.");
plugin_unregister_shutdown("python");
+ Py_Finalize();
return 0;
}
PyEval_InitThreads();
return 0;
}
PyEval_InitThreads();