summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/core.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2022-08-18 19:05:33 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2022-08-18 19:05:33 (GMT)
commitf22f73bcbdb6510169c8b7c7c3fea842750c6fe5 (patch)
treec621257695d9f132dff88b9149ee86d3b0888236 /plugins/pychrysalide/core.c
parent429b54556283116a29c5d699af0cf891bb1c1055 (diff)
Handle the Python Global Interpreter Lock with more care.
Diffstat (limited to 'plugins/pychrysalide/core.c')
-rw-r--r--plugins/pychrysalide/core.c67
1 files changed, 20 insertions, 47 deletions
diff --git a/plugins/pychrysalide/core.c b/plugins/pychrysalide/core.c
index ef119fb..c062d15 100644
--- a/plugins/pychrysalide/core.c
+++ b/plugins/pychrysalide/core.c
@@ -85,9 +85,6 @@ static bool _standalone = true;
/* Réceptacle pour le chargement forcé */
static PyObject *_chrysalide_module = NULL;
-/* Conservation des informations du thread principal */
-static PyThreadState *_main_tstate = NULL;
-
/* Fournit la révision du programme global. */
static PyObject *py_chrysalide_revision(PyObject *, PyObject *);
@@ -656,7 +653,6 @@ static void load_python_plugins(GPluginModule *plugin)
struct dirent *entry; /* Elément trouvé */
char *modname; /* Nom du module pour Python */
char *filename; /* Chemin d'accès reconstruit */
- PyThreadState *tstate; /* Contexte d'environnement */
GPluginModule *pyplugin; /* Lien vers un grffon Python */
bool status; /* Bilan d'une opération */
GGenConfig *config; /* Configuration à charger */
@@ -736,17 +732,8 @@ static void load_python_plugins(GPluginModule *plugin)
filename = stradd(filename, G_DIR_SEPARATOR_S);
filename = stradd(filename, entry->d_name);
- if (!_standalone)
- {
- tstate = get_pychrysalide_main_tstate();
- PyEval_RestoreThread(tstate);
- }
-
pyplugin = g_python_plugin_new(modname, filename);
- if (!_standalone)
- PyEval_SaveThread();
-
if (pyplugin == NULL)
{
g_plugin_module_log_variadic_message(plugin, LMT_ERROR,
@@ -808,6 +795,7 @@ static void load_python_plugins(GPluginModule *plugin)
G_MODULE_EXPORT bool chrysalide_plugin_init(GPluginModule *plugin)
{
bool result; /* Bilan à retourner */
+ PyGILState_STATE gstate; /* Sauvegarde d'environnement */
int ret; /* Bilan de préparatifs */
_standalone = false;
@@ -825,6 +813,8 @@ G_MODULE_EXPORT bool chrysalide_plugin_init(GPluginModule *plugin)
Py_Initialize();
+ gstate = PyGILState_Ensure();
+
PySys_SetArgv(0, (wchar_t *[]) { NULL });
_chrysalide_module = PyImport_ImportModule("pychrysalide");
@@ -842,9 +832,7 @@ G_MODULE_EXPORT bool chrysalide_plugin_init(GPluginModule *plugin)
result = (_chrysalide_module != NULL);
- _main_tstate = PyThreadState_Get();
-
- PyEval_ReleaseLock();
+ PyGILState_Release(gstate);
cpi_done:
@@ -867,10 +855,16 @@ G_MODULE_EXPORT bool chrysalide_plugin_init(GPluginModule *plugin)
G_MODULE_EXPORT void chrysalide_plugin_exit(GPluginModule *plugin)
{
+ PyGILState_STATE gstate; /* Sauvegarde d'environnement */
+
+ gstate = PyGILState_Ensure();
+
clear_all_accesses_to_python_modules();
Py_XDECREF(_chrysalide_module);
+ PyGILState_Release(gstate);
+
}
@@ -911,6 +905,7 @@ static void free_native_plugin_type(PyTypeObject *type)
G_MODULE_EXPORT void chrysalide_plugin_on_plugins_loaded(GPluginModule *plugin, PluginAction action)
{
+ PyGILState_STATE gstate; /* Sauvegarde d'environnement */
size_t count; /* Quantité de greffons chargés*/
PyTypeObject *parent; /* Type Python pour greffon */
PyObject *module; /* Module à recompléter */
@@ -922,6 +917,8 @@ G_MODULE_EXPORT void chrysalide_plugin_on_plugins_loaded(GPluginModule *plugin,
int ret; /* Bilan d'un appel */
PyTypeObject *type; /* Nouveau type dynamique */
+ gstate = PyGILState_Ensure();
+
if (action == PGA_NATIVE_PLUGINS_LOADED)
{
/* Intégration des greffons natifs en Python */
@@ -982,6 +979,8 @@ G_MODULE_EXPORT void chrysalide_plugin_on_plugins_loaded(GPluginModule *plugin,
}
+ PyGILState_Release(gstate);
+
}
@@ -1002,17 +1001,13 @@ G_MODULE_EXPORT void chrysalide_plugin_on_plugins_loaded(GPluginModule *plugin,
G_MODULE_EXPORT gpointer chrysalide_plugin_build_type_instance(GPluginModule *plugin, PluginAction action, GType type)
{
gpointer result; /* Instance à retourner */
- PyThreadState *tstate; /* Contexte d'environnement */
+ PyGILState_STATE gstate; /* Sauvegarde d'environnement */
PyTypeObject *pytype; /* Classe Python concernée */
PyObject *instance; /* Initialisation forcée */
result = NULL;
- if (!_standalone)
- {
- tstate = get_pychrysalide_main_tstate();
- PyEval_RestoreThread(tstate);
- }
+ gstate = PyGILState_Ensure();
pytype = pygobject_lookup_class(type);
@@ -1025,31 +1020,7 @@ G_MODULE_EXPORT gpointer chrysalide_plugin_build_type_instance(GPluginModule *pl
}
- if (!_standalone)
- PyEval_SaveThread();
-
- return result;
-
-}
-
-
-/******************************************************************************
-* *
-* Paramètres : - *
-* *
-* Description : Fournit les informations du thread principal. *
-* *
-* Retour : Indications utiles à Python. *
-* *
-* Remarques : - *
-* *
-******************************************************************************/
-
-PyThreadState *get_pychrysalide_main_tstate(void)
-{
- PyThreadState *result; /* Indications à retourner */
-
- result = _main_tstate;
+ PyGILState_Release(gstate);
return result;
@@ -1078,6 +1049,8 @@ void log_pychrysalide_exception(const char *prefix, ...)
PyObject *err_string; /* Description Python d'erreur */
const char *err_msg; /* Représentation humaine */
+ assert(PyGILState_Check() == 1);
+
if (PyErr_Occurred())
{
/* Base de la communication */