[gnumeric] PythonLoader: cleanups and fixes.



commit c8c9bcafeb4a5b7090cbe0315e330655b649de88
Author: Morten Welinder <terra gnome org>
Date:   Sun Mar 21 16:49:58 2021 -0400

    PythonLoader: cleanups and fixes.
    
    Don't access a non-tuple (for py_printf) as a tuple.

 NEWS                                       |  1 +
 plugins/python-loader/ChangeLog            | 12 ++++++++++++
 plugins/python-loader/gnm-py-interpreter.c | 20 ++++++++++++--------
 plugins/python-loader/gnm-python.c         |  6 ++++--
 plugins/python-loader/python-loader.c      |  7 +++++--
 5 files changed, 34 insertions(+), 12 deletions(-)
---
diff --git a/NEWS b/NEWS
index 043fb07e0..a56330bd5 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@ Gnumeric 1.12.50
 
 Morten:
        * Fix undo problem with sorting and cell comments.
+       * Python loader cleanups.
 
 --------------------------------------------------------------------------
 Gnumeric 1.12.49
diff --git a/plugins/python-loader/ChangeLog b/plugins/python-loader/ChangeLog
index 386f2f0e5..3154a10d5 100644
--- a/plugins/python-loader/ChangeLog
+++ b/plugins/python-loader/ChangeLog
@@ -1,3 +1,15 @@
+2021-03-21  Morten Welinder  <terra gnome org>
+
+       * python-loader.c (gplp_unload_service_function_group): Clear
+       python_fn_info_dict after unref so we don't do it again.
+       (python_function_get_gnumeric_help): Check for tuple before using.
+
+       * gnm-py-interpreter.c (gnm_py_interpreter_new): Restore state
+       after Py_NewInterpreter.
+
+       * gnm-python.c (gnm_python_finalize): Copy list of interpreters as
+       it will change underneath us.
+
 2021-03-14  Morten Welinder <terra gnome org>
 
        * Release 1.12.49
diff --git a/plugins/python-loader/gnm-py-interpreter.c b/plugins/python-loader/gnm-py-interpreter.c
index 3c57fd5ac..204355964 100644
--- a/plugins/python-loader/gnm-py-interpreter.c
+++ b/plugins/python-loader/gnm-py-interpreter.c
@@ -88,18 +88,18 @@ gnm_py_interpreter_new (GOPlugin *plugin)
        g_return_val_if_fail (plugin == NULL || GO_IS_PLUGIN (plugin), NULL);
 
        if (plugin != NULL) {
+               PyThreadState* main_ = PyThreadState_Get ();
                py_thread_state = Py_NewInterpreter ();
+               PyThreadState_Swap (main_);
        } else {
                py_thread_state = PyThreadState_Get ();
        }
-       g_return_val_if_fail (py_thread_state != NULL, NULL);
 
        interpreter = g_object_new (GNM_PY_INTERPRETER_TYPE, NULL);
        interpreter->py_thread_state = py_thread_state;
        interpreter->plugin = plugin;
-
        PySys_SetArgv (G_N_ELEMENTS (plugin_argv) - 1, plugin_argv);
-       gnm_py_interpreter_switch_to (interpreter);
+
        if (plugin != NULL)
                py_gnumeric_add_plugin (py_initgnumeric (), interpreter);
 
@@ -112,8 +112,10 @@ gnm_py_interpreter_destroy (GnmPyInterpreter *interpreter,
 {
        g_return_if_fail (GNM_IS_PY_INTERPRETER (interpreter));
 
-       gnm_py_interpreter_switch_to (interpreter);
-       Py_EndInterpreter (interpreter->py_thread_state);
+       if (interpreter->plugin != NULL) {
+               gnm_py_interpreter_switch_to (interpreter);
+               Py_EndInterpreter (interpreter->py_thread_state);
+       }
        (void) PyThreadState_Swap (new_interpreter->py_thread_state);
        interpreter->py_thread_state = NULL;
        g_object_unref (interpreter);
@@ -124,10 +126,12 @@ gnm_py_interpreter_switch_to (GnmPyInterpreter *interpreter)
 {
        g_return_if_fail (GNM_IS_PY_INTERPRETER (interpreter));
 
-       if (PyThreadState_Get ()->interp != interpreter->py_thread_state->interp) {
+       if (PyThreadState_Get () != interpreter->py_thread_state) {
+               // g_printerr ("Switching to %p\n", interpreter->py_thread_state);
                (void) PyThreadState_Swap (interpreter->py_thread_state);
-               g_signal_emit (
-                       interpreter, signals[SET_CURRENT_SIGNAL], 0);
+               g_signal_emit (interpreter, signals[SET_CURRENT_SIGNAL], 0);
+       } else {
+               // g_printerr ("Not switching to %p\n", PyThreadState_Get ());
        }
 }
 
diff --git a/plugins/python-loader/gnm-python.c b/plugins/python-loader/gnm-python.c
index d49319114..4bc517352 100644
--- a/plugins/python-loader/gnm-python.c
+++ b/plugins/python-loader/gnm-python.c
@@ -71,11 +71,13 @@ gnm_python_finalize (GObject *obj)
 
        g_source_remove_by_user_data (gnm_python_obj);
        if (gpy->default_interpreter != NULL) {
-               GO_SLIST_FOREACH (gpy->interpreters, GnmPyInterpreter, interpreter,
+               GSList *ints = g_slist_copy (gpy->interpreters);
+               GO_SLIST_FOREACH (ints, GnmPyInterpreter, interpreter,
                        if (interpreter != gpy->default_interpreter) {
                                gnm_py_interpreter_destroy (interpreter, gpy->default_interpreter);
                        }
                );
+               g_slist_free (ints);
                gnm_py_interpreter_switch_to (gpy->default_interpreter);
                g_object_unref (gpy->default_interpreter);
                gpy->default_interpreter = NULL;
@@ -122,7 +124,7 @@ gnm_python_object_get (GOErrorInfo **err)
        GO_INIT_RET_ERROR_INFO (err);
        if (!Py_IsInitialized ()) {
                PyImport_AppendInittab ("Gnumeric", py_initgnumeric);
-               Py_Initialize ();
+               Py_InitializeEx (1);
 #ifdef WITH_THREAD
                PyEval_InitThreads ();
 #endif
diff --git a/plugins/python-loader/python-loader.c b/plugins/python-loader/python-loader.c
index 14d2869c5..2e6616d53 100644
--- a/plugins/python-loader/python-loader.c
+++ b/plugins/python-loader/python-loader.c
@@ -489,7 +489,7 @@ call_python_function_args (GnmFuncEvalInfo *ei, GnmValue const * const *args)
        SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service));
        fn_info_tuple = PyDict_GetItemString (loader_data->python_fn_info_dict,
                                              gnm_func_get_name (fndef, FALSE));
-       g_assert (fn_info_tuple != NULL);
+       g_assert (fn_info_tuple != NULL && PyTuple_Check (fn_info_tuple));
        python_fn = PyTuple_GetItem (fn_info_tuple, 2);
        gnm_func_count_args (fndef, &min_n_args, &max_n_args);
        for (n_args = min_n_args; n_args < max_n_args && args[n_args] != NULL; n_args++) {
@@ -549,7 +549,9 @@ python_function_get_gnumeric_help (PyObject *python_fn_info_dict, PyObject *pyth
        PyObject *fn_info_obj;
 
        fn_info_obj = PyDict_GetItemString (python_fn_info_dict, fn_name);
-       python_arg_names = PyTuple_GetItem (fn_info_obj, 1);
+       python_arg_names = PyTuple_Check (fn_info_obj)
+               ? PyTuple_GetItem (fn_info_obj, 1)
+               : NULL;
 
        help_attr_name = g_strdup_printf ("_CGnumericHelp_%s", fn_name);
        cobject_help_value = PyDict_GetItemString (python_fn_info_dict, help_attr_name);
@@ -782,6 +784,7 @@ gplp_unload_service_function_group (GOPluginLoader *loader,
        loader_data = g_object_get_data (G_OBJECT (service), "loader_data");
        SWITCH_TO_PLUGIN (go_plugin_service_get_plugin (service));
        Py_DECREF (loader_data->python_fn_info_dict);
+       loader_data->python_fn_info_dict = NULL;
 }
 
 typedef struct {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]