summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/helpers.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2019-08-13 22:04:53 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2019-08-13 22:04:53 (GMT)
commit7cb4e815b691d05d8c0aea9decf56b9dbc41dfa6 (patch)
treeade598a747daa2e061f4903a81ac74e52e0c38b1 /plugins/pychrysalide/helpers.c
parentdd2853ea8cf97b773ba362da50d06d4abc608a09 (diff)
Used a Python enumeration for the binary symbol status.
Diffstat (limited to 'plugins/pychrysalide/helpers.c')
-rw-r--r--plugins/pychrysalide/helpers.c199
1 files changed, 199 insertions, 0 deletions
diff --git a/plugins/pychrysalide/helpers.c b/plugins/pychrysalide/helpers.c
index 6ffb4fe..6e13d3c 100644
--- a/plugins/pychrysalide/helpers.c
+++ b/plugins/pychrysalide/helpers.c
@@ -939,3 +939,202 @@ bool register_class_for_dynamic_pygobject(GType gtype, PyTypeObject *type, PyTyp
return result;
}
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/* TRANSFERT DES VALEURS CONSTANTES */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+* *
+* Paramètres : owner = type dont le dictionnaire est à compléter. *
+* flags = indique le type d'énumération ciblée. *
+* name = désignation humaine du groupe à constituer. *
+* values = noms et valeurs associées. *
+* doc = documentation à associer au groupe. *
+* *
+* Description : Officialise un groupe de constantes avec sémentique. *
+* *
+* Retour : true en cas de succès de l'opération, false sinon. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool attach_constants_group(PyTypeObject *owner, bool flags, const char *name, PyObject *values, const char *doc)
+{
+ bool result; /* Bilan à retourner */
+ PyObject *enum_mod; /* Module Python enum */
+ PyObject *class; /* Classe "Enum*" */
+ PyObject *str_obj; /* Conversion en Python */
+ int ret; /* Bilan d'une insertion */
+ PyObject *args; /* Arguments de la construction*/
+ PyObject *kwargs; /* Mots clefs en complément */
+ PyObject *new; /* Nouvelle instance en place */
+ PyObject *features; /* Module à recompléter */
+ PyObject *features_dict; /* Dictionnaire à compléter */
+
+ result = false;
+
+ /* Recherche de la classe Python */
+
+ enum_mod = PyImport_ImportModule("enum");
+
+ if (enum_mod == NULL)
+ goto no_mod;
+
+ if (flags)
+ class = PyObject_GetAttrString(enum_mod, "IntFlag");
+ else
+ class = PyObject_GetAttrString(enum_mod, "IntEnum");
+
+ Py_DECREF(enum_mod);
+
+ if (class == NULL)
+ goto no_class;
+
+ /* Compléments des paramètres */
+
+ str_obj = PyUnicode_FromString(doc);
+ ret = PyDict_SetItemString(values, "__doc__", str_obj);
+ Py_DECREF(str_obj);
+
+ if (ret != 0)
+ goto doc_error;
+
+ args = PyTuple_New(2);
+
+ ret = PyTuple_SetItem(args, 0, PyUnicode_FromString(name));
+ if (ret != 0) goto args_error;
+
+ Py_INCREF(values);
+ ret = PyTuple_SetItem(args, 1, values);
+ if (ret != 0) goto args_error;
+
+ kwargs = PyDict_New();
+
+ str_obj = PyUnicode_FromString(owner->tp_name);
+ ret = PyDict_SetItemString(kwargs, "module", str_obj);
+ Py_DECREF(str_obj);
+
+ if (ret != 0) goto kwargs_error;
+
+ /* Constitution de l'énumération et enregistrement */
+
+ new = PyObject_Call(class, args, kwargs);
+ if (new == NULL) goto build_error;
+
+ ret = PyDict_SetItemString(owner->tp_dict, name, new);
+ if (ret != 0) goto register_0_error;
+
+ features = get_access_to_python_module("pychrysalide.features");
+
+ features_dict = PyModule_GetDict(features);
+
+ ret = PyDict_SetItemString(features_dict, name, new);
+ if (ret != 0) goto register_1_error;
+
+ result = true;
+
+ /* Sortie propre */
+
+ register_1_error:
+ register_0_error:
+
+ Py_DECREF(new);
+
+ build_error:
+ kwargs_error:
+
+ Py_DECREF(kwargs);
+
+ args_error:
+
+ Py_DECREF(args);
+
+ doc_error:
+
+ Py_DECREF(class);
+
+ no_class:
+ no_mod:
+
+ Py_DECREF(values);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : owner = propriétaire du groupe de constantes. *
+* name = désignation humaine du groupe à consulter. *
+* value = valeur à transmettre à Python. *
+* *
+* Description : Traduit une valeur constante C en équivalent Python. *
+* *
+* Retour : Objet Python résultant ou NULL en cas d'erreur. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+PyObject *cast_with_constants_group(const PyTypeObject *owner, const char *name, unsigned long value)
+{
+ PyObject *result; /* Objet Python à retourner */
+ char *dot; /* Position du dernier point */
+ char *modname; /* Chemin d'accès au module */
+ PyObject *module; /* Module à consulter */
+ PyObject *type; /* Classe propriétaire */
+ PyObject *class; /* Classe "Enum*" */
+ PyObject *args; /* Arguments de la construction*/
+
+ result = NULL;
+
+ /* Recherche de la classe Python */
+
+ dot = strrchr(owner->tp_name, '.');
+ assert(dot != NULL);
+
+ modname = strndup(owner->tp_name, dot - owner->tp_name);
+
+ module = get_access_to_python_module(modname);
+
+ if (module == NULL)
+ goto no_mod;
+
+ type = PyObject_GetAttrString(module, dot + 1);
+
+ if (type == NULL)
+ goto no_type;
+
+ class = PyObject_GetAttrString(type, name);
+
+ if (class == NULL)
+ goto no_class;
+
+ /* Construction */
+
+ args = Py_BuildValue("(k)", value);
+
+ result = PyObject_CallObject(class, args);
+
+ Py_DECREF(args);
+
+ Py_DECREF(class);
+
+ no_class:
+
+ Py_DECREF(type);
+
+ no_type:
+ no_mod:
+
+ free(modname);
+
+ return result;
+
+}