lib: make public reference count functions have strict types
[babeltrace.git] / python-plugin-provider / python-plugin-provider.c
index 2cb5a7334f82169b608d557180875e9f619dd3ec..1eb1fa34182d95c539b06797c7e8da39c7f1e053 100644 (file)
@@ -29,8 +29,7 @@
 
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/compiler-internal.h>
-#include <babeltrace/ref.h>
-#include <babeltrace/plugin/plugin.h>
+#include <babeltrace/plugin/plugin-const.h>
 #include <babeltrace/plugin/plugin-internal.h>
 #include <babeltrace/graph/component-class.h>
 #include <babeltrace/graph/component-class-internal.h>
@@ -57,6 +56,7 @@ enum python_state {
 } python_state = PYTHON_STATE_NOT_INITED;
 
 static PyObject *py_try_load_plugin_module_func = NULL;
+static bool python_was_initialized_by_us;
 
 static
 void print_python_traceback_warn(void)
@@ -80,7 +80,9 @@ void init_python(void)
 {
        PyObject *py_bt2_py_plugin_mod = NULL;
        const char *dis_python_env;
+#ifndef __MINGW32__
        sighandler_t old_sigint = signal(SIGINT, SIG_DFL);
+#endif
 
        if (python_state != PYTHON_STATE_NOT_INITED) {
                goto end;
@@ -101,6 +103,7 @@ void init_python(void)
        if (!Py_IsInitialized()) {
                BT_LOGI_STR("Python interpreter is not initialized: initializing Python interpreter.");
                Py_InitializeEx(0);
+               python_was_initialized_by_us = true;
                BT_LOGI("Initialized Python interpreter: version=\"%s\"",
                        Py_GetVersion());
        } else {
@@ -126,9 +129,11 @@ void init_python(void)
        python_state = PYTHON_STATE_FULLY_INITIALIZED;
 
 end:
+#ifndef __MINGW32__
        if (old_sigint != SIG_ERR) {
                (void) signal(SIGINT, old_sigint);
        }
+#endif
 
        print_python_traceback_warn();
        pyerr_clear();
@@ -138,7 +143,7 @@ end:
 
 __attribute__((destructor)) static
 void fini_python(void) {
-       if (Py_IsInitialized()) {
+       if (Py_IsInitialized() && python_was_initialized_by_us) {
                if (py_try_load_plugin_module_func) {
                        Py_DECREF(py_try_load_plugin_module_func);
                        py_try_load_plugin_module_func = NULL;
@@ -152,9 +157,9 @@ void fini_python(void) {
 }
 
 static
-struct bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
+const struct bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
 {
-       struct bt_plugin *plugin = NULL;
+       const struct bt_plugin *plugin = NULL;
        PyObject *py_name = NULL;
        PyObject *py_author = NULL;
        PyObject *py_description = NULL;
@@ -169,8 +174,8 @@ struct bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
        const char *version_extra = NULL;
        int ret;
 
-       assert(plugin_info);
-       assert(python_state == PYTHON_STATE_FULLY_INITIALIZED);
+       BT_ASSERT(plugin_info);
+       BT_ASSERT(python_state == PYTHON_STATE_FULLY_INITIALIZED);
        py_name = PyObject_GetAttrString(plugin_info, "name");
        if (!py_name) {
                BT_LOGW("Cannot find `name` attribute in Python plugin info object: "
@@ -261,9 +266,9 @@ struct bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
                        PyObject *py_minor = PyTuple_GetItem(py_version, 1);
                        PyObject *py_patch = PyTuple_GetItem(py_version, 2);
 
-                       assert(py_major);
-                       assert(py_minor);
-                       assert(py_patch);
+                       BT_ASSERT(py_major);
+                       BT_ASSERT(py_minor);
+                       BT_ASSERT(py_patch);
 
                        if (PyLong_Check(py_major)) {
                                major = PyLong_AsUnsignedLong(py_major);
@@ -288,7 +293,7 @@ struct bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
                if (PyTuple_Size(py_version) >= 4) {
                        PyObject *py_extra = PyTuple_GetItem(py_version, 3);
 
-                       assert(py_extra);
+                       BT_ASSERT(py_extra);
 
                        if (PyUnicode_Check(py_extra)) {
                                version_extra = PyUnicode_AsUTF8(py_extra);
@@ -332,7 +337,7 @@ struct bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
 
                        py_comp_class_addr =
                                PyList_GetItem(py_comp_class_addrs, i);
-                       assert(py_comp_class_addr);
+                       BT_ASSERT(py_comp_class_addr);
                        if (PyLong_Check(py_comp_class_addr)) {
                                comp_class = (struct bt_component_class *)
                                        PyLong_AsUnsignedLongLong(py_comp_class_addr);
@@ -369,7 +374,7 @@ struct bt_plugin *bt_plugin_from_python_plugin_info(PyObject *plugin_info)
 error:
        print_python_traceback_warn();
        pyerr_clear();
-       BT_PUT(plugin);
+       BT_OBJECT_PUT_REF_AND_RESET(plugin);
 
 end:
        Py_XDECREF(py_name);
@@ -385,12 +390,12 @@ G_MODULE_EXPORT
 struct bt_plugin_set *bt_plugin_python_create_all_from_file(const char *path)
 {
        struct bt_plugin_set *plugin_set = NULL;
-       struct bt_plugin *plugin = NULL;
+       const struct bt_plugin *plugin = NULL;
        PyObject *py_plugin_info = NULL;
        gchar *basename = NULL;
        size_t path_len;
 
-       assert(path);
+       BT_ASSERT(path);
 
        if (python_state == PYTHON_STATE_CANNOT_INITIALIZE) {
                /*
@@ -486,10 +491,10 @@ struct bt_plugin_set *bt_plugin_python_create_all_from_file(const char *path)
        goto end;
 
 error:
-       BT_PUT(plugin_set);
+       BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
 
 end:
-       bt_put(plugin);
+       bt_plugin_put_ref(plugin);
        Py_XDECREF(py_plugin_info);
        g_free(basename);
        return plugin_set;
This page took 0.024783 seconds and 4 git commands to generate.