Salut a tous,
Nous voulons convertir une struct
en une class (en suivant l'example 25.2 donne dans "Python in a Nutshell, 2nd Edition" )
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 typedef struct { PyObject_HEAD int first, second; } intpair;
On obtient bien une class mais __dict__ montre une doc string vide et ne contient pas les membres "first" et "second"...
De plus, la fonction d'initialisation intpair_init (voir plus bas) n'as pas l'air d'etre executee du tout car le commande printf de debuggage ne s'affiche pas
1) Soit, on fait l'initialisation de maniere erronee.
2) soit on a un problem avec l'ordre des champs dans PyTypeObject
3) ou ???
merci d'avance pous vos suggestions
David, Juergen
On utilise la definition de PyTypeObject a partir de usr/lib/python2.5/doc/commontex/typestruct.h (voir plus bas):
et les fonctions intpair_dealloc,intpair_str, intpair_init.
avec intpair_members:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 static int intpair_init(PyObject *self , PyObject *args, PyObject *kwds) { static char* nams[] = {"first","second",NULL}; int first, second; printf("1 in init\n");fflush(stdout); if(!PyArg_ParseTupleAndKeywords(args, kwds, "ii", nams, &first, &second)) return -1; ((intpair*)self)->first = 101; ((intpair*)self)->second = 102; printf("in init\n"); return 0; }
l'initialisation est faite avec:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 static PyMemberDef intpair_members[] = { {"first", T_INT, offsetof(intpair, first), 0, "first item" }, {"second", T_INT, offsetof(intpair, second), 0, "second item" }, {NULL} };
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 void initintpair(void) { static PyMethodDef no_methods[] = { {NULL} }; PyObject* this_module = Py_InitModule("intpair", no_methods); PyType_Ready(&t_intpair); PyObject_SetAttrString(this_module, "intpair", (PyObject*)&t_intpair); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 static PyTypeObject t_intpair = { PyObject_HEAD_INIT(NULL) 0, "intpair.intpair", /* For printing, in format "<module>.<name>" */ sizeof(intpair), 0, /* For allocation */ /* Methods to implement standard operations */ intpair_dealloc, 0, 0, 0, 0, intpair_str, /* Method suites for standard classes */ 0, 0, 0, /* More standard operations (here for binary compatibility) */ 0, 0, 0, PyObject_GenericGetAttr, PyObject_GenericSetAttr, /* Functions to access object as input/output buffer */ 0, /* Flags to define presence of optional/expanded features */ Py_TPFLAGS_DEFAULT, "two ints (first,second)", /* Documentation string */ /* Assigned meaning in release 2.0 */ /* call function for all accessible objects */ 0, /* delete references to contained objects */ 0, /* Assigned meaning in release 2.1 */ /* rich comparisons */ 0, /* weak reference enabler */ 0, /* Added in release 2.2 */ /* Iterators */ 0, 0, /* Attribute descriptor and subclassing stuff */ 0, intpair_members, 0, 0, 0, 0, 0, 0, intpair_init, PyType_GenericAlloc, PyType_GenericNew , _PyObject_Del, /* Low-level free-memory routine */ 0, 0, 0, 0, 0, 0 };
Partager