IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Interfaçage autre langage Python Discussion :

[SWIG] Module I/O binaire C++ pour python


Sujet :

Interfaçage autre langage Python

  1. #1
    Candidat au Club
    Homme Profil pro
    Doctorant
    Inscrit en
    Avril 2011
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Doctorant

    Informations forums :
    Inscription : Avril 2011
    Messages : 3
    Points : 2
    Points
    2
    Par défaut [SWIG] Module I/O binaire C++ pour python
    Bonjour à tous,

    Voici ma situation (je la détaille au cas où quelqu'un aurait une solution plus simple pour ma problématique ): pour concevoir un outil de post-traitement en python (sous linux), j'aimerais utiliser les librairies d'import/export de fichiers binaires fourni par le logiciel de post-traitement Tecplot. Ces librairies sont fournies à travers leurs sources C++ ainsi qu'un makefile qui permet de générer une librairie statique par défaut contenant alors un set de fonctions à appeler pour obtenir différents fichiers binaires.

    Ayant déjà pratiqué l'interfaçage python/fortran, j'avais au début créé un script python appelant une subroutine fortran utilisant la fameuse librairie statique. Ce n'était pas très propre mais ça marchait bien ; en revanche, ce n'était absolument pas flexible. Je me suis donc dit que si le compliqué marchait, le plus simple le devrait aussi, à savoir appeler directement les fonctions dans le code python sans passer par le fortran.

    J'ai donc mis le nez dans SWIG, j'ai (un peu) ramé, et j'ai finalement réussi à créer une librairie dynamique que python pouvait m'importer sans erreur. M'importer seulement, car à l'appel de la première fonction, j'ai eu un message d'erreur semblant m'indiquer qu'il n'avait pas été possible de convertir un int python en int * requis par la fonction :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    >>> tecio.tecini112("TITLE", "x y z", "Tecplot_file.out", ".", 0, 1, 1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: in method 'tecini112', argument 5 of type 'int *'
    La déclaration de la fonction en question est la suivante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    LIBFUNCTION int LIBCALL TECINI112(char     *Title,
                                           char     *Variables,
                                           char     *FName,
                                           char     *ScratchDir,
                                           int *FileType,
                                           int *Debug,
                                           int *VIsDouble);
    Elle est tirée de mon fichier d'interface copié ci-dessous, qui a le format suivant (il est simple dans la forme, mais j'ai vu qu'il y avait d'autres mots clés qu'on pouvait passer à SWIG, c'est peut être là ma solution...) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    %module [nom du module]
     
    %{
    #include [header du fichier cpp contenant certaines fonctions qui m'intéressent]
    %}
     
    [déclarations des fonctions qui m'interessent, copiées du header]
    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    // tecio interface
     
    %module tecio
     
    %{
    #include "TECXXX.h"
    %}
     
     
    #if !defined TECXXX_H_
    #define TECXXX_H_
     
    #if !defined CRAY
    #  define TECFOREIGN112 tecforeign112
    #  define TECINI112     tecini112
    #  define TECZNE112     teczne112
    #  define TECDAT112     tecdat112
    #  define TECNOD112     tecnod112
    #  define TECGEO112     tecgeo112
    #  define TECTXT112     tectxt112
    #  define TECLAB112     teclab112
    #  define TECFIL112     tecfil112
    #  define TECEND112     tecend112
    #  define TECUSR112     tecusr112
    #  define TECAUXSTR112  tecauxstr112
    #  define TECZAUXSTR112 teczauxstr112
    #  define TECVAUXSTR112 tecvauxstr112
    #  define TECFACE112    tecface112
    #  define TECPOLY112    tecpoly112
     
     
    #if defined TECPLOTKERNEL
    /* CORE SOURCE CODE REMOVED */
    #else
    #define INTEGER2  short
    #endif
     
    #if defined _WIN32
    #if !defined MSWIN
    #define MSWIN /* MSWIN */
    #endif
    #endif /* _WIN32 */
     
    #if !defined (EXTERNC)
    # if defined (__cplusplus)
    #  define EXTERNC extern "C"
    # else
    #  define EXTERNC
    # endif /* __cplusplus */
    #endif /* EXTERN_C */
     
    #if !defined (STDCALL)
    # if defined MSWIN
    #  define STDCALL __stdcall
    # else /* !MSWIN */
    #  define STDCALL
    # endif /* MSWIN */
    #endif /* STDCALL */
     
    #if !defined (DLLEXPORT)
    # if defined (MSWIN)
    #  define DLLEXPORT _declspec (dllexport)
    # else
    #  define DLLEXPORT
    # endif /* MSWIN */
    #endif /* DLLEXPORT */
     
    #if !defined (DLLIMPORT)
    # if defined (MSWIN)
    #  define DLLIMPORT _declspec (dllimport)
    # else
    #  define DLLIMPORT
    # endif /* MSWIN */
    #endif /* DLLIMPORT */
     
     
    #if defined (TECPLOTKERNEL)
    /* CORE SOURCE CODE REMOVED */
    #else /* !TECPLOTKERNAL && !MAKEARCHIVE */
    # define LIBCALL STDCALL
    # define LIBFUNCTION EXTERNC DLLIMPORT
    #endif
     
    /*
     *  V11.3 tecio functions
     */
     
     
    LIBFUNCTION int LIBCALL TECINI112(char     *Title,
                                           char     *Variables,
                                           char     *FName,
                                           char     *ScratchDir,
                                           int *FileType,
                                           int *Debug,
                                           int *VIsDouble);
     
    LIBFUNCTION int LIBCALL TECZNE112(char     *ZoneTitle,
                                           int *ZoneType,
                                           int *IMxOrNumPts,
                                           int *JMxOrNumElements,
                                           int *KMxOrNumFaces,
                                           int *ICellMx,
                                           int *JCellMx,
                                           int *KCellMx,
                                           double   *SolutionTime,
                                           int *StrandID,
                                           int *ParentZone,
                                           int *IsBlock,
                                           int *NumFaceConnections,
                                           int *FaceNeighborMode,
                                           int *TotalNumFaceNodes,
                                           int *NumConnectedBoundaryFaces,
                                           int *TotalNumBoundaryConnections,
                                           int *PassiveVarList,
                                           int *ValueLocation,
                                           int *ShareVarFromZone,
                                           int *ShareConnectivityFromZone);
     
    LIBFUNCTION int LIBCALL TECDAT112(int  *N,
                                           void      *FieldData,
                                           int  *IsDouble);
     
    LIBFUNCTION int LIBCALL TECNOD112(int *NData);
     
    LIBFUNCTION int LIBCALL TECEND112(void);
     
    LIBFUNCTION int LIBCALL TECLAB112(char *S);
     
    LIBFUNCTION int LIBCALL TECUSR112(char *S);
     
    LIBFUNCTION int LIBCALL TECGEO112(double    *XPos,
                                           double    *YPos,
                                           double    *ZPos,
                                           int  *PosCoordMode,
                                           int  *AttachToZone,
                                           int  *Zone,
                                           int  *Color,
                                           int  *FillColor,
                                           int  *IsFilled,
                                           int  *GeomType,
                                           int  *LinePattern,
                                           double    *PatternLength,
                                           double    *LineThickness,
                                           int  *NumEllipsePts,
                                           int  *ArrowheadStyle,
                                           int  *ArrowheadAttachment,
                                           double    *ArrowheadSize,
                                           double    *ArrowheadAngle,
                                           int  *Scope,
                                           int  *Clipping,
                                           int  *NumSegments,
                                           int  *NumSegPts,
                                           float     *XGeomData,
                                           float     *YGeomData,
                                           float     *ZGeomData,
                                           char      *mfc);
     
    LIBFUNCTION int LIBCALL TECTXT112(double    *XOrThetaPos,
                                           double    *YOrRPos,
                                           double    *ZOrUnusedPos,
                                           int  *PosCoordMode,
                                           int  *AttachToZone,
                                           int  *Zone,
                                           int  *BFont,
                                           int  *FontHeightUnits,
                                           double    *FontHeight,
                                           int  *BoxType,
                                           double    *BoxMargin,
                                           double    *BoxLineThickness,
                                           int  *BoxColor,
                                           int  *BoxFillColor,
                                           double    *Angle,
                                           int  *Anchor,
                                           double    *LineSpacing,
                                           int  *TextColor,
                                           int  *Scope,
                                           int  *Clipping,
                                           char      *String,
                                           char      *mfc);
     
    LIBFUNCTION int LIBCALL TECFIL112(int *F);
     
    LIBFUNCTION int LIBCALL TECAUXSTR112(char *Name,
                                              char *Value);
     
    LIBFUNCTION int LIBCALL TECZAUXSTR112(char *Name,
                                               char *Value);
     
    LIBFUNCTION int LIBCALL TECVAUXSTR112(int *Var,
                                               char     *Name,
                                               char     *Value);
     
    LIBFUNCTION int LIBCALL TECFACE112(int *FaceConnections);
     
    LIBFUNCTION int LIBCALL TECPOLY112(int *FaceNodeCounts,
                                            int *FaceNodes,
                                            int *FaceLeftElems,
                                            int *FaceRightElems,
                                            int *FaceBndryConnectionCounts,
                                            int *FaceBndryConnectionElems,
                                            int *FaceBndryConnectionZones);
     
     
    #endif /* TECXXX_H_ */
    La démarche que j'ai utilisé est la suivante (les flag associés à g++ sont les flags utilisés par le makefile original) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    swig -c++ -python -o tecio_wrap.cpp tecio.i
    g++ -fPIC -DLINUX -DLINUX64 -DUSEENUM -DTHREED -c -I/usr/include/python2.6/  tecio_wrap.cpp
     
    [compilation des sources de la librairie]
    ...
    g++ -fPIC -DLINUX -DLINUX64  -DUSEENUM -DTHREED  -c dataio4.cpp
    g++ -fPIC -DLINUX -DLINUX64  -DUSEENUM -DTHREED  -c dataio.cpp
    g++ -fPIC -DLINUX -DLINUX64  -DUSEENUM -DTHREED  -c dataset0.cpp
    ...
    [fin des sources]
     
    g++ -shared *.o -o _tecio.so
    Si ça vous parle et que vous avez une solution, je suis preneur !
    Merci !

  2. #2
    Candidat au Club
    Homme Profil pro
    Doctorant
    Inscrit en
    Avril 2011
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Doctorant

    Informations forums :
    Inscription : Avril 2011
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    (J'avais zappé mais il y a un sujet avec le même type d'erreur dans les discussions récentes.. )

Discussions similaires

  1. voir les modules installés pour python?
    Par ELODIE2 dans le forum Déploiement/Installation
    Réponses: 6
    Dernier message: 27/08/2014, 19h35
  2. Module LaTeX pour Python
    Par soucou dans le forum Calcul scientifique
    Réponses: 1
    Dernier message: 27/01/2013, 15h09
  3. midi module pour python
    Par pfeuh dans le forum Bibliothèques tierces
    Réponses: 3
    Dernier message: 22/12/2011, 10h12
  4. [C++] Ecrire un module pour python
    Par anthyme dans le forum Interfaçage autre langage
    Réponses: 3
    Dernier message: 21/02/2007, 23h14
  5. module .so pour python... ?!
    Par totoetlititi dans le forum Langages de programmation
    Réponses: 2
    Dernier message: 09/03/2004, 14h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo