Précédent   Forum du club des développeurs et IT Pro > Autres langages > Python & Zope > Interfaçage autre langage
Interfaçage autre langage Forum d'entraide pour l'interfaçage avec d'autres langages (ctypes, api C, swig, ...)
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 03/05/2012, 13h34   #1
spartan811
Invité régulier
 
Homme
Étudiant
Inscription : avril 2012
Messages : 23
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : avril 2012
Messages : 23
Points : 6
Points : 6
Par défaut SWIG probleme pour lancer le script python dans mon code C++ et récupérer un objet

Bonjour,

grâce à swig, j'arrive à étendre une classe C++ en Python.Maintenant j'aimerais créer un objet en python et pouvoir le récupérer dans le code C++

créer l'objet et l'utiliser dans le script marche très bien, mais j'aimerais récupérer l'objet dans mon code C++

voici mon main pour exécuter le script :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
int main()
{
    //Test existance du fichier
    ifstream fichier("Perso.py");
 
    if (fichier.fail()) {
        cout << "Fichier de script introuvable : " << "Perso.py" <<"\n";
        return 0;
    }
 
    // Ouvre le script python a executer
    FILE* pyFile = fopen("Perso.py", "r");
 
    Py_Initialize();
 
    // Execute le script
    string exec ( "execfile(r\"" );  // note r for raw Python string.
    exec += "Perso.py" ; exec += "\")" ;
    PyRun_SimpleString( (char*) exec.c_str() );
 
    Py_Finalize();
}
spartan811 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/05/2012, 15h11   #2
spartan811
Invité régulier
 
Homme
Étudiant
Inscription : avril 2012
Messages : 23
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : avril 2012
Messages : 23
Points : 6
Points : 6
Bon j'ai un peu avancé, j'ai modifié mon main, il appel maintenant une fonction de mon script.

J'utilise ma fonction pour envoyer des arguments à Python, le souci est que les arguments que j'arrive a envoyer ne sont que de type int ou string, et j'aimerai envoyer des objets, quelqu'un sait comment faire ?

voici mon main actuel :

Code :
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
 
     PyObject *pName, *pModule, *pDict, *pFunc, *pValue;
     PyObject *pClass, *pInstance, *pArgs;
 
     // Initialize the Python Interpreter
     Py_Initialize();
 
     // Build the name object
     pName = PyString_FromString("Perso");
 
     if(pName == NULL) 
       cout<< "error" << endl;
 
     // Load the module object
     pModule = PyImport_Import(pName);
 
     if(pName == NULL) 
        cout<< "error" << endl;
 
     // pDict is a borrowed reference 
     pDict = PyModule_GetDict(pModule);
     if(pDict == NULL) 
         cout<< "error" << endl;
 
     // pFunc is also a borrowed reference 
     pFunc = PyDict_GetItemString(pDict, "test");
     if(pFunc == NULL) 
         cout<< "error fonction" << endl;
 
     if (PyCallable_Check(pFunc)) 
     {
         pArgs = PyTuple_New(1);                     // create list of arg
         pValue = PyInt_FromLong(123);           // a la place de 123 jaimerai transmettre une instance
         PyTuple_SetItem(pArgs, 0, pValue);          
        PyObject_CallObject(pFunc, pArgs);
     } else 
     {
         PyErr_Print();
     }
 
      // Clean up
     Py_DECREF(pModule);
     Py_DECREF(pName);
 
     Py_Finalize();

J'ai essayé d'utiliser Py_BuildValue pour caster mon objet en PyObject mais ça ne marche pas
spartan811 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/05/2012, 14h46   #3
Nanzilla
Membre actif
 
Homme Guillaume
Développeur informatique
Inscription : janvier 2012
Messages : 27
Détails du profil
Informations personnelles :
Nom : Homme Guillaume
Localisation : France

Informations professionnelles :
Activité : Développeur informatique
Secteur : Aéronautique - Marine - Espace - Armement

Informations forums :
Inscription : janvier 2012
Messages : 27
Points : 176
Points : 176
Il faut utiliser des pointeurs et il me semble déclarer la classe des deux côté (mais pas sûr).
http://www.swig.org/papers/PyTutoria...Tutorial98.pdf.
L'article est un peu vieux, mais il est bien expliqué je trouve (et je n'ai pas accès au site SWIG depuis le taf :/).
Nanzilla est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 11/05/2012, 16h23   #4
spartan811
Invité régulier
 
Homme
Étudiant
Inscription : avril 2012
Messages : 23
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : avril 2012
Messages : 23
Points : 6
Points : 6
Ok merci, je vais regarder ça
spartan811 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 13h52.


 
 
 
 
Partenaires

Hébergement Web