From d5b598b14fd4c50847ce536692ded258ba1720ca Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Wed, 12 Dec 2018 18:02:49 +0100
Subject: Registered basic Python objects in the features namespace.

---
 plugins/pychrysalide/analysis/db/certs.c     |   6 +-
 plugins/pychrysalide/arch/instriter.c        |   7 +-
 plugins/pychrysalide/arch/vmpa.c             |  12 +--
 plugins/pychrysalide/common/bits.c           |   6 +-
 plugins/pychrysalide/common/fnv1a.c          |   9 +-
 plugins/pychrysalide/common/pathname.c       |   7 +-
 plugins/pychrysalide/format/symiter.c        |   7 +-
 plugins/pychrysalide/glibext/configuration.c |   8 +-
 plugins/pychrysalide/gui/core/items.c        |   6 +-
 plugins/pychrysalide/helpers.c               | 135 +++++++++++++++++++++------
 plugins/pychrysalide/helpers.h               |   3 +
 plugins/pychrysalide/struct.c                |  11 +--
 12 files changed, 129 insertions(+), 88 deletions(-)

diff --git a/plugins/pychrysalide/analysis/db/certs.c b/plugins/pychrysalide/analysis/db/certs.c
index 385e277..61b5f58 100644
--- a/plugins/pychrysalide/analysis/db/certs.c
+++ b/plugins/pychrysalide/analysis/db/certs.c
@@ -312,7 +312,6 @@ bool ensure_python_certs_is_registered(void)
 {
     PyTypeObject *type;                     /* Type Python pour 'certs'    */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
 
     type = get_python_certs_type();
 
@@ -325,10 +324,7 @@ bool ensure_python_certs_is_registered(void)
 
         module = get_access_to_python_module("pychrysalide.analysis.db");
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "certs", (PyObject *)type);
-
-        if (ret != 0)
+        if (!register_python_module_object(module, type))
             return false;
 
     }
diff --git a/plugins/pychrysalide/arch/instriter.c b/plugins/pychrysalide/arch/instriter.c
index f665cba..9b149fd 100644
--- a/plugins/pychrysalide/arch/instriter.c
+++ b/plugins/pychrysalide/arch/instriter.c
@@ -33,6 +33,7 @@
 
 #include "processor.h"
 #include "../access.h"
+#include "../helpers.h"
 
 
 
@@ -263,7 +264,6 @@ bool ensure_python_instr_iterator_is_registered(void)
 {
     PyTypeObject *type;                     /* Type Python 'InstrIterator' */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
 
     type = get_python_instr_iterator_type();
 
@@ -274,10 +274,7 @@ bool ensure_python_instr_iterator_is_registered(void)
 
         module = get_access_to_python_module("pychrysalide.arch");
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "InstrIterator", (PyObject *)type);
-
-        if (ret != 0)
+        if (!register_python_module_object(module, type))
             return false;
 
     }
diff --git a/plugins/pychrysalide/arch/vmpa.c b/plugins/pychrysalide/arch/vmpa.c
index cdf20c9..f2450bb 100644
--- a/plugins/pychrysalide/arch/vmpa.c
+++ b/plugins/pychrysalide/arch/vmpa.c
@@ -662,7 +662,6 @@ bool ensure_python_vmpa_is_registered(void)
 {
     PyTypeObject *type;                     /* Type Python pour 'vmpa'     */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
 
     type = get_python_vmpa_type();
 
@@ -676,10 +675,7 @@ bool ensure_python_vmpa_is_registered(void)
 
         module = get_access_to_python_module("pychrysalide.arch");
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "vmpa", (PyObject *)type);
-
-        if (ret != 0)
+        if (!register_python_module_object(module, type))
             return false;
 
     }
@@ -1260,7 +1256,6 @@ bool ensure_python_mrange_is_registered(void)
 {
     PyTypeObject *type;                     /* Type Python pour 'mrange'   */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
 
     type = get_python_mrange_type();
 
@@ -1271,10 +1266,7 @@ bool ensure_python_mrange_is_registered(void)
 
         module = get_access_to_python_module("pychrysalide.arch");
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "mrange", (PyObject *)type);
-
-        if (ret != 0)
+        if (!register_python_module_object(module, type))
             return false;
 
     }
diff --git a/plugins/pychrysalide/common/bits.c b/plugins/pychrysalide/common/bits.c
index 62aeb6d..7a6454d 100644
--- a/plugins/pychrysalide/common/bits.c
+++ b/plugins/pychrysalide/common/bits.c
@@ -762,7 +762,6 @@ bool ensure_python_bitfield_is_registered(void)
 {
     PyTypeObject *type;                     /* Type Python pour 'bitfield' */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
 
     type = get_python_bitfield_type();
 
@@ -773,10 +772,7 @@ bool ensure_python_bitfield_is_registered(void)
 
         module = get_access_to_python_module("pychrysalide.common");
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "bitfield", (PyObject *)type);
-
-        if (ret != 0)
+        if (!register_python_module_object(module, type))
             return false;
 
     }
diff --git a/plugins/pychrysalide/common/fnv1a.c b/plugins/pychrysalide/common/fnv1a.c
index 759c7ea..5caad48 100644
--- a/plugins/pychrysalide/common/fnv1a.c
+++ b/plugins/pychrysalide/common/fnv1a.c
@@ -32,6 +32,7 @@
 
 
 #include "../access.h"
+#include "../helpers.h"
 
 
 
@@ -130,9 +131,8 @@ PyTypeObject *get_python_fnv1a_type(void)
 
 bool ensure_python_fnv1a_is_registered(void)
 {
-    PyTypeObject *type;                     /* Type Python pour 'fnv1a'   */
+    PyTypeObject *type;                     /* Type Python pour 'fnv1a'    */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
 
     type = get_python_fnv1a_type();
 
@@ -145,10 +145,7 @@ bool ensure_python_fnv1a_is_registered(void)
 
         module = get_access_to_python_module("pychrysalide.common");
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "fnv1a", (PyObject *)type);
-
-        if (ret != 0)
+        if (!register_python_module_object(module, type))
             return false;
 
     }
diff --git a/plugins/pychrysalide/common/pathname.c b/plugins/pychrysalide/common/pathname.c
index c2d69c6..e07ebe5 100644
--- a/plugins/pychrysalide/common/pathname.c
+++ b/plugins/pychrysalide/common/pathname.c
@@ -36,6 +36,7 @@
 
 
 #include "../access.h"
+#include "../helpers.h"
 
 
 
@@ -190,7 +191,6 @@ bool ensure_python_pathname_is_registered(void)
 {
     PyTypeObject *type;                     /* Type Python pour 'pathname' */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
 
     type = get_python_pathname_type();
 
@@ -203,10 +203,7 @@ bool ensure_python_pathname_is_registered(void)
 
         module = get_access_to_python_module("pychrysalide.common");
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "pathname", (PyObject *)type);
-
-        if (ret != 0)
+        if (!register_python_module_object(module, type))
             return false;
 
     }
diff --git a/plugins/pychrysalide/format/symiter.c b/plugins/pychrysalide/format/symiter.c
index 74a215d..03a0ec8 100644
--- a/plugins/pychrysalide/format/symiter.c
+++ b/plugins/pychrysalide/format/symiter.c
@@ -33,6 +33,7 @@
 
 #include "format.h"
 #include "../access.h"
+#include "../helpers.h"
 
 
 
@@ -263,7 +264,6 @@ bool ensure_python_sym_iterator_is_registered(void)
 {
     PyTypeObject *type;                     /* Type Python 'SymIterator'   */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
 
     type = get_python_sym_iterator_type();
 
@@ -274,10 +274,7 @@ bool ensure_python_sym_iterator_is_registered(void)
 
         module = get_access_to_python_module("pychrysalide.format");
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "SymIterator", (PyObject *)type);
-
-        if (ret != 0)
+        if (!register_python_module_object(module, type))
             return false;
 
     }
diff --git a/plugins/pychrysalide/glibext/configuration.c b/plugins/pychrysalide/glibext/configuration.c
index 18d681f..e74f0a4 100644
--- a/plugins/pychrysalide/glibext/configuration.c
+++ b/plugins/pychrysalide/glibext/configuration.c
@@ -782,9 +782,8 @@ PyTypeObject *get_python_config_param_iterator_type(void)
 
 bool ensure_python_config_param_iterator_is_registered(void)
 {
-    PyTypeObject *type;                     /* Type Python 'Cnf...Iter'*/
+    PyTypeObject *type;                     /* Type Python 'Cnf...Iter'    */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
 
     type = get_python_config_param_iterator_type();
 
@@ -797,10 +796,7 @@ bool ensure_python_config_param_iterator_is_registered(void)
         if (PyType_Ready(type) != 0)
             return false;
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "ConfigParamIterator", (PyObject *)type);
-
-        if (ret != 0)
+        if (!register_python_module_object(module, type))
             return false;
 
     }
diff --git a/plugins/pychrysalide/gui/core/items.c b/plugins/pychrysalide/gui/core/items.c
index 82c2c1c..0912bc4 100644
--- a/plugins/pychrysalide/gui/core/items.c
+++ b/plugins/pychrysalide/gui/core/items.c
@@ -258,7 +258,6 @@ bool ensure_python_items_is_registered(void)
 {
     PyTypeObject *type;                     /* Type Python de 'items'      */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
 
     type = get_python_items_type();
 
@@ -271,10 +270,7 @@ bool ensure_python_items_is_registered(void)
 
         module = get_access_to_python_module("pychrysalide.gui.core");
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "items", (PyObject *)type);
-
-        if (ret != 0)
+        if (!register_python_module_object(module, type))
             return false;
 
     }
diff --git a/plugins/pychrysalide/helpers.c b/plugins/pychrysalide/helpers.c
index 5ef10eb..be580a4 100644
--- a/plugins/pychrysalide/helpers.c
+++ b/plugins/pychrysalide/helpers.c
@@ -35,6 +35,14 @@
 
 
 
+/* ---------------------------- MISE EN PLACE DE MODULES ---------------------------- */
+
+
+/* Ajoute une classe dans les fonctionnalités globales. */
+static bool include_python_type_into_features(PyObject *, PyTypeObject *);
+
+
+
 /* ---------------------------------------------------------------------------------- */
 /*                        ACCELERATEURS POUR PYTHON UNIQUEMENT                        */
 /* ---------------------------------------------------------------------------------- */
@@ -297,7 +305,11 @@ PyObject *build_python_module(PyObject *super, PyModuleDef *def)
 
     Py_INCREF(result);
     ret = PyModule_AddObject(super, dot + 1, result);
-    if (ret != 0) goto bad_exit;
+    if (ret != 0)
+    {
+        Py_DECREF(result);
+        goto bad_exit;
+    }
 
     register_access_to_python_module(def->m_name, result);
 
@@ -372,6 +384,94 @@ bool register_python_module_methods(PyObject *module, PyMethodDef *defs)
 }
 
 
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : dict = dictionnaire où conserver une référence au type créé. *
+*                type = type dans sa version Python.                          *
+*                                                                             *
+*  Description : Ajoute une classe dans les fonctionnalités globales.         *
+*                                                                             *
+*  Retour      : Bilan de l'opération.                                        *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static bool include_python_type_into_features(PyObject *dict, PyTypeObject *type)
+{
+    bool result;                            /* Bilan à retourner           */
+    PyObject *features;                     /* Module à recompléter        */
+    PyObject *features_dict;                /* Dictionnaire à compléter    */
+    char *name;                             /* Désignation de la classe    */
+    PyObject *item;                         /* Nouvel élément à exporter   */
+    int ret;                                /* Bilan d'une insertion       */
+
+    features = get_access_to_python_module("pychrysalide.features");
+
+    features_dict = PyModule_GetDict(features);
+
+    name = strrchr(type->tp_name, '.');
+    assert(name != NULL);
+
+    name++;
+
+    item = PyDict_GetItemString(dict, name);
+    result = (item != NULL);
+    assert(result);
+
+    ret = PyDict_SetItemString(features_dict, name, item);
+    result = (ret == 0);
+    assert(result);
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : module = module dont la définition est à compléter.          *
+*                type   = type à intégrer dans sa version Python.             *
+*                                                                             *
+*  Description : Met en place un objet au sein d'un module Python.            *
+*                                                                             *
+*  Retour      : Bilan de l'opération.                                        *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+bool register_python_module_object(PyObject *module, PyTypeObject *type)
+{
+    bool result;                            /* Bilan à retourner           */
+    char *name;                             /* Désignation de la classe    */
+    int ret;                                /* Bilan d'un appel            */
+    PyObject *dict;                         /* Dictionnaire du module      */
+
+    name = strrchr(type->tp_name, '.');
+    assert(name != NULL);
+
+    name++;
+
+    Py_INCREF(type);
+    ret = PyModule_AddObject(module, name, (PyObject *)type);
+
+    result = (ret == 0);
+
+    if (!result)
+        Py_DECREF(type);
+
+    else
+    {
+        dict = PyModule_GetDict(module);
+        result = include_python_type_into_features(dict, type);
+    }
+
+    return result;
+
+}
+
+
 
 /* ---------------------------------------------------------------------------------- */
 /*                             CONFORTS CIBLANT PYGOBJECT                             */
@@ -494,10 +594,10 @@ PyTypeObject *define_python_dynamic_type(const PyTypeObject *spec)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : module = module où conserver une référence au type créé.     *
-*                gtype  = type dans sa version GLib.                          *
-*                type   = type dans sa version Python.                        *
-*                base   = type de base de l'objet.                            *
+*  Paramètres  : dict  = dictionnaire où conserver une référence au type créé.*
+*                gtype = type dans sa version GLib.                           *
+*                type  = type dans sa version Python.                         *
+*                base  = type de base de l'objet.                             *
 *                                                                             *
 *  Description : Enregistre correctement une surcouche de conversion GObject. *
 *                                                                             *
@@ -514,11 +614,6 @@ bool _register_class_for_pygobject(PyObject *dict, GType gtype, PyTypeObject *ty
     PyObject *static_bases;                 /* Base(s) de l'objet          */
     va_list ap;                             /* Parcours des arguments      */
     PyTypeObject *static_base;              /* Base à rajouter à la liste  */
-    PyObject *features;                     /* Module à recompléter        */
-    PyObject *features_dict;                /* Dictionnaire à compléter    */
-    char *name;                             /* Désignation de la classe    */
-    PyObject *item;                         /* Nouvel élément à exporter   */
-    int ret;                                /* Bilan d'une insertion       */
 
     /**
      * pygobject_register_class() définit type->tp_base à partir des arguments fournis,
@@ -597,25 +692,7 @@ bool _register_class_for_pygobject(PyObject *dict, GType gtype, PyTypeObject *ty
      */
 
     if (result)
-    {
-        features = get_access_to_python_module("pychrysalide.features");
-
-        features_dict = PyModule_GetDict(features);
-
-        name = strrchr(type->tp_name, '.');
-        assert(name != NULL);
-
-        name++;
-
-        item = PyDict_GetItemString(dict, name);
-        assert(item != NULL);
-
-        ret = PyDict_SetItemString(features_dict, name, item);
-        assert(ret == 0);
-
-        result = (ret == 0);
-
-    }
+        result = include_python_type_into_features(dict, type);
 
     return result;
 
diff --git a/plugins/pychrysalide/helpers.h b/plugins/pychrysalide/helpers.h
index 486ebdc..9f02daa 100644
--- a/plugins/pychrysalide/helpers.h
+++ b/plugins/pychrysalide/helpers.h
@@ -67,6 +67,9 @@ PyObject *build_python_module(PyObject *, PyModuleDef *);
 /* Met en place une série de méthodes pour un module Python. */
 bool register_python_module_methods(PyObject *, PyMethodDef *);
 
+/* Met en place un objet au sein d'un module Python. */
+bool register_python_module_object(PyObject *, PyTypeObject *);
+
 
 
 /* --------------------------- CONFORTS CIBLANT PYGOBJECT --------------------------- */
diff --git a/plugins/pychrysalide/struct.c b/plugins/pychrysalide/struct.c
index 98704c0..d4d6a9f 100644
--- a/plugins/pychrysalide/struct.c
+++ b/plugins/pychrysalide/struct.c
@@ -26,6 +26,7 @@
 
 
 #include "access.h"
+#include "helpers.h"
 
 
 
@@ -147,7 +148,6 @@ bool ensure_python_py_struct_is_registered(void)
 {
     PyTypeObject *type;                     /* Type Python 'PyStructObject'*/
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan des préparatifs       */
 
     type = get_python_py_struct_type();
 
@@ -155,14 +155,11 @@ bool ensure_python_py_struct_is_registered(void)
     {
         module = get_access_to_python_module("pychrysalide");
 
-        ret = PyType_Ready(type);
-
-        if (ret != 0)
+        if (PyType_Ready(type) != 0)
             return false;
 
-        Py_INCREF(type);
-
-        PyModule_AddObject(module, "PyStructObject", (PyObject *)type);
+        if (!register_python_module_object(module, type))
+            return false;
 
     }
 
-- 
cgit v0.11.2-87-g4458