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 :

Problèmes pour utiliser Python.h


Sujet :

Interfaçage autre langage Python

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 35
    Points : 24
    Points
    24
    Par défaut Problèmes pour utiliser Python.h
    Bonjour,

    J'ai découvert swig et numpy récemment, mais j'ai quelques problèmes
    pour utiliser ces outils.
    Pour essayer de comprendre ce qui se passe je fais des tests.
    En voilà un qui me pose un probleme.

    Je peux compiler et exécuter les quelques lignes suivantes:

    """
    Code c : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #include <iostream>
    #include <Python.h>
     
    int main()
    {
      PyObject* list=PyList_New(5);
      std::cout<<"typeid list: "<<typeid(list).name()<<"\n";
      std::cout<<"list check: "<<PyList_Check(list)<<"\n";
      std::cout<<"list size: "<<PyList_Size(list)<<"\n";
      std::cout<<"fin normale\n";
    }
    """"

    Si maintenant je rajoute la ligne suivante:
    """
    Code c : Sélectionner tout - Visualiser dans une fenêtre à part
    PyObject* pentier=PyInt_FromLong(long(10));
    """

    la compilation passe, mais à l'exécution j'ai une
    erreur de segmentation. Je ne vois pas du tout ce qui
    se passe. Le débugeur d'Eclipse me dit ceci:

    """
    No symbol "new" in current context.
    [Thread debugging using libthread_db enabled]
    [New Thread -1212565280 (LWP 8711)]
    Stopped due to shared library event
    Current language: auto; currently c
    [Switching to Thread -1212565280 (LWP 8711)]
    Stopped due to shared library event
    Stopped due to shared library event
    Current language: auto; currently c++
    Single stepping until exit from function PyInt_FromLong,
    which has no line number information.
    Couldn't get registers: Aucun processus de ce type.
    """

    Je ne suis qu'un programmeur amateur et là ca ma dépasse un peu.
    Si quelqu'un pouvait avoir la gentillesse de m'orienter ?
    Merci infiniment !!!
    Michel

  2. #2
    Rédacteur

    Avatar de Matthieu Brucher
    Profil pro
    Développeur HPC
    Inscrit en
    Juillet 2005
    Messages
    9 810
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur HPC
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2005
    Messages : 9 810
    Points : 20 970
    Points
    20 970
    Par défaut
    Lance gdb sur le processus python :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    gdb python
    run le_module.py

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 35
    Points : 24
    Points
    24
    Par défaut
    Bonjour,

    Je voulais faire un petit exercice qui consistait à faire un typemap pour transformer un array en vector mais ca ne veut pas passer.
    J'ai l'impression de n'avoir rien compris ....

    Example.h:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    #include <iostream>
    #include <vector>
     
    void globale_print(std::vector<int> vint)
    {
      std::cout<<"\n\tglobale_print vecteur: ";
      for (size_t i=0; i<vint.size(); i++)
      {
        std::cout<<vint[i]<<" - ";
      }
      std::cout<<"\n";
    }
    Example.i:
    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
     
    %module example
     
    %{
    #include "example.h"
    #include "arrayobject.h"
    %}
     
    /*
    %typemap(python,in) std::vector<int>
    {
      std::cout<<"\ttypeid $input: "<<typeid($input).name()<<"\n";
      std::cout<<"\tPyList_Check($input): "<<PyList_Check($input)<<"\n";
     
      int size=PyList_Size($input);
      std::cout<<"\tlist $input size: "<<size<<"\n";
      $1.resize(size);
      for (int i=0; i<size; i++)
      {
        $1[i]=PyInt_AsLong(PyList_GetItem($input,i));
        std::cout<<"\t$1[i]: "<<$1[i]<<"\n";
      }
    };
    */
     
    %typemap(python,in) std::vector<int>
    {
      std::cout<<"\ttypeid $input: "<<typeid($input).name()<<"\n";
      std::cout<<"\t$input->ob_type: "<<$input->ob_type<<"\n";
     
      //std::cout<<"\tPyArray_Check($input): "<<PyArray_Check($input)<<"\n";
     
      PyArrayObject* aa=(PyArrayObject *) $input;
      $1.resize(aa->dimensions[0]);
      for (unsigned int i=0; i<aa->dimensions[0]; i++)
      {
        $1[i]= *(int *) (aa->data + i* aa->strides[0] );
      }
    };
     
    %include "example.h"
    my_run.py:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    import example
    from Numeric import *
     
    #print "test avec list -> vector"
    #list1=[1,2,3,4,5]
    #print "\tlist1: ",list1
    #example.globale_print(list1)
     
    print "test avec array -> vector"
    array=array(range(5))
    print "\t",array
    print "\t",type(array)
    example.globale_print(array)
    Tout marche bien quand j'utilise les listes python (parties commentées)
    et aussi quand j'utilise les arrays python sauf pour la ligne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    //std::cout<<"\tPyArray_Check($input): "<<PyArray_Check($input)<<"\n";
    Si je tente de faire un PyArray_Check j'ai un "Erreur de Segmentation"

    Et gdb m'envoie quelque chose que je ne sais pas utiliser ...:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread -1212324160 (LWP 23435)]
    0xb7b1c53a in _wrap_globale_print () from /home/..../swig_TEST/_example.so
    Current language:  auto; currently c
    Matthieu je viens de découvrir que vous êtes un spécialiste !!
    et j'ai commandé votre livre.
    J'ai peut être mérité un commentaire ?!
    Amicalement
    Michel

  4. #4
    Rédacteur

    Avatar de Matthieu Brucher
    Profil pro
    Développeur HPC
    Inscrit en
    Juillet 2005
    Messages
    9 810
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur HPC
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2005
    Messages : 9 810
    Points : 20 970
    Points
    20 970
    Par défaut
    Un spécialiste, c'est vite dit

    Je te conseille de commencer par passer à Numpy, l'avantage c'est qu'il y a des typemaps déjà proposés pour les tableaux C, c'est un début pour ce que tu cherches.

    Par la suite, tapes "bt" avec gdb une fois que tu as le segfault pour voir exactement d'où il vient.

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 35
    Points : 24
    Points
    24
    Par défaut
    J'étais justement en train de tenter un bt, mais ca ne m'aide pas.
    J'utilise python 2.4.1, Numeric 23.7 et swig 1.3.24

    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
     
    (gdb) run my_run.py
    Starting program: /home/francois/Travail/salome_3.2.2/Python-2.4.1/bin/python my_run.py
    Reading symbols from shared object read from target memory...done.
    Loaded system supplied DSO at 0xffffe000
    [Thread debugging using libthread_db enabled]
    [New Thread -1211976000 (LWP 12430)]
    test avec array -> vector
            [0 1 2 3 4]
            <type 'array'>
            typeid obj0: P7_object
            obj0->ob_type: 0xb7b6aea0
     
    Program received signal SIGSEGV, Segmentation fault.
    [Switching to Thread -1211976000 (LWP 12430)]
    0xb7b7153a in _wrap_globale_print (args=0xb7bb3f8c) at example_wrap.cxx:1375
    1375            std::cout<<"\tPyArray_Check(obj0): "<<PyArray_Check(obj0)<<"\n";
    (gdb) bt
    #0  0xb7b7153a in _wrap_globale_print (args=0xb7bb3f8c) at example_wrap.cxx:1375
    #1  0xb7ee4826 in PyCFunction_Call (func=0xb7ba98ec, arg=0xb7bb3f8c, kw=0x0) at Objects/methodobject.c:108
    #2  0xb7f21863 in PyEval_EvalFrame (f=0x804c1ac) at Python/ceval.c:3547
    #3  0xb7f22261 in PyEval_EvalCodeEx (co=0xb7c23ce0, globals=0xb7c05824, locals=0xb7c05824, args=0x0, argcount=0, kws=0x0, kwcount=0, defs=0x0,
        defcount=0, closure=0x0) at Python/ceval.c:2730
    #4  0xb7f22382 in PyEval_EvalCode (co=0xb7c23ce0, globals=0xb7c05824, locals=0xb7c05824) at Python/ceval.c:484
    #5  0xb7f45804 in PyRun_FileExFlags (fp=0x804a008, filename=0xbfdb653f "my_run.py", start=257, globals=0xb7c05824, locals=0xb7c05824,
        closeit=1, flags=0xbfdb5b14) at Python/pythonrun.c:1265
    #6  0xb7f461d8 in PyRun_SimpleFileExFlags (fp=Variable "fp" is not available.
    ) at Python/pythonrun.c:860
    #7  0xb7f476b0 in PyRun_AnyFileExFlags (fp=0x804a008, filename=0xbfdb653f "my_run.py", closeit=1, flags=0xbfdb5b14) at Python/pythonrun.c:664
    #8  0xb7f4d2bd in Py_Main (argc=1, argv=0xbfdb5bd4) at Modules/main.c:484
    #9  0x08048644 in main (argc=2, argv=0xbfdb5bd4) at ./Modules/ccpython.cc:10
    Est ce qu'il y a quelque chose qui semble stupide dans ce petit test ?
    A priori ca devrait marcher non ?

  6. #6
    Rédacteur

    Avatar de Matthieu Brucher
    Profil pro
    Développeur HPC
    Inscrit en
    Juillet 2005
    Messages
    9 810
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur HPC
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2005
    Messages : 9 810
    Points : 20 970
    Points
    20 970
    Par défaut
    Maintenant regarde dans le fichier example_wrap.cxx à la ligne indiquée.

  7. #7
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 35
    Points : 24
    Points
    24
    Par défaut
    La ligne qui plante est:

    1375 std::cout<<"\tPyArray_Check(obj0): "<<PyArray_Check(obj0)<<"\n"

    C'est précisément celle que je commente dans l'interface pour qu'elle n'apparaisse
    pas dans le wrapper.

    Bon ...

  8. #8
    Rédacteur

    Avatar de Matthieu Brucher
    Profil pro
    Développeur HPC
    Inscrit en
    Juillet 2005
    Messages
    9 810
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur HPC
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2005
    Messages : 9 810
    Points : 20 970
    Points
    20 970
    Par défaut
    Vérifie dans le débuggeur la valeur de tes pointeurs. Il y a en a sans doute un qui est nul. Après, l'objectif est de savoir pourquoi et de corriger ça.

Discussions similaires

  1. [log4j] problème pour utilisation des extras
    Par shift dans le forum Weblogic
    Réponses: 0
    Dernier message: 06/12/2007, 09h58
  2. [Fileinfo] problème pour utiliser finfo
    Par mattstriker dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 02/11/2007, 14h09
  3. Réponses: 3
    Dernier message: 27/09/2007, 13h55
  4. Problème pour utiliser JWS
    Par yas2006 dans le forum JWS
    Réponses: 11
    Dernier message: 02/07/2007, 13h28
  5. Problème pour utiliser split avec "\"
    Par Nicolas_555 dans le forum Langage
    Réponses: 6
    Dernier message: 03/08/2006, 14h42

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