summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/common
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2019-02-07 23:42:02 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2019-02-07 23:42:02 (GMT)
commit32bef30025f5e3f513c2b4936c0573cc3b629961 (patch)
treed4ceb04d4a6357b772a8ea55f8035701c41c6dd0 /plugins/pychrysalide/common
parentdbdd42585e8aa3333010bd57f0cd1129ac3c1d2f (diff)
Rewritten some Python instance allocation functions.
Diffstat (limited to 'plugins/pychrysalide/common')
-rw-r--r--plugins/pychrysalide/common/bits.c81
1 files changed, 63 insertions, 18 deletions
diff --git a/plugins/pychrysalide/common/bits.c b/plugins/pychrysalide/common/bits.c
index 7a6454d..c3bacd4 100644
--- a/plugins/pychrysalide/common/bits.c
+++ b/plugins/pychrysalide/common/bits.c
@@ -41,8 +41,11 @@ typedef struct _py_bitfield_t
-/* Crée un nouvel objet Python de type 'bitfield_t'. */
-static PyObject *py_bitfield_new(PyTypeObject *, PyObject *, PyObject *);
+/* Libère de la mémoire un objet Python de type 'bitfield_t'. */
+static void py_bitfield_dealloc(py_bitfield_t *);
+
+/* Initialise un objet Python de type 'bitfield_t'. */
+static int py_bitfield_init(py_bitfield_t *, PyObject *, PyObject *);
/* Crée une copie d'un champ de bits classique. */
static PyObject *py_bitfield_dup(PyObject *, PyObject *);
@@ -93,33 +96,58 @@ static PyObject *py_bitfield_popcount(PyObject *, void *);
/******************************************************************************
* *
-* Paramètres : type = type de l'objet à instancier. *
-* args = arguments fournis à l'appel. *
-* kwds = arguments de type key=val fournis. *
+* Paramètres : self = champ de bits à supprimer. *
+* *
+* Description : Libère de la mémoire un objet Python de type 'bitfield_t'. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void py_bitfield_dealloc(py_bitfield_t *self)
+{
+ delete_bit_field(self->native);
+
+ Py_TYPE(self)->tp_free((PyObject *)self);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = instance d'objet à initialiser. *
+* args = arguments passés pour l'appel. *
+* kwds = mots clefs éventuellement fournis en complément. *
* *
-* Description : Crée un nouvel objet Python de type 'bitfield_t'. *
+* Description : Initialise un objet Python de type 'bitfield_t'. *
* *
-* Retour : Instance Python mise en place. *
+* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
-static PyObject *py_bitfield_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+static int py_bitfield_init(py_bitfield_t *self, PyObject *args, PyObject *kwds)
{
- py_bitfield_t *result; /* Instance à retourner */
+ int result; /* Bilan à retourner */
unsigned long length; /* Taille du champ à créer */
int state; /* Initialisation par défaut */
int ret; /* Bilan de lecture des args. */
+ result = -1;
+
ret = PyArg_ParseTuple(args, "kp", &length, &state);
- if (!ret) return NULL;
+ if (ret == 0) goto exit;
+
+ self->native = create_bit_field(length, state);
- result = (py_bitfield_t *)type->tp_alloc(type, 0);
+ result = 0;
- result->native = create_bit_field(length, state);
+ exit:
- return (PyObject *)result;
+ return result;
}
@@ -726,6 +754,8 @@ PyTypeObject *get_python_bitfield_type(void)
.tp_name = "pychrysalide.common.bitfield",
.tp_basicsize = sizeof(py_bitfield_t),
+ .tp_dealloc = (destructor)py_bitfield_dealloc,
+
.tp_as_number = &py_bitfield_nb_proto,
.tp_as_sequence = &py_bitfield_sequence_proto,
@@ -737,7 +767,9 @@ PyTypeObject *get_python_bitfield_type(void)
.tp_methods = py_bitfield_methods,
.tp_getset = py_bitfield_getseters,
- .tp_new = py_bitfield_new
+
+ .tp_init = (initproc)py_bitfield_init,
+ .tp_new = PyType_GenericNew,
};
@@ -796,15 +828,28 @@ bool ensure_python_bitfield_is_registered(void)
PyObject *build_from_internal_bitfield(const bitfield_t *field)
{
- py_bitfield_t *result; /* Instance à retourner */
+ PyObject *result; /* Instance à retourner */
PyTypeObject *type; /* Type à instancier */
+ PyObject *args; /* Liste des arguments d'appel */
+ py_bitfield_t *bf_obj; /* Objet précis instancié */
type = get_python_bitfield_type();
- result = (py_bitfield_t *)type->tp_alloc(type, 0);
+ /**
+ * Le format "p" n'existe pas pour Py_BuildValue().
+ */
+ args = Py_BuildValue("kk", 0, 0);
+
+ result = PyObject_CallObject((PyObject *)type, args);
+
+ Py_DECREF(args);
- result->native = dup_bit_field(field);
+ bf_obj = (py_bitfield_t *)result;
- return (PyObject *)result;
+ delete_bit_field(bf_obj->native);
+
+ bf_obj->native = dup_bit_field(field);
+
+ return result;
}