From 135a05b755d4dcf8c3fadc7befe6229dfcdc1e4f Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Sat, 26 Jan 2019 13:40:43 +0100 Subject: Loaded Python plugins even from the Python interpreter. --- plugins/pychrysalide/pychrysa.c | 93 ++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 19 deletions(-) diff --git a/plugins/pychrysalide/pychrysa.c b/plugins/pychrysalide/pychrysa.c index 502bf20..928743d 100644 --- a/plugins/pychrysalide/pychrysa.c +++ b/plugins/pychrysalide/pychrysa.c @@ -94,6 +94,9 @@ static bool is_current_abi_suitable(void); /* Définit la version attendue de GTK à charger dans Python. */ static bool set_version_for_gtk_namespace(const char *); +/* Complète les chemins de recherches de Python. */ +static void extend_python_path(const char *); + /* Charge autant de greffons composés en Python que possible. */ static void load_python_plugins(GPluginModule *); @@ -347,6 +350,27 @@ PyMODINIT_FUNC PyInit_pychrysalide(void) }; + /** + * Vérification préalable : dans le cas où on est embarqué directement dans + * un interpréteur Python, le module se charge et termine par charger à leur + * tour les différentes extensions trouvées, via load_remaning_plugins() puis + * chrysalide_plugin_on_native_loaded(). + * + * Lesquelles vont très probablement charger le module pychrysalide. + * + * Comme le chargement de ce dernier n'est alors pas encore terminé, + * Python va relancer cette procédure, et register_access_to_python_module() + * va détecter un doublon. + */ + + result = get_access_to_python_module(py_chrysalide_module.m_name); + + if (result != NULL) + { + Py_INCREF(result); + return result; + } + if (!is_current_abi_suitable()) return NULL; @@ -445,6 +469,37 @@ PyMODINIT_FUNC PyInit_pychrysalide(void) } +/****************************************************************************** +* * +* Paramètres : path = chemin supplémentaire pour l'espace de recherche. * +* * +* Description : Complète les chemins de recherches de Python. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void extend_python_path(const char *path) +{ + PyObject *list; /* Liste de chemins à compléter*/ + PyObject *new; /* Nouveau chemin à intégrer */ + + list = PySys_GetObject("path"); + assert(list != NULL); + + new = PyUnicode_FromString(path); + assert(new != NULL); + + PyList_Append(list, new); + + Py_DECREF(new); + + add_to_env_var("PYTHONPATH", path, ":"); + +} + /****************************************************************************** * * @@ -460,15 +515,33 @@ PyMODINIT_FUNC PyInit_pychrysalide(void) static void load_python_plugins(GPluginModule *plugin) { + DIR *dir; /* Répertoire à parcourir */ char *paths; /* Emplacements de greffons */ char *save; /* Sauvegarde pour ré-entrance */ char *path; /* Chemin à fouiller */ - DIR *dir; /* Répertoire à parcourir */ struct dirent *entry; /* Elément trouvé */ char *modname; /* Nom du module pour Python */ char *filename; /* Chemin d'accès reconstruit */ GPluginModule *pyplugin; /* Lien vers un grffon Python */ + /* Définition des zones d'influence */ + + dir = opendir(PLUGINS_DATA_DIR G_DIR_SEPARATOR_S "python"); + + if (dir != NULL) + { + closedir(dir); + extend_python_path(PLUGINS_DATA_DIR G_DIR_SEPARATOR_S "python"); + } + else + extend_python_path(PACKAGE_SOURCE_DIR G_DIR_SEPARATOR_S "plugins" G_DIR_SEPARATOR_S "python"); + + g_plugin_module_log_variadic_message(plugin, LMT_INFO, + _("PYTHONPATH environment variable set to '%s'"), + getenv("PYTHONPATH")); + + /* Chargements des extensions Python */ + paths = get_env_var("PYTHONPATH"); save = NULL; /* gcc... */ @@ -563,28 +636,10 @@ static void load_python_plugins(GPluginModule *plugin) G_MODULE_EXPORT bool chrysalide_plugin_init(GPluginModule *plugin) { bool result; /* Bilan à retourner */ - DIR *dir; /* Répertoire à parcourir */ int ret; /* Bilan de préparatifs */ _this = plugin; - /* Définition des zones d'influence */ - - dir = opendir(PLUGINS_DATA_DIR G_DIR_SEPARATOR_S "python"); - - if (dir != NULL) - { - closedir(dir); - add_to_env_var("PYTHONPATH", PLUGINS_DATA_DIR G_DIR_SEPARATOR_S "python", ":"); - } - else - add_to_env_var("PYTHONPATH", PACKAGE_SOURCE_DIR G_DIR_SEPARATOR_S "plugins" \ - G_DIR_SEPARATOR_S "python", ":"); - - g_plugin_module_log_variadic_message(plugin, LMT_INFO, - _("PYTHONPATH environment variable set to '%s'"), - getenv("PYTHONPATH")); - /* Chargement du module pour Python */ ret = PyImport_AppendInittab("pychrysalide", &PyInit_pychrysalide); -- cgit v0.11.2-87-g4458