From 4140c074a40ec1e1a68b6c2cb040b8746a7c0e34 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Mon, 11 Nov 2019 13:08:32 +0100
Subject: Printed more details when a Python plugin fails to load.

---
 plugins/pychrysalide/plugin.c   | 62 ++++++++++++-------------------------
 plugins/pychrysalide/pychrysa.c | 68 ++++++++++++++++++++++++++++++++++++++++-
 plugins/pychrysalide/pychrysa.h |  3 ++
 3 files changed, 89 insertions(+), 44 deletions(-)

diff --git a/plugins/pychrysalide/plugin.c b/plugins/pychrysalide/plugin.c
index ad77c34..a0822f7 100644
--- a/plugins/pychrysalide/plugin.c
+++ b/plugins/pychrysalide/plugin.c
@@ -978,60 +978,26 @@ GPluginModule *g_python_plugin_new(const char *modname, const char *filename)
     GPythonPlugin *result;                  /* Structure à retourner       */
     PyObject *name;                         /* Chemin d'accès pour Python  */
     PyObject *module;                       /* Script Python chargé        */
-    PyObject *err_type;                     /* Type d'erreur Python        */
-    PyObject *err_value;                    /* Instance Python d'erreur    */
-    PyObject *err_traceback;                /* Trace Python associée       */
-    PyObject *err_string;                   /* Description Python d'erreur */
-    const char *err_msg;                    /* Représentation humaine      */
     PyObject *dict;                         /* Dictionnaire associé        */
     PyObject *class;                        /* Classe à instancier         */
     PyObject *instance;                     /* Instance Python du greffon  */
 
     name = PyUnicode_FromString(modname);
-    if (name == NULL) goto gppn_bad_exit;
+    if (name == NULL) goto bad_exit;
 
     module = PyImport_Import(name);
     Py_DECREF(name);
 
-    if (PyErr_Occurred())
-    {
-        PyErr_Fetch(&err_type, &err_value, &err_traceback);
-
-        if (err_value == NULL)
-            log_variadic_message(LMT_ERROR,
-                                 _("An unknown error occured when importing '%s'..."), modname);
-        else
-        {
-            err_string = PyObject_Str(err_value);
-            err_msg = PyUnicode_AsUTF8(err_string);
-
-            log_variadic_message(LMT_ERROR,
-                                 _("An error occured when importing '%s': \"%s\""), modname, err_msg);
-
-            Py_DECREF(err_string);
-            Py_DECREF(err_value);
-
-        }
-
-        Py_XDECREF(err_traceback);
-        Py_XDECREF(err_type);
-
-        Py_XDECREF(module);
-
-        module = NULL;
-
-    }
-
-    if (module == NULL) goto gppn_bad_exit;
+    if (module == NULL) goto no_import;
 
     dict = PyModule_GetDict(module);
     class = PyDict_GetItemString(dict, "AutoLoad");
 
-    if (class == NULL) goto gppn_no_class;
-    if (!PyType_Check(class->ob_type)) goto gppn_no_class;
+    if (class == NULL) goto no_class;
+    if (!PyType_Check(class->ob_type)) goto no_class;
 
     instance = PyObject_CallFunction(class, NULL);
-    if (instance == NULL) goto gppn_no_instance;
+    if (instance == NULL) goto no_instance;
 
     result = G_PYTHON_PLUGIN(pygobject_get(instance));
 
@@ -1048,13 +1014,23 @@ GPluginModule *g_python_plugin_new(const char *modname, const char *filename)
 
     return G_PLUGIN_MODULE(result);
 
- gppn_no_instance:
+ no_instance:
 
- gppn_no_class:
+    log_pychrysalide_exception(_("An error occured when building the 'AutoLoad' instance"));
 
-    Py_DECREF(module);
+ no_class:
+
+    if (class == NULL)
+        log_pychrysalide_simple_message(LMT_ERROR,
+                                        _("An error occured when looking for the 'AutoLoad': item not found!"));
+
+ no_import:
+
+    Py_XDECREF(module);
+
+    log_pychrysalide_exception(_("An error occured when importing '%s'"), modname);
 
- gppn_bad_exit:
+ bad_exit:
 
     return NULL;
 
diff --git a/plugins/pychrysalide/pychrysa.c b/plugins/pychrysalide/pychrysa.c
index d97cbf0..f998b67 100644
--- a/plugins/pychrysalide/pychrysa.c
+++ b/plugins/pychrysalide/pychrysa.c
@@ -26,7 +26,9 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <malloc.h>
 #include <pygobject.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdbool.h>
 #include <string.h>
@@ -34,6 +36,7 @@
 
 
 #include <config.h>
+#include <i18n.h>
 #include <gleak.h>
 #include <common/cpp.h>
 #include <common/environment.h>
@@ -864,7 +867,6 @@ PyThreadState *get_pychrysalide_main_tstate(void)
 }
 
 
-
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : msg = message à faire apparaître à l'écran.                  *
@@ -886,3 +888,67 @@ void log_pychrysalide_simple_message(LogMessageType type, const char *msg)
         log_simple_message(type, msg);
 
 }
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : prefix = message d'introduction à faire apparaître à l'écran.*
+*                                                                             *
+*  Description : Présente dans le journal une exception survenue.             *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+void log_pychrysalide_exception(const char *prefix, ...)
+{
+    va_list ap;                             /* Compléments argumentaires   */
+    char *msg;                              /* Message complet à imprimer  */
+    PyObject *err_type;                     /* Type d'erreur Python        */
+    PyObject *err_value;                    /* Instance Python d'erreur    */
+    PyObject *err_traceback;                /* Trace Python associée       */
+    PyObject *err_string;                   /* Description Python d'erreur */
+    const char *err_msg;                    /* Représentation humaine      */
+
+    if (PyErr_Occurred())
+    {
+        /* Base de la communication */
+
+        va_start(ap, prefix);
+
+        vasprintf(&msg, prefix, ap);
+
+        va_end(ap);
+
+        /* Détails complémentaires */
+
+        PyErr_Fetch(&err_type, &err_value, &err_traceback);
+
+        if (err_value == NULL)
+            msg = stradd(msg, _("; no extra information is provided..."));
+
+        else
+        {
+            err_string = PyObject_Str(err_value);
+            err_msg = PyUnicode_AsUTF8(err_string);
+
+            msg = stradd(msg, ": ");
+            msg = stradd(msg, err_msg);
+
+            Py_DECREF(err_string);
+            Py_DECREF(err_value);
+
+        }
+
+        Py_XDECREF(err_traceback);
+        Py_XDECREF(err_type);
+
+        log_pychrysalide_simple_message(LMT_ERROR, msg);
+
+        free(msg);
+
+    }
+
+}
diff --git a/plugins/pychrysalide/pychrysa.h b/plugins/pychrysalide/pychrysa.h
index 74f699a..bef8963 100644
--- a/plugins/pychrysalide/pychrysa.h
+++ b/plugins/pychrysalide/pychrysa.h
@@ -58,6 +58,9 @@ PyThreadState *get_pychrysalide_main_tstate(void);
 /* Présente dans le journal un message simple. */
 void log_pychrysalide_simple_message(LogMessageType, const char *);
 
+/* Présente dans le journal une exception survenue. */
+void log_pychrysalide_exception(const char *, ...);
+
 
 
 #endif  /* _PLUGINS_PYCHRYSALIDE_PYCHRYSA_H */
-- 
cgit v0.11.2-87-g4458