Commit c8c9bcaf authored by Morten Welinder's avatar Morten Welinder
Browse files

PythonLoader: cleanups and fixes.

Don't access a non-tuple (for py_printf) as a tuple.
parent 56e84eb0
......@@ -2,6 +2,7 @@ Gnumeric 1.12.50
Morten:
* Fix undo problem with sorting and cell comments.
* Python loader cleanups.
--------------------------------------------------------------------------
Gnumeric 1.12.49
......
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
......
......@@ -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 ());
}
}
......
......@@ -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
......
......@@ -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 {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment