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

Python Discussion :

python callback et librairie C


Sujet :

Python

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Décembre 2011
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2011
    Messages : 7
    Par défaut python callback et librairie C
    Bonjour,
    Je souhaite utiliser une librairie c :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    ....
    typedef struct __pmlist_t PM_LIST;
    typedef struct __pmdb_t PM_DB;
    ..
    int pacman_parse_config(char *file, pacman_cb_db_register callback, const char *this_section);
    /* Database registration callback */
    typedef void (*pacman_cb_db_register)(const char *, PM_DB *);
    ....
    J'ai donc écris :
    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
     
    #!/usr/bin/env python
     
    import os, tempfile, shutil, sys
    from ctypes import *
    import ctypes
     
    #GLOBAL
    pacman=cdll.LoadLibrary("libpacman.so")
    CFG_FILE = '/etc/pacman-g2.conf'
     
    #for print debug messages
    debug=1
     
     
    #some class for pacman-g2
    class PM_LIST(Structure):
      pass
    PM_LIST._fields_ = [
            ("data", ctypes.c_void_p),
            ("prev", POINTER(PM_LIST)),
            ("next", POINTER(PM_LIST)),
            ("last", POINTER(PM_LIST))]
    class PM_DB(Structure):
      pass
    PM_DB._fields_ = [
            ("path", ctypes.c_char),
            ("treename", ctypes.c_char),
            ("handle", ctypes.c_void_p),
            ("pkgcache", POINTER(PM_LIST)),
            ("grpcache", POINTER(PM_LIST)),
            ("handle", POINTER(PM_LIST)),
            ("servers", ctypes.c_char)]
     
    def _db_cb (section,db):
      print_debug(db)
      return
     
    def pacman_init():
      pacman.pacman_release()
      if pacman.pacman_initialize("/") == -1:
        print "Can't initialise pacman-g2"
        sys.exit(0) 
     
    def pacman_init_database():
      print_debug("init database")
      pacman_cb_db_register = CFUNCTYPE(POINTER(ctypes.c_void_p), POINTER(ctypes.c_char_p), PM_DB)
      print_debug("pacman_parse_config")
      pacman.pacman_parse_config.argtypes = [POINTER(ctypes.c_char_p),POINTER(pacman_cb_db_register),POINTER(ctypes.c_char_p)]
      pacman.pacman_parse_config(CFG_FILE,pacman_cb_db_register(_db_cb),'')
     
    def main():
      pacman_init()
      pacman_init_database()
     
     
    def help():
      print "help :"
      print "--search Package"
      sys.exit(0)
     
     
    def print_debug(textConsole):
      if debug <> 1:
        return
      print "DEBUG : "+textConsole
     
    def _db_cb(section , db):
      print db
     
    #start main program
    main()
    Mais quand je lance le script :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
     DEBUG : init database
    DEBUG : pacman_parse_config
    Traceback (most recent call last):
      File "py-pacman.py", line 73, in <module>
        main()
      File "py-pacman.py", line 55, in main
        pacman_init_database()
      File "py-pacman.py", line 51, in pacman_init_database
        pacman.pacman_parse_config(CFG_FILE,pacman_cb_db_register(_db_cb),'')
    TypeError: invalid result type for callback function
    Je me doute que j'ai du me tromper mais je vois pas si quelqu'un à une idée je suis preneur

  2. #2
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Décembre 2011
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2011
    Messages : 7
    Par défaut
    Bon, j'ai un peu avancé j'avais des problèmes de typage :
    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
     
    #!/usr/bin/env python
     
    import os, tempfile, shutil, sys
    from ctypes import *
    import ctypes
     
    #GLOBAL
    pacman=cdll.LoadLibrary("libpacman.so")
    CFG_FILE = "/etc/pacman-g2.conf"
     
    #for print debug messages
    debug=1
     
     
    #some class for pacman-g2
    class PM_LIST(Structure):
      pass
    PM_LIST._fields_ = [
            ("data", ctypes.c_void_p),
            ("prev", POINTER(PM_LIST)),
            ("next", POINTER(PM_LIST)),
            ("last", POINTER(PM_LIST))]     
    class PM_DB(Structure):
      pass
    PM_DB._fields_ = [
            ("path", ctypes.c_char_p),
            ("treename", ctypes.c_char * 255),
            ("handle", ctypes.c_void_p),
            ("pkgcache", POINTER(PM_LIST)),
            ("grpcache", POINTER(PM_LIST)),
            ("handle", POINTER(PM_LIST)),
            ("servers", ctypes.c_char),
            ("lastupdate", ctypes.c_char * 16)]
     
    def _db_cb (section,db):
      print_debug("passe")
      print_debug(db)
      return
     
    def pacman_init():
      pacman.pacman_release()
      if pacman.pacman_initialize("/") == -1:
        print "Can't initialise pacman-g2"
        sys.exit(0) 
     
    def pacman_init_database():
      print_debug("init database")
      pacman_cb_db_register = CFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p, POINTER(PM_DB))
      print_debug("pacman_parse_config")
      pacman.pacman_parse_config.argtypes = [ctypes.c_char_p,pacman_cb_db_register,ctypes.c_char_p]
      pacman.pacman_parse_config.restype = ctypes.c_int
      pacman.pacman_parse_config(CFG_FILE,pacman_cb_db_register(_db_cb),'')
      print_debug("pacman_parse_config fini")
     
    def main():
      pacman_init()
      pacman_init_database()
     
     
    def help():
      print "help :"
      print "--search Package"
      sys.exit(0)
     
     
    def print_debug(textConsole):
      if debug <> 1:
        return
      print "DEBUG : "+textConsole
     
    def _db_cb(section , db):
      print db
     
    #start main program
    main()
    Mais en sorti :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    DEBUG : init database
    DEBUG : pacman_parse_config
    <__main__.LP_PM_DB object at 0xb733ef5c>
    DEBUG : pacman_parse_config fini
    Voici la structure C de PM_DB :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    /* Database */
    typedef struct __pmdb_t {
    	char *path;
    	char treename[PATH_MAX];
    	void *handle;
    	pmlist_t *pkgcache;
    	pmlist_t *grpcache;
    	pmlist_t *servers;
    	char lastupdate[16];
    } pmdb_t;

  3. #3
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Décembre 2011
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2011
    Messages : 7
    Par défaut
    Resolu :
    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
     
    #!/usr/bin/env python
     
    import os, tempfile, shutil, sys
    from ctypes import *
    import ctypes
     
    #GLOBAL
    pacman=cdll.LoadLibrary("libpacman.so")
    CFG_FILE = "/etc/pacman-g2.conf"
     
    #for print debug messages
    debug=1
     
     
    #some class for pacman-g2
    class PM_LIST(Structure):
      pass
    PM_LIST._fields_ = [
            ("data", ctypes.c_void_p),
            ("prev", POINTER(PM_LIST)),
            ("next", POINTER(PM_LIST)),
            ("last", POINTER(PM_LIST))]     
    class PM_DB(Structure):
      pass
    PM_DB._fields_ = [
            ("path", ctypes.c_char_p),
            ("treename", ctypes.c_char * 255),
            ("handle", ctypes.c_void_p),
            ("pkgcache", POINTER(PM_LIST)),
            ("grpcache", POINTER(PM_LIST)),
            ("handle", POINTER(PM_LIST)),
            ("servers", ctypes.c_char),
            ("lastupdate", ctypes.c_char * 16)]
     
    def _db_cb (section,db):
      print_debug("passe")
      print_debug(section)
      return
     
    def pacman_init():
      print_debug("init database")
      pacman.pacman_release()
      if pacman.pacman_initialize("/") == -1:
        print "Can't initialise pacman-g2"
        sys.exit(0) 
     
    pacman_cb_db_register = CFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p, POINTER(PM_DB))
     
    def pacman_init_database():
      print_debug("pacman_parse_config")
      pacman.pacman_parse_config.argtypes = [ctypes.c_char_p,pacman_cb_db_register,ctypes.c_char_p]
      pacman.pacman_parse_config.restype = ctypes.c_int
      pacman.pacman_parse_config(CFG_FILE,pacman_cb_db_register(_db_cb),'')
     
    def main():
      pacman_init()
      pacman_init_database()
     
     
    def help():
      print "help :"
      print "--search Package"
      sys.exit(0)
     
    def print_debug(textConsole):
      if debug <> 1:
        return
      print "DEBUG : "+textConsole
     
    #start main program
    main()

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Blender][Python]importer une librairie
    Par straasha dans le forum Développement 2D, 3D et Jeux
    Réponses: 0
    Dernier message: 25/04/2013, 14h06
  2. Type de licence des librairies pour Python ?
    Par Invité dans le forum Contribuez
    Réponses: 6
    Dernier message: 09/07/2008, 19h13
  3. Path Python et librairies
    Par metalamania dans le forum Général Python
    Réponses: 2
    Dernier message: 16/04/2008, 01h11
  4. Utilisation de librairies .net c++ au sein de python
    Par petitmatin dans le forum Interfaçage autre langage
    Réponses: 3
    Dernier message: 15/02/2006, 09h05
  5. Librairie 7z Pour Python
    Par kedare dans le forum Général Python
    Réponses: 2
    Dernier message: 19/12/2005, 15h49

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