/* Chrysalide - Outil d'analyse de fichiers binaires * work.c - prototypes pour l'équivalent Python du fichier "glibext/work.c" * * Copyright (C) 2024 Cyrille Bagard * * This file is part of Chrysalide. * * Chrysalide is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Chrysalide is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "work.h" #include #include #include #include "../access.h" #include "../helpers.h" /* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ /* Initialise la classe des travaux programmés. */ static void py_generic_work_init_gclass(GGenericWorkClass *, gpointer); CREATE_DYN_ABSTRACT_CONSTRUCTOR(generic_work, G_TYPE_GENERIC_WORK, py_generic_work_init_gclass); /* Initialise une instance sur la base du dérivé de GObject. */ static int py_generic_work_init(PyObject *, PyObject *, PyObject *); /* Mène l'opération programmée. */ static void py_generic_work_run_wrapper(GGenericWork *); /* ------------------------- CONNEXION AVEC L'API DE PYTHON ------------------------- */ /* Mène l'opération programmée. */ static PyObject *py_generic_work_process(PyObject *, PyObject *); /* ---------------------------------------------------------------------------------- */ /* GLUE POUR CREATION DEPUIS PYTHON */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : class = classe à initialiser. * * unused = données non utilisées ici. * * * * Description : Initialise la classe des travaux programmés. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void py_generic_work_init_gclass(GGenericWorkClass *class, gpointer unused) { class->run = py_generic_work_run_wrapper; } /****************************************************************************** * * * Paramètres : self = objet à initialiser (théoriquement). * * args = arguments fournis à l'appel. * * kwds = arguments de type key=val fournis. * * * * Description : Initialise une instance sur la base du dérivé de GObject. * * * * Retour : 0. * * * * Remarques : - * * * ******************************************************************************/ static int py_generic_work_init(PyObject *self, PyObject *args, PyObject *kwds) { int ret; /* Bilan de lecture des args. */ #define GENERIC_WORK_DOC \ "GenericWork defines a basic work aimed to get processed in a" \ " thread set up by a pychrysalide.glibext.WorkQueue object.\n" \ "\n" \ "Instances can be created using the following constructor:\n" \ "\n" \ " GenericWork()" \ "\n" \ "The following method has to be defined for new classes:\n" \ "* pychrysalide.glibext.GenericWork._run();\n" \ "\n" \ "The following signal may be emitted:\n" \ "* *work-completed* : when the work has been processed." /* Initialisation d'un objet GLib */ ret = forward_pygobjet_init(self); if (ret == -1) return -1; return 0; } /****************************************************************************** * * * Paramètres : work = travail à effectuer. * * * * Description : Mène l'opération programmée. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void py_generic_work_run_wrapper(GGenericWork *work) { PyGILState_STATE gstate; /* Sauvegarde d'environnement */ PyObject *pyobj; /* Objet Python concerné */ PyObject *pyret; /* Bilan de consultation */ #define GENERIC_WORK_RUN_WRAPPER PYTHON_WRAPPER_DEF \ ( \ _run, "$self", \ METH_NOARGS, \ "Abstract method used to process the work.\n" \ "\n" \ "No result is expected from this function." \ ) gstate = PyGILState_Ensure(); pyobj = pygobject_new(G_OBJECT(work)); if (has_python_method(pyobj, "_run")) { pyret = run_python_method(pyobj, "_run", NULL); Py_XDECREF(pyret); } Py_DECREF(pyobj); PyGILState_Release(gstate); } /* ---------------------------------------------------------------------------------- */ /* CONNEXION AVEC L'API DE PYTHON */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : self = élément d'appel à consulter. * * args = arguments fournis pour l'opération. * * * * Description : Mène l'opération programmée. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_generic_work_process(PyObject *self, PyObject *args) { PyObject *result; /* Bilan à retourner */ GGenericWork *work; /* Version native */ #define GENERIC_WORK_PROCESS_METHOD PYTHON_METHOD_DEF \ ( \ process, "$self", \ METH_NOARGS, py_generic_work, \ "Process the work.\n" \ "\n" \ "The function does not return anything.\n" \ "\n" \ "The *work-completed* signal is emitted when the" \ " function terminates." \ ) work = G_GENERIC_WORK(pygobject_get(self)); g_generic_work_process(work); result = Py_None; Py_INCREF(result); return result; } /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *get_python_generic_work_type(void) { static PyMethodDef py_generic_work_methods[] = { GENERIC_WORK_RUN_WRAPPER, GENERIC_WORK_PROCESS_METHOD, { NULL } }; static PyGetSetDef py_generic_work_getseters[] = { { NULL } }; static PyTypeObject py_generic_work_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.glibext.GenericWork", .tp_basicsize = sizeof(PyGObject), .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, .tp_doc = GENERIC_WORK_DOC, .tp_methods = py_generic_work_methods, .tp_getset = py_generic_work_getseters, .tp_init = py_generic_work_init, .tp_new = py_generic_work_new, }; return &py_generic_work_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide.glibext.GenericWork'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool ensure_python_generic_work_is_registered(void) { PyTypeObject *type; /* Type Python 'GenericWork' */ PyObject *module; /* Module à recompléter */ PyObject *dict; /* Dictionnaire du module */ type = get_python_generic_work_type(); if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { module = get_access_to_python_module("pychrysalide.glibext"); dict = PyModule_GetDict(module); if (!register_class_for_pygobject(dict, G_TYPE_GENERIC_WORK, type)) return false; } return true; } /****************************************************************************** * * * Paramètres : arg = argument quelconque à tenter de convertir. * * dst = destination des valeurs récupérées en cas de succès. * * * * Description : Tente de convertir en travail générique. * * * * Retour : Bilan de l'opération, voire indications supplémentaires. * * * * Remarques : - * * * ******************************************************************************/ int convert_to_generic_work(PyObject *arg, void *dst) { int result; /* Bilan à retourner */ result = PyObject_IsInstance(arg, (PyObject *)get_python_generic_work_type()); switch (result) { case -1: /* L'exception est déjà fixée par Python */ result = 0; break; case 0: PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to generic work"); break; case 1: *((GGenericWork **)dst) = G_GENERIC_WORK(pygobject_get(arg)); break; default: assert(false); break; } return result; }