diff options
Diffstat (limited to 'plugins/pychrysa')
-rw-r--r-- | plugins/pychrysa/pychrysa.c | 144 |
1 files changed, 134 insertions, 10 deletions
diff --git a/plugins/pychrysa/pychrysa.c b/plugins/pychrysa/pychrysa.c index 1835b18..2b8202e 100644 --- a/plugins/pychrysa/pychrysa.c +++ b/plugins/pychrysa/pychrysa.c @@ -347,7 +347,6 @@ PyMODINIT_FUNC initpychrysa(void) #include <pygobject.h> -#include "../../revision.h" @@ -368,9 +367,45 @@ PyMODINIT_FUNC initpychrysa(void) + + + + + +/////////////////////////// + + +#include <stdio.h> +#include <stdbool.h> +#include <string.h> +#include <unistd.h> + + +#include <config.h> +#include <plugins/plugin-def.h> +#include <plugins/plugin-int.h> + + +#include "../../revision.h" + + + + + + +DEFINE_CHRYSALIDE_ACTIVE_PLUGIN("PyChrysalide", "Provides bindings to Python", "0.1.0", PGA_ALL); + + /* Fournit la version du programme global. */ static PyObject *py_chrysalide_version(PyObject *, PyObject *); +/* Fournit la version du greffon pour Python. */ +static PyObject *py_chrysalide_mod_version(PyObject *, PyObject *); + +/* Détermine si l'interpréteur lancé est celui pris en compte. */ +static bool is_current_abi_suitable(void); + + @@ -405,9 +440,90 @@ static PyObject *py_chrysalide_version(PyObject *self, PyObject *args) } +/****************************************************************************** +* * +* Paramètres : self = NULL car méthode statique. * +* args = non utilisé ici. * +* * +* Description : Fournit la version du greffon pour Python. * +* * +* Retour : Numéro de révision. * +* * +* Remarques : - * +* * +******************************************************************************/ +static PyObject *py_chrysalide_mod_version(PyObject *self, PyObject *args) +{ + char version[16]; + snprintf(version, sizeof(version), "%s", _chrysalide_plugin.version); + return PyUnicode_FromString(version); + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Détermine si l'interpréteur lancé est celui pris en compte. * +* * +* Retour : true si l'exécution peut se poursuivre, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool is_current_abi_suitable(void) +{ + bool result; + int fds[2]; + int ret; + char cmds[128]; + char content[64]; + ssize_t got; + +#define GRAB_ABI_FLAGS_IN_PYTHON \ + "import sys" "\n" \ + "import os" "\n" \ + "os.write(%d, bytes(sys.abiflags, 'UTF-8'))" "\n" + + result = false; + + ret = pipe(fds); + if (ret == -1) + { + perror("pipe()"); + return false; + } + + snprintf(cmds, sizeof(cmds), GRAB_ABI_FLAGS_IN_PYTHON, fds[1]); + + ret = PyRun_SimpleString(cmds); + if (ret != 0) goto icas_exit; + + got = read(fds[0], content, sizeof(content)); + if (got < 0) + { + perror("read()"); + goto icas_exit; + } + + content[got] = '\0'; + + result = (strcmp(content, LIBPYTHON_ABI_FLAGS) == 0); + + icas_exit: + + if (!result) + PyErr_SetString(PyExc_SystemError, "the ABI flags of the current interpreter do not match " \ + "the ones of the Python library used during the module compilation."); + + return result; + +} /****************************************************************************** @@ -434,7 +550,7 @@ PyMODINIT_FUNC PyInit_pychrysalide(void) METH_NOARGS, "Provide the revision number of Chrysalide." }, - { "python", py_chrysalide_version, + { "mod_version", py_chrysalide_mod_version, METH_NOARGS, "Provide the revision number of Chrysalide module for Python." }, @@ -457,18 +573,24 @@ PyMODINIT_FUNC PyInit_pychrysalide(void) // TODO : à bouger ! - init_all_processors(); - init_all_formats(); + //init_all_processors(); + //init_all_formats(); + if (!is_current_abi_suitable()) + return NULL; + if (pygobject_init(-1, -1, -1) == NULL) + { + PyErr_SetString(PyExc_SystemError, "unable to init GObject in Python."); return NULL; + } if (!load_all_basic_components()) + { + PyErr_SetString(PyExc_SystemError, "unable to load all basic components."); return NULL; - - - + } /** * Pour une raison non identifiée, si le module n'est pas préchargé, @@ -487,9 +609,11 @@ PyMODINIT_FUNC PyInit_pychrysalide(void) status &= add_core_module_to_python_module(result); status &= add_glibext_module_to_python_module(result); - printf("status :: %d\n", status); - - /* TODO : if !status... */ + if (!status) + { + PyErr_SetString(PyExc_SystemError, "fail to load all PyChrysalide components."); + return NULL; + } return result; |