Ctypes

Un article de Haypo.

(Différences entre les versions)
Version du 19 février 2008 à 22:42 (modifier)
Haypo (Discuter | Contributions)
(Hello World!)
← Différence précédente
Version du 19 février 2008 à 22:43 (modifier) (défaire)
Haypo (Discuter | Contributions)
(Hello World!)
Différence suivante →
Ligne 6 : Ligne 6 :
== Hello World! == == Hello World! ==
-Appel simple à printf :+Appel simple à printf de la libc :
$ python $ python
>>> from ctypes import cdll >>> from ctypes import cdll
Ligne 14 : Ligne 14 :
13 13
-Définition du prototype (arguments et type de retour) :+Définition du prototype (arguments et type de retour), exemple avec sqrt() de la bibliothèque mathématique :
$ python $ python
>>> from ctypes import cdll, c_double >>> from ctypes import cdll, c_double
>>> libm = cdll.LoadLibrary('libm.so') >>> libm = cdll.LoadLibrary('libm.so')
>>> sqrt = libm.sqrt >>> sqrt = libm.sqrt
- >>> sqrt.argstype = (c_double,)+ >>> sqrt.'''argstype''' = (c_double,)
- >>> sqrt.restype = c_double+ >>> sqrt.'''restype''' = c_double
>>> sqrt(c_double(10)) >>> sqrt(c_double(10))

Version du 19 février 2008 à 22:43

Retour à la page précédente Retour aux langages de programmation

ctypes est une bibliothèque Python permettant d'accéder aux fonctions et symboles d'une bibliothèque externe (en particulier, codée en C).

Sommaire

Hello World!

Appel simple à printf de la libc :

$ python
>>> from ctypes import cdll
>>> libc=cdll.LoadLibrary('libc.so.6')
>>> libc.printf("Hello World!\n")
Hello World!
13

Définition du prototype (arguments et type de retour), exemple avec sqrt() de la bibliothèque mathématique :

$ python
>>> from ctypes import cdll, c_double
>>> libm = cdll.LoadLibrary('libm.so')
>>> sqrt = libm.sqrt
>>> sqrt.argstype = (c_double,)
>>> sqrt.restype = c_double
>>> sqrt(c_double(10))

Note : bug de ctypes 1.0, il faut quand même faire une conversion explicite pour les arguments c_double.

Pour définir une structure

from ctypes import Structure, Union

class user_regs_struct(Structure):
   _fields_ = (
      ("ebx", c_ulong),
      ("ecx", c_ulong),
      ("edx", c_ulong),
      ("esi", c_ulong),
      ...
   )

_sifields_t = (...)
class siginfo(Structure):
   _fields_ = (
        ("si_signo", c_int),
        ("si_errno", c_int),
        ("si_code", c_int),
        ("_sifields", _sifields_t)
   )
   _anonymous_ = ("_sifields",)

Tableau d'octet (char*)

create_string_buffer(str) crée un tableau d'octets pouvant contenir des octets nuls :

from ctypes import create_string_buffer
python_string = "string with \0 byte"
c_string = create_string_buffer(python_string)
assert c_string.value == 'string with '
assert c_string.raw == python_string

create_string_buffer(int) crée un tampon de N octet pouvant contenir de octets nuls :

>>> from ctypes import create_string_buffer
>>> buffer = create_string_buffer(5)
>>> buffer.raw
'\x00\x00\x00\x00\x00'

c_char_p tronque au premier octet nul :

from ctypes import create_string_buffer
python_string = "string with \0 byte"
c_string = c_char_p(python_string)
assert c_string.value == 'string with '

Modules

Outils connexes :

Articles connexes