ctypes — Bibliothèque Python d'appels à des fonctions externes

Source code: Lib/ctypes


ctypes est une bibliothèque d'appel à des fonctions externes en python. Elle fournit des types de données compatibles avec le langage C et permet d'appeler des fonctions depuis des DLL ou des bibliothèques partagées, rendant ainsi possible l'interfaçage de ces bibliothèques avec du pur code Python.

Didacticiel de ctypes

Remarque : les exemples de code de ce didacticiel utilisent doctest pour s'assurer de leur propre bon fonctionnement. Vu que certains de ces exemples ont un comportement différent en Linux, Windows ou macOS, ils contiennent des directives doctest dans les commentaires.

Remarque : le type c_int du module apparaît dans certains de ces exemples. Sur les plates-formes où sizeof(long) == sizeof(int), ce type est un alias de c_long. Ne soyez donc pas surpris si c_long s'affiche là où vous vous attendiez à c_int — il s'agit bien du même type.

Accès aux fonctions des DLL chargées

Les fonctions sont alors des attributs des objets DLL :

>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>

Les DLL des systèmes win32 comme kernel32 et user32 exportent souvent une version ANSI et une version UNICODE d'une fonction. La version UNICODE est exportée avec un W à la fin, et la version ANSI avec un A. La fonction win32 GetModuleHandle, qui renvoie un gestionnaire de module à partir de son nom, a le prototype C suivant (c'est une macro qui décide d'exporter l'une ou l'autre à travers GetModuleHandle, selon qu'UNICODE est définie ou non) :

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);

windll n'en choisit pas une par magie, il faut accéder à la bonne en écrivant explicitement GetModuleHandleA ou GetModuleHandleW et en les appelant ensuite avec des objets octets ou avec des chaînes de caractères, respectivement.

Les DLL exportent parfois des fonctions dont les noms ne sont pas des identifiants Python valides, comme "??2@YAPAXI@Z". Dans ce cas, il faut utiliser getattr() pour accéder à la fonction :

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>

Sous Windows, certaines DLL exportent des fonctions à travers un indice plutôt qu'à travers un nom. On accède à une fonction en indiçant l'objet DLL avec son index :

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>

Appel de fonctions

You can call these functions like any other Python callable. This example uses the rand() function, which takes no arguments and returns a pseudo-random integer:

>>> print(libc.rand())  
1804289383

On Windows, you can call the GetModuleHandleA() function, which returns a win32 module handle (passing None as single argument to call it with a NULL pointer):

>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>

Une ValueError est levée quand on appelle une fonction stdcall avec la convention d'appel cdecl et vice-versa :

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>

Pour déterminer la convention d'appel, il faut consulter l'en-tête C ou la documentation de la fonction à appeler.

En Windows, ctypes tire profit de la gestion structurée des exceptions (structured exception handling) win32 pour empêcher les plantages dus à des interruptions, afin de préserver la protection globale (general protection faults) du système, lorsque des fonctions sont appelées avec un nombre incorrect d'arguments :

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: exception: access violation reading 0x00000020
>>>

Cependant, il y a suffisamment de façons de faire planter Python avec ctypes, donc il faut être prudent dans tous les cas. Le module faulthandler est pratique pour déboguer les plantages (p. ex. dus à des erreurs de segmentation produites par des appels erronés à la bibliothèque C).

None, integers, bytes objects and (unicode) strings are the only native Python objects that can directly be used as parameters in these function calls. None is passed as a C NULL pointer, bytes objects and strings are passed as pointer to the memory block that contains their data (char* or wchar_t*). Python integers are passed as the platform's default C int type, their value is masked to fit into the C type.

Avant de poursuivre sur l'appel de fonctions avec d'autres types de paramètres, apprenons-en un peu plus sur les types de données de ctypes.

Types de données de base

ctypes définit plusieurs types de donnée de base compatibles avec le C :

Types de ctypes

Type C

Type Python

c_bool

_Bool

bool (1)

c_char

char

objet octet (bytes) de 1 caractère

c_wchar

wchar_t

chaîne de caractères (string) de longueur 1

c_byte

char

int

c_ubyte

unsigned char

int

c_short

short

int

c_ushort

unsigned short

int

c_int

int

int

c_uint

unsigned int

int

c_long

long

int

c_ulong

unsigned long

int

c_longlong

__int64 or long long

int

c_ulonglong

unsigned __int64 or unsigned long long

int

c_size_t

size_t

int

c_ssize_t

ssize_t or Py_ssize_t

int

c_time_t

time_t

int

c_float

float

float

c_double

double

float

c_longdouble

long double

float

c_char_p

char* (NUL terminated)

objet octet (bytes) ou None

c_wchar_p

wchar_t* (NUL terminated)

chaîne de caractères (string) ou None

c_void_p

void*

int ou None

  1. Le constructeur accepte n'importe quel objet convertible en booléen.

Il est possible de créer chacun de ces types en les appelant avec une valeur d'initialisation du bon type et avec une valeur cohérente :

>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>

Ces types étant des mutables, leur valeur peut aussi être modifiée après coup :

>>> i = c_int(42)
>>> print(i)
c_long(42)
>>> print(i.value)
42
>>> i.value = -99
>>> print(i.value)
-99
>>>

Affecter une nouvelle valeur à une instance de type pointeur — c_char_p, c_wchar_p et c_void_p — change la zone mémoire sur laquelle elle pointe, et non le contenu de ce bloc mémoire (c'est logique parce que les objets octets sont immuables en Python) :

>>> s = "Hello, World"
>>> c_s = c_wchar_p(s)
>>> print(c_s)
c_wchar_p(139966785747344)
>>> print(c_s.value)
Hello World
>>> c_s.value = "Hi, there"
>>> print(c_s)              # the memory location has changed
c_wchar_p(139966783348904)
>>> print(c_s.value)
Hi, there
>>> print(s)                # first object is unchanged
Hello, World
>>>

Cependant, prenez garde à ne pas en passer à des fonctions qui prennent en paramètre des pointeurs sur de la mémoire modifiable. S'il vous faut de la mémoire modifiable, ctypes fournit la fonction create_string_buffer() qui en crée de plusieurs façons. L'attribut raw permet d'accéder à (ou de modifier) un bloc mémoire ; l'attribut value permet d'y accéder comme à une chaîne de caractères terminée par NUL :

>>> from ctypes import *
>>> p = create_string_buffer(3)            # create a 3 byte buffer, initialized to NUL bytes
>>> print(sizeof(p), repr(p.raw))
3 b'\x00\x00\x00'
>>> p = create_string_buffer(b"Hello")     # create a buffer containing a NUL terminated string
>>> print(sizeof(p), repr(p.raw))
6 b'Hello\x00'
>>> print(repr(p.value))
b'Hello'
>>> p = create_string_buffer(b"Hello", 10) # create a 10 byte buffer
>>> print(sizeof(p), repr(p.raw))
10 b'Hello\x00\x00\x00\x00\x00'
>>> p.value = b"Hi"
>>> print(sizeof(p), repr(p.raw))
10 b'Hi\x00lo\x00\x00\x00\x00\x00'
>>>

The create_string_buffer() function replaces the old c_buffer() function (which is still available as an alias). To create a mutable memory block containing unicode characters of the C type wchar_t, use the create_unicode_buffer() function.

Appel de fonctions, suite

printf utilise la vraie sortie standard, et non sys.stdout ; les exemples suivants ne fonctionnent donc que dans une invite de commande et non depuis IDLE or PythonWin :

>>> printf = libc.printf
>>> printf(b"Hello, %s\n", b"World!")
Hello, World!
14
>>> printf(b"Hello, %S\n", "World!")
Hello, World!
14
>>> printf(b"%d bottles of beer\n", 42)
42 bottles of beer
19
>>> printf(b"%f bottles of beer\n", 42.5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 2: TypeError: Don't know how to convert parameter 2
>>>

Comme mentionné plus haut, tous les types Python (les entiers, les chaînes de caractères et les objets octet exceptés) doivent être encapsulés dans leur type ctypes correspondant pour pouvoir être convertis dans le type C requis :

>>> printf(b"An int %d, a double %f\n", 1234, c_double(3.14))
An int 1234, a double 3.140000
31
>>>

Calling variadic functions

On a lot of platforms calling variadic functions through ctypes is exactly the same as calling functions with a fixed number of parameters. On some platforms, and in particular ARM64 for Apple Platforms, the calling convention for variadic functions is different than that for regular functions.

On those platforms it is required to specify the argtypes attribute for the regular, non-variadic, function arguments:

libc.printf.argtypes = [ctypes.c_char_p]

Because specifying the attribute does not inhibit portability it is advised to always specify argtypes for all variadic functions.

Appel de fonctions avec des types de données personnalisés

You can also customize ctypes argument conversion to allow instances of your own classes be used as function arguments. ctypes looks for an _as_parameter_ attribute and uses this as the function argument. The attribute must be an integer, string, bytes, a ctypes instance, or an object with an _as_parameter_ attribute:

>>> class Bottles:
...     def __init__(self, number):
...         self._as_parameter_ = number
...
>>> bottles = Bottles(42)
>>> printf(b"%d bottles of beer\n", bottles)
42 bottles of beer
19
>>>

If you don't want to store the instance's data in the _as_parameter_ instance variable, you could define a property which makes the attribute available on request.

Définition du type des arguments nécessaires (prototypes de fonction)

It is possible to specify the required argument types of functions exported from DLLs by setting the argtypes attribute.

argtypes must be a sequence of C data types (the printf() function is probably not a good example here, because it takes a variable number and different types of parameters depending on the format string, on the other hand this is quite handy to experiment with this feature):

>>> printf.argtypes = [c_char_p, c_char_p, c_int, c_double]
>>> printf(b"String '%s', Int %d, Double %f\n", b"Hi", 10, 2.2)
String 'Hi', Int 10, Double 2.200000
37
>>>

Définir un format empêche de passer des arguments de type incompatible (comme le fait le prototype d'une fonction C) et tente de convertir les arguments en des types valides :

>>> printf(b"%d %d %d", 1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 2: TypeError: 'int' object cannot be interpreted as ctypes.c_char_p
>>> printf(b"%s %d %f\n", b"X", 2, 3)
X 2 3.000000
13
>>>

If you have defined your own classes which you pass to function calls, you have to implement a from_param() class method for them to be able to use them in the argtypes sequence. The from_param() class method receives the Python object passed to the function call, it should do a typecheck or whatever is needed to make sure this object is acceptable, and then return the object itself, its _as_parameter_ attribute, or whatever you want to pass as the C function argument in this case. Again, the result should be an integer, string, bytes, a ctypes instance, or an object with an _as_parameter_ attribute.

Types de sortie

By default functions are assumed to return the C int type. Other return types can be specified by setting the restype attribute of the function object.

The C prototype of time() is time_t time(time_t *). Because time_t might be of a different type than the default return type int, you should specify the restype attribute:

>>> libc.time.restype = c_time_t

The argument types can be specified using argtypes:

>>> libc.time.argtypes = (POINTER(c_time_t),)

To call the function with a NULL pointer as first argument, use None:

>>> print(libc.time(None))  
1150640792

Here is a more advanced example, it uses the strchr() function, which expects a string pointer and a char, and returns a pointer to a string:

>>> strchr = libc.strchr
>>> strchr(b"abcdef", ord("d"))  
8059983
>>> strchr.restype = c_char_p    # c_char_p is a pointer to a string
>>> strchr(b"abcdef", ord("d"))
b'def'
>>> print(strchr(b"abcdef", ord("x")))
None
>>>

If you want to avoid the ord("x") calls above, you can set the argtypes attribute, and the second argument will be converted from a single character Python bytes object into a C char:

>>> strchr.restype = c_char_p
>>> strchr.argtypes = [c_char_p, c_char]
>>> strchr(b"abcdef", b"d")
b'def'
>>> strchr(b"abcdef", b"def")
Traceback (most recent call last):
ctypes.ArgumentError: argument 2: TypeError: one character bytes, bytearray or integer expected
>>> print(strchr(b"abcdef", b"x"))
None
>>> strchr(b"abcdef", b"d")
b'def'
>>>

You can also use a callable Python object (a function or a class for example) as the restype attribute, if the foreign function returns an integer. The callable will be called with the integer the C function returns, and the result of this call will be used as the result of your function call. This is useful to check for error return values and automatically raise an exception:

>>> GetModuleHandle = windll.kernel32.GetModuleHandleA  
>>> def ValidHandle(value):
...     if value == 0:
...         raise WinError()
...     return value
...
>>>
>>> GetModuleHandle.restype = ValidHandle  
>>> GetModuleHandle(None)  
486539264
>>> GetModuleHandle("something silly")  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in ValidHandle
OSError: [Errno 126] The specified module could not be found.
>>>

WinError appelle l'API Windows FormatMessage() pour obtenir une représentation de la chaîne de caractères qui correspond au code d'erreur, et renvoie une exception. WinError prend en paramètre — optionnel — le code d'erreur. Si celui-ci n'est pas passé, elle appelle GetLastError() pour le récupérer.

Please note that a much more powerful error checking mechanism is available through the errcheck attribute; see the reference manual for details.

Passage de pointeurs (passage de paramètres par référence)

Il arrive qu'une fonction C du code à interfacer requière un pointeur vers un certain type de donnée en paramètre, typiquement pour écrire à l'endroit correspondant ou si la donnée est trop grande pour pouvoir être passée par valeur. Ce mécanisme est appelé passage de paramètres par référence.

ctypes contient la fonction byref() qui permet de passer des paramètres par référence. La fonction pointer() a la même utilité, mais fait plus de travail car pointer() construit un véritable objet pointeur. Ainsi, si vous n'avez pas besoin de cet objet dans votre code Python, utiliser byref() est plus performant :

>>> i = c_int()
>>> f = c_float()
>>> s = create_string_buffer(b'\000' * 32)
>>> print(i.value, f.value, repr(s.value))
0 0.0 b''
>>> libc.sscanf(b"1 3.14 Hello", b"%d %f %s",
...             byref(i), byref(f), s)
3
>>> print(i.value, f.value, repr(s.value))
1 3.1400001049 b'Hello'
>>>

Structures et unions

Structures and unions must derive from the Structure and Union base classes which are defined in the ctypes module. Each subclass must define a _fields_ attribute. _fields_ must be a list of 2-tuples, containing a field name and a field type.

Le type de champ doit être un type ctypes comme c_int ou un type ctypes dérivé : structure, union, tableau ou pointeur.

Voici un exemple simple : une structure POINT qui contient deux entiers x et y et qui montre également comment instancier une structure avec le constructeur :

>>> from ctypes import *
>>> class POINT(Structure):
...     _fields_ = [("x", c_int),
...                 ("y", c_int)]
...
>>> point = POINT(10, 20)
>>> print(point.x, point.y)
10 20
>>> point = POINT(y=5)
>>> print(point.x, point.y)
0 5
>>> POINT(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: too many initializers
>>>

Il est bien entendu possible de créer des structures plus complexes. Une structure peut elle-même contenir d'autres structures en prenant une structure comme type de champ.

Voici une structure RECT qui contient deux POINTs upperleft et lowerright :

>>> class RECT(Structure):
...     _fields_ = [("upperleft", POINT),
...                 ("lowerright", POINT)]
...
>>> rc = RECT(point)
>>> print(rc.upperleft.x, rc.upperleft.y)
0 5
>>> print(rc.lowerright.x, rc.lowerright.y)
0 0
>>>

Une structure encapsulée peut être instanciée par un constructeur de plusieurs façons :

>>> r = RECT(POINT(1, 2), POINT(3, 4))
>>> r = RECT((1, 2), (3, 4))

Il est possible de récupérer les descripteurs des champs depuis la classe. Ils sont importants pour déboguer car ils contiennent des informations utiles :

>>> print(POINT.x)
<Field type=c_long, ofs=0, size=4>
>>> print(POINT.y)
<Field type=c_long, ofs=4, size=4>
>>>

Avertissement

ctypes ne prend pas en charge le passage par valeur des unions ou des structures avec des champs de bits. Bien que cela puisse fonctionner sur des architectures 32 bits avec un jeu d'instructions x86, ce n'est pas garanti par la bibliothèque en général. Les unions et les structures avec des champs de bits doivent toujours être passées par pointeur.

Alignement et boutisme des structures et des unions

By default, Structure and Union fields are aligned in the same way the C compiler does it. It is possible to override this behavior by specifying a _pack_ class attribute in the subclass definition. This must be set to a positive integer and specifies the maximum alignment for the fields. This is what #pragma pack(n) also does in MSVC. It is also possible to set a minimum alignment for how the subclass itself is packed in the same way #pragma align(n) works in MSVC. This can be achieved by specifying a :_align_ class attribute in the subclass definition.

ctypes suit le boutisme natif pour les Structure et les Union. Pour construire des structures avec un boutisme différent, utilisez les classes de base BigEndianStructure, LittleEndianStructure, BigEndianUnion ou LittleEndianUnion. Ces classes ne peuvent pas avoir de champ pointeur.

Champs de bits dans les structures et les unions

It is possible to create structures and unions containing bit fields. Bit fields are only possible for integer fields, the bit width is specified as the third item in the _fields_ tuples:

>>> class Int(Structure):
...     _fields_ = [("first_16", c_int, 16),
...                 ("second_16", c_int, 16)]
...
>>> print(Int.first_16)
<Field type=c_long, ofs=0:0, bits=16>
>>> print(Int.second_16)
<Field type=c_long, ofs=0:16, bits=16>
>>>

Tableaux

Les tableaux sont des séquences qui contiennent un nombre fixe d'instances du même type.

La meilleure façon de créer des tableaux consiste à multiplier le type de donnée par un entier positif :

TenPointsArrayType = POINT * 10

Voici un exemple — un peu artificiel — d'une structure contenant, entre autres, 4 POINTs :

>>> from ctypes import *
>>> class POINT(Structure):
...     _fields_ = ("x", c_int), ("y", c_int)
...
>>> class MyStruct(Structure):
...     _fields_ = [("a", c_int),
...                 ("b", c_float),
...                 ("point_array", POINT * 4)]
>>>
>>> print(len(MyStruct().point_array))
4
>>>

Comme d'habitude, on crée les instances en appelant la classe :

arr = TenPointsArrayType()
for pt in arr:
    print(pt.x, pt.y)

Le code précédent affiche une suite de 0 0 car le contenu du tableau est initialisé avec des zéros.

Des valeurs d'initialisation du bon type peuvent être passées :

>>> from ctypes import *
>>> TenIntegers = c_int * 10
>>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>> print(ii)
<c_long_Array_10 object at 0x...>
>>> for i in ii: print(i, end=" ")
...
1 2 3 4 5 6 7 8 9 10
>>>

Pointeurs

On crée une instance de pointeur en appelant la fonction pointer() sur un type ctypes :

>>> from ctypes import *
>>> i = c_int(42)
>>> pi = pointer(i)
>>>

Les instances de pointeurs ont un attribut contents qui renvoie l'objet pointé (l'objet i ci-dessus) :

>>> pi.contents
c_long(42)
>>>

Attention, ctypes ne fait pas de ROI (retour de l'objet initial). Il crée un nouvel objet à chaque fois qu'on accède à un attribut :

>>> pi.contents is i
False
>>> pi.contents is pi.contents
False
>>>

Affecter une autre instance de c_int à l'attribut contents du pointeur fait pointer le pointeur vers l'adresse mémoire de cette nouvelle instance :

>>> i = c_int(99)
>>> pi.contents = i
>>> pi.contents
c_long(99)
>>>

Il est possible d'indexer les pointeurs par des entiers :

>>> pi[0]
99
>>>

Affecter à travers un indice change la valeur pointée :

>>> print(i)
c_long(99)
>>> pi[0] = 22
>>> print(i)
c_long(22)
>>>

Si vous êtes sûr de vous, vous pouvez utiliser d'autres valeurs que 0, comme en C : il est ainsi possible de modifier une zone mémoire de votre choix. De manière générale cette fonctionnalité ne s'utilise que sur un pointeur renvoyé par une fonction C, pointeur que vous savez pointer vers un tableau et non sur un seul élément.

Sous le capot, la fonction pointer() fait plus que simplement créer une instance de pointeur ; elle doit d'abord créer un type « pointeur sur… ». Cela s'effectue avec la fonction POINTER(), qui prend en paramètre n'importe quel type ctypes et renvoie un nouveau type :

>>> PI = POINTER(c_int)
>>> PI
<class 'ctypes.LP_c_long'>
>>> PI(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected c_long instead of int
>>> PI(c_int(42))
<ctypes.LP_c_long object at 0x...>
>>>

Appeler le pointeur sur type sans arguments crée un pointeur NULL. Les pointeurs NULL s'évaluent à False :

>>> null_ptr = POINTER(c_int)()
>>> print(bool(null_ptr))
False
>>>

ctypes vérifie que le pointeur n'est pas NULL quand il en déréférence un (mais déréférencer des pointeurs non NULL invalides fait planter Python) :

>>> null_ptr[0]
Traceback (most recent call last):
    ....
ValueError: NULL pointer access
>>>

>>> null_ptr[0] = 1234
Traceback (most recent call last):
    ....
ValueError: NULL pointer access
>>>

Conversions de type

Usually, ctypes does strict type checking. This means, if you have POINTER(c_int) in the argtypes list of a function or as the type of a member field in a structure definition, only instances of exactly the same type are accepted. There are some exceptions to this rule, where ctypes accepts other objects. For example, you can pass compatible array instances instead of pointer types. So, for POINTER(c_int), ctypes accepts an array of c_int:

>>> class Bar(Structure):
...     _fields_ = [("count", c_int), ("values", POINTER(c_int))]
...
>>> bar = Bar()
>>> bar.values = (c_int * 3)(1, 2, 3)
>>> bar.count = 3
>>> for i in range(bar.count):
...     print(bar.values[i])
...
1
2
3
>>>

In addition, if a function argument is explicitly declared to be a pointer type (such as POINTER(c_int)) in argtypes, an object of the pointed type (c_int in this case) can be passed to the function. ctypes will apply the required byref() conversion in this case automatically.

Pour mettre un champ de type POINTER à NULL, il faut lui affecter None :

>>> bar.values = None
>>>

Parfois il faut gérer des incompatibilités entre les types. En C, il est possible de convertir un type en un autre. ctypes fournit la fonction cast() qui permet la même chose. La structure Bar ci-dessus accepte des pointeurs POINTER(c_int) ou des tableaux de c_int comme valeur pour le champ values, mais pas des instances d'autres types :

>>> bar.values = (c_byte * 4)()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: incompatible types, c_byte_Array_4 instance instead of LP_c_long instance
>>>

C'est là que la fonction cast() intervient.

La fonction cast() permet de convertir une instance de ctypes en un pointeur vers un type de données ctypes différent. cast() prend deux paramètres : un objet ctypes qui est, ou qui peut être converti en, un certain pointeur et un type pointeur de ctypes. Elle renvoie une instance du second argument, qui pointe sur le même bloc mémoire que le premier argument :

>>> a = (c_byte * 4)()
>>> cast(a, POINTER(c_int))
<ctypes.LP_c_long object at ...>
>>>

Ainsi, la fonction cast() permet de remplir le champ values de la structure Bar :

>>> bar = Bar()
>>> bar.values = cast((c_byte * 4)(), POINTER(c_int))
>>> print(bar.values[0])
0
>>>

Types incomplets

Un type incomplet est une structure, une union ou un tableau dont les membres ne sont pas encore définis. C'est l'équivalent d'une déclaration avancée en C, où la définition est fournie plus tard :

struct cell; /* forward declaration */

struct cell {
    char *name;
    struct cell *next;
};

Une traduction naïve, mais invalide, en code ctypes ressemblerait à ça :

>>> class cell(Structure):
...     _fields_ = [("name", c_char_p),
...                 ("next", POINTER(cell))]
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in cell
NameError: name 'cell' is not defined
>>>

because the new class cell is not available in the class statement itself. In ctypes, we can define the cell class and set the _fields_ attribute later, after the class statement:

>>> from ctypes import *
>>> class cell(Structure):
...     pass
...
>>> cell._fields_ = [("name", c_char_p),
...                  ("next", POINTER(cell))]
>>>

Essayons. Nous créons deux instances de cell, les faisons pointer l'une sur l'autre et enfin nous suivons quelques maillons de la chaîne de pointeurs :

>>> c1 = cell()
>>> c1.name = b"foo"
>>> c2 = cell()
>>> c2.name = b"bar"
>>> c1.next = pointer(c2)
>>> c2.next = pointer(c1)
>>> p = c1
>>> for i in range(8):
...     print(p.name, end=" ")
...     p = p.next[0]
...
foo bar foo bar foo bar foo bar
>>>

Fonctions de rappel

ctypes permet de créer des pointeurs de fonctions appelables par des appelables Python. On les appelle parfois fonctions de rappel.

Tout d'abord, il faut créer une classe pour la fonction de rappel. La classe connaît la convention d'appel, le type de retour ainsi que le nombre et le type de paramètres que la fonction accepte.

La fabrique CFUNCTYPE() crée un type pour les fonctions de rappel qui suivent la convention d'appel cdecl. En Windows, c'est la fabrique WINFUNCTYPE() qui crée un type pour les fonctions de rappel qui suivent la convention d'appel stdcall.

Le premier paramètre de ces deux fonctions est le type de retour, et les suivants sont les types des arguments qu'attend la fonction de rappel.

I will present an example here which uses the standard C library's qsort() function, that is used to sort items with the help of a callback function. qsort() will be used to sort an array of integers:

>>> IntArray5 = c_int * 5
>>> ia = IntArray5(5, 1, 7, 33, 99)
>>> qsort = libc.qsort
>>> qsort.restype = None
>>>

qsort() must be called with a pointer to the data to sort, the number of items in the data array, the size of one item, and a pointer to the comparison function, the callback. The callback will then be called with two pointers to items, and it must return a negative integer if the first item is smaller than the second, a zero if they are equal, and a positive integer otherwise.

Ainsi notre fonction de rappel reçoit des pointeurs vers des entiers et doit renvoyer un entier. Créons d'abord le type pour la fonction de rappel :

>>> CMPFUNC = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
>>>

Pour commencer, voici une fonction de rappel simple qui affiche les valeurs qu'on lui passe :

>>> def py_cmp_func(a, b):
...     print("py_cmp_func", a[0], b[0])
...     return 0
...
>>> cmp_func = CMPFUNC(py_cmp_func)
>>>

Résultat :

>>> qsort(ia, len(ia), sizeof(c_int), cmp_func)  
py_cmp_func 5 1
py_cmp_func 33 99
py_cmp_func 7 33
py_cmp_func 5 7
py_cmp_func 1 7
>>>

À présent, comparons pour de vrai les deux entiers et renvoyons un résultat utile :

>>> def py_cmp_func(a, b):
...     print("py_cmp_func", a[0], b[0])
...     return a[0] - b[0]
...
>>>
>>> qsort(ia, len(ia), sizeof(c_int), CMPFUNC(py_cmp_func)) 
py_cmp_func 5 1
py_cmp_func 33 99
py_cmp_func 7 33
py_cmp_func 1 7
py_cmp_func 5 7
>>>

Et comme il est facile de le voir, notre tableau est désormais classé :

>>> for i in ia: print(i, end=" ")
...
1 5 7 33 99
>>>

Ces fonctions peuvent aussi être utilisées comme des décorateurs ; il est donc possible d'écrire :

>>> @CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
... def py_cmp_func(a, b):
...     print("py_cmp_func", a[0], b[0])
...     return a[0] - b[0]
...
>>> qsort(ia, len(ia), sizeof(c_int), py_cmp_func)
py_cmp_func 5 1
py_cmp_func 33 99
py_cmp_func 7 33
py_cmp_func 1 7
py_cmp_func 5 7
>>>

Note

Prenez garde à bien conserver une référence à un objet CFUNCTYPE() tant que celui-ci est utilisé par le code C. ctypes ne le fait pas tout seul et, si vous ne le faites pas, le ramasse-miette pourrait les libérer, ce qui fera planter votre programme quand un appel sera fait.

Notez aussi que si la fonction de rappel est appelée dans un fil d'exécution créé hors de Python (p. ex. par du code externe qui appelle la fonction de rappel), ctypes crée un nouveau fil Python « creux » à chaque fois. Ce comportement est acceptable pour la plupart des cas d'utilisation, mais cela implique que les valeurs stockées avec threading.local ne seront pas persistantes d'un appel à l'autre, même si les appels proviennent du même fil d'exécution C.

Accès aux variables exportées depuis une DLL

Some shared libraries not only export functions, they also export variables. An example in the Python library itself is the Py_Version, Python runtime version number encoded in a single constant integer.

ctypes can access values like this with the in_dll() class methods of the type. pythonapi is a predefined symbol giving access to the Python C api:

>>> version = ctypes.c_int.in_dll(ctypes.pythonapi, "Py_Version")
>>> print(hex(version.value))
0x30c00a0

Le pointeur PyImport_FrozenModules exposé par Python est un autre exemple complet de l'utilisation de pointeurs.

Citons la documentation :

This pointer is initialized to point to an array of _frozen records, terminated by one whose members are all NULL or zero. When a frozen module is imported, it is searched in this table. Third-party code could play tricks with this to provide a dynamically created collection of frozen modules.

Donc manipuler ce pointeur peut même se révéler utile. Pour limiter la taille de l'exemple, nous nous bornons à montrer comment lire ce tableau avec ctypes :

>>> from ctypes import *
>>>
>>> class struct_frozen(Structure):
...     _fields_ = [("name", c_char_p),
...                 ("code", POINTER(c_ubyte)),
...                 ("size", c_int),
...                 ("get_code", POINTER(c_ubyte)),  # Function pointer
...                ]
...
>>>

We have defined the _frozen data type, so we can get the pointer to the table:

>>> FrozenTable = POINTER(struct_frozen)
>>> table = FrozenTable.in_dll(pythonapi, "_PyImport_FrozenBootstrap")
>>>

Puisque table est un pointer vers un tableau d'entrées de struct_frozen, il est possible d'itérer dessus, mais il faut être certain que la boucle se termine, car les pointeurs n'ont pas de taille. Tôt ou tard, il planterait probablement avec une erreur de segmentation ou autre, donc mieux vaut sortir de la boucle quand on lit l'entrée NULL :

>>> for item in table:
...     if item.name is None:
...         break
...     print(item.name.decode("ascii"), item.size)
...
_frozen_importlib 31764
_frozen_importlib_external 41499
zipimport 12345
>>>

Le fait que le standard Python possède un module et un paquet figés (indiqués par la valeur négative du membre size) est peu connu, cela ne sert qu'aux tests. Essayez avec import __hello__ par exemple.

Pièges

Il y a quelques cas tordus dans ctypes où on peut s'attendre à un résultat différent de la réalité.

Examinons l'exemple suivant :

>>> from ctypes import *
>>> class POINT(Structure):
...     _fields_ = ("x", c_int), ("y", c_int)
...
>>> class RECT(Structure):
...     _fields_ = ("a", POINT), ("b", POINT)
...
>>> p1 = POINT(1, 2)
>>> p2 = POINT(3, 4)
>>> rc = RECT(p1, p2)
>>> print(rc.a.x, rc.a.y, rc.b.x, rc.b.y)
1 2 3 4
>>> # now swap the two points
>>> rc.a, rc.b = rc.b, rc.a
>>> print(rc.a.x, rc.a.y, rc.b.x, rc.b.y)
3 4 3 4
>>>

Diantre. On s'attendait certainement à ce que le dernier résultat affiche 3 4 1 2. Que s'est-il passé ? Les étapes de la ligne rc.a, rc.b = rc.b, rc.a ci-dessus sont les suivantes :

>>> temp0, temp1 = rc.b, rc.a
>>> rc.a = temp0
>>> rc.b = temp1
>>>

Les objets temp0 et temp1 utilisent encore le tampon interne de l'objet rc ci-dessus. Donc exécuter rc.a = temp0 copie le contenu du tampon de temp0 dans celui de rc. Ce qui, par ricochet, modifie le contenu de temp1. Et donc, la dernière affectation, rc.b = temp1, n'a pas l'effet escompté.

Gardez en tête qu'accéder au sous-objet depuis une Structure, une Union ou un Array ne copie pas le sous-objet, mais crée un objet interface qui accède au tampon sous-jacent de l'objet initial.

Un autre exemple de comportement a priori inattendu est le suivant :

>>> s = c_char_p()
>>> s.value = b"abc def ghi"
>>> s.value
b'abc def ghi'
>>> s.value is s.value
False
>>>

Note

La valeur d'une instance de c_char_p ne peut être initialisée qu'avec un octet ou un entier.

Pourquoi cela affiche-t'il False ? Les instances ctypes sont des objets qui contiennent un bloc mémoire et des descriptor qui donnent accès au contenu du ce bloc. Stocker un objet Python dans le bloc mémoire ne stocke pas l'objet même ; seuls ses contents le sont. Accéder au contents crée un nouvel objet Python à chaque fois !

Types de données à taille flottante

ctypes assure la prise en charge des tableaux et des structures à taille flottante.

La fonction resize() permet de redimensionner la taille du tampon mémoire d'un objet ctypes existant. Cette fonction prend l'objet comme premier argument et la taille en octets désirée comme second. La taille du tampon mémoire ne peut pas être inférieure à celle occupée par un objet unitaire du type considéré. Une ValueError est levée si c'est le cas :

>>> short_array = (c_short * 4)()
>>> print(sizeof(short_array))
8
>>> resize(short_array, 4)
Traceback (most recent call last):
    ...
ValueError: minimum size is 8
>>> resize(short_array, 32)
>>> sizeof(short_array)
32
>>> sizeof(type(short_array))
8
>>>

Cela dit, comment accéder aux éléments supplémentaires contenus dans le tableau ? Vu que le type ne connaît que 4 éléments, on obtient une erreur si l'on accède aux suivants :

>>> short_array[:]
[0, 0, 0, 0]
>>> short_array[7]
Traceback (most recent call last):
    ...
IndexError: invalid index
>>>

Une autre approche pour utiliser des types de donnée à taille flottante avec ctypes consiste à tirer profit de la nature intrinsèquement dynamique de Python et de (re)définir le type de donnée une fois que la taille demandée est connue, au cas-par-cas.

Référence du module

Recherche de bibliothèques partagées

Les langages compilés ont besoin d'accéder aux bibliothèques partagées au moment de la compilation, de l'édition de liens et pendant l'exécution du programme.

The purpose of the find_library() function is to locate a library in a way similar to what the compiler or runtime loader does (on platforms with several versions of a shared library the most recent should be loaded), while the ctypes library loaders act like when a program is run, and call the runtime loader directly.

The ctypes.util module provides a function which can help to determine the library to load.

ctypes.util.find_library(name)

Tente de trouver une bibliothèque et en renvoie le chemin. name est le nom de la bibliothèque sans préfixe — comme lib — ni suffixe — comme .so, .dylib ou un numéro de version (c.-à-d. la même forme que l'option POSIX de l'éditeur de lien -l). Si la fonction ne parvient pas à trouver de bibliothèque, elle renvoie None.

Le mode opératoire exact dépend du système.

On Linux, find_library() tries to run external programs (/sbin/ldconfig, gcc, objdump and ld) to find the library file. It returns the filename of the library file.

Modifié dans la version 3.6: Sous Linux, si les autres moyens échouent, la fonction utilise la variable d'environnement LD_LIBRARY_PATH pour trouver la bibliothèque.

Voici quelques exemples :

>>> from ctypes.util import find_library
>>> find_library("m")
'libm.so.6'
>>> find_library("c")
'libc.so.6'
>>> find_library("bz2")
'libbz2.so.1.0'
>>>

On macOS and Android, find_library() uses the system's standard naming schemes and paths to locate the library, and returns a full pathname if successful:

>>> from ctypes.util import find_library
>>> find_library("c")
'/usr/lib/libc.dylib'
>>> find_library("m")
'/usr/lib/libm.dylib'
>>> find_library("bz2")
'/usr/lib/libbz2.dylib'
>>> find_library("AGL")
'/System/Library/Frameworks/AGL.framework/AGL'
>>>

On Windows, find_library() searches along the system search path, and returns the full pathname, but since there is no predefined naming scheme a call like find_library("c") will fail and return None.

If wrapping a shared library with ctypes, it may be better to determine the shared library name at development time, and hardcode that into the wrapper module instead of using find_library() to locate the library at runtime.

Chargement des bibliothèques partagées

Il y a plusieurs moyens de charger une bibliothèque partagée dans un processus Python. L'un d'entre eux consiste à instancier une des classes suivantes :

class ctypes.CDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None)

Instances of this class represent loaded shared libraries. Functions in these libraries use the standard C calling convention, and are assumed to return int.

En Windows, créer une instance de CDLL peut échouer, même si une DLL du bon nom existe. Quand une des dépendances de la DLL à charger ne peut pas être trouvée, une OSError est levée avec le message "[WinError 126] The specified module could not be found". Ce message d'erreur ne contient pas le nom de la DLL manquante car l'API Windows ne fournit pas cette information. Cela rend l'erreur délicate à analyser ; pour la résoudre, il faut lister toutes les dépendances de la DLL et trouver celle qui manque en utilisant des outils de débogage et de traçage Windows.

Modifié dans la version 3.12: The name parameter can now be a path-like object.

Voir aussi

DUMPBIN — un utilitaire Microsoft pour lister les dépendances d'une DLL.

class ctypes.OleDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None)

En Windows seulement : une instance de cette classe représente une bibliothèque partagée déjà chargée. Les fonctions de cette bibliothèque utilisent la convention d'appel stdcall, et doivent renvoyer un code HRESULT (propre à Windows). Les valeurs de HRESULT contiennent des informations précisant si l'appel de la fonction a échoué ou s'il a réussi, ainsi qu'un code d'erreur supplémentaire. Si la valeur de retour signale un échec, une OSError est levée automatiquement.

Modifié dans la version 3.3: WindowsError used to be raised, which is now an alias of OSError.

Modifié dans la version 3.12: The name parameter can now be a path-like object.

class ctypes.WinDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None)

Windows only: Instances of this class represent loaded shared libraries, functions in these libraries use the stdcall calling convention, and are assumed to return int by default.

Modifié dans la version 3.12: The name parameter can now be a path-like object.

Le verrou global de l'interpréteur Python est relâché avant chaque appel d'une fonction exposée par ces bibliothèques et ré-activé après.

class ctypes.PyDLL(name, mode=DEFAULT_MODE, handle=None)

Cette classe est identique à CDLL, à ceci près que le GIL n'est pas relâché pendant l'appel de la fonction, et, qu'au terme de l'appel, le drapeau d'erreur Python est vérifié. Si celui-ci est activé, une exception Python est levée.

Donc, cette classe ne sert qu'à appeler les fonctions de l'API C de Python.

Modifié dans la version 3.12: The name parameter can now be a path-like object.

All these classes can be instantiated by calling them with at least one argument, the pathname of the shared library. If you have an existing handle to an already loaded shared library, it can be passed as the handle named parameter, otherwise the underlying platform's dlopen() or LoadLibrary() function is used to load the library into the process, and to get a handle to it.

Le mode de chargement de la bibliothèque est défini par le paramètre mode. Pour plus de détails, référez-vous à l'entrée dlopen(3) du manuel. En Windows, mode est ignoré. Sur les systèmes POSIX, RTLD_NOW y est toujours ajouté. Ceci n'est pas configurable.

The use_errno parameter, when set to true, enables a ctypes mechanism that allows accessing the system errno error number in a safe way. ctypes maintains a thread-local copy of the system's errno variable; if you call foreign functions created with use_errno=True then the errno value before the function call is swapped with the ctypes private copy, the same happens immediately after the function call.

La fonction ctypes.get_errno() renvoie la valeur de la copie privée de ctypes. La fonction ctypes.set_errno() affecte une nouvelle valeur à la copie privée et renvoie l'ancienne valeur.

The use_last_error parameter, when set to true, enables the same mechanism for the Windows error code which is managed by the GetLastError() and SetLastError() Windows API functions; ctypes.get_last_error() and ctypes.set_last_error() are used to request and change the ctypes private copy of the windows error code.

The winmode parameter is used on Windows to specify how the library is loaded (since mode is ignored). It takes any value that is valid for the Win32 API LoadLibraryEx flags parameter. When omitted, the default is to use the flags that result in the most secure DLL load, which avoids issues such as DLL hijacking. Passing the full path to the DLL is the safest way to ensure the correct library and dependencies are loaded.

Modifié dans la version 3.8: Ajout du paramètre winmode.

ctypes.RTLD_GLOBAL

Valeur possible pour le paramètre mode. Vaut zéro sur les plates-formes où ce drapeau n'est pas disponible.

ctypes.RTLD_LOCAL

Valeur possible pour le paramètre mode. Vaut RTLD_GLOBAL sur les plates-formes où ce drapeau n'est pas disponible.

ctypes.DEFAULT_MODE

Mode de chargement par défaut des bibliothèques partagées. Vaut RTLD_GLOBAL sur OSX 10.3 et RTLD_LOCAL sur les autres systèmes d'exploitation.

Les instances de ces classes n'ont pas de méthodes publiques ; on accède aux fonctions de la bibliothèque partagée par attribut ou par indiçage. Notez que les résultats des accès par attribut sont mis en cache, et donc des accès consécutifs renvoient à chaque fois le même objet. Accéder à une fonction par indice renvoie cependant chaque fois un nouvel objet :

>>> from ctypes import CDLL
>>> libc = CDLL("libc.so.6")  # On Linux
>>> libc.time == libc.time
True
>>> libc['time'] == libc['time']
False

Les attributs publics suivants sont disponibles, leur nom commence par un tiret bas pour éviter les conflits avec les noms des fonctions exportées :

PyDLL._handle

Le lien système d'accès à la bibliothèque.

PyDLL._name

Nom de la bibliothèque donné au constructeur.

Shared libraries can also be loaded by using one of the prefabricated objects, which are instances of the LibraryLoader class, either by calling the LoadLibrary() method, or by retrieving the library as attribute of the loader instance.

class ctypes.LibraryLoader(dlltype)

Classe pour charger une bibliothèque partagée. dlltype doit être de type CDLL, PyDLL, WinDLL ou OleDLL.

__getattr__() has special behavior: It allows loading a shared library by accessing it as attribute of a library loader instance. The result is cached, so repeated attribute accesses return the same library each time.

LoadLibrary(name)

Charge une bibliothèque partagée dans le processus et la renvoie. Cette méthode renvoie toujours une nouvelle instance de la bibliothèque.

Plusieurs chargeurs sont fournis :

ctypes.cdll

Pour créer des instances de CDLL.

ctypes.windll

Pour créer des instances de WinDLL (uniquement en Windows).

ctypes.oledll

Pour créer des instances de OleDLL (uniquement en Windows).

ctypes.pydll

Pour créer des instances de PyDLL.

Il existe un moyen rapide d'accéder directement à l'API C Python :

ctypes.pythonapi

An instance of PyDLL that exposes Python C API functions as attributes. Note that all these functions are assumed to return C int, which is of course not always the truth, so you have to assign the correct restype attribute to use these functions.

Lève un évènement d'audit ctypes.dlopen, avec en argument name.

Accéder à une fonction d'une bibliothèque lève un évènement d'audit ctypes.dlsym avec library (l'objet bibliothèque) et name (le nom du symbole — une chaîne de caractères ou un entier) comme arguments.

Si seul le lien sur la bibliothèque, et non l'objet, est disponible, accéder à une fonction lève l'évènement d'audit ctypes.dlsym/handle avec handle (le lien vers la bibliothèque) et name comme arguments.

Fonctions externes

Comme expliqué dans la section précédente, on peut accéder aux fonctions externes au travers des attributs des bibliothèques partagées. Un objet fonction créé de cette façon accepte par défaut un nombre quelconque d'arguments qui peuvent être de n'importe quel type de données ctypes. Il renvoie le type par défaut du chargeur de la bibliothèque. Ce sont des instances de la classe privée :

class ctypes._FuncPtr

Classe de base pour les fonctions externes C.

Une instance de fonction externe est également un type de donnée compatible avec le C ; elle représente un pointeur vers une fonction.

Son comportement peut-être personnalisé en réaffectant les attributs spécifiques de l'objet représentant la fonction externe.

restype

Assign a ctypes type to specify the result type of the foreign function. Use None for void, a function not returning anything.

It is possible to assign a callable Python object that is not a ctypes type, in this case the function is assumed to return a C int, and the callable will be called with this integer, allowing further processing or error checking. Using this is deprecated, for more flexible post processing or error checking use a ctypes data type as restype and assign a callable to the errcheck attribute.

argtypes

Fait correspondre le type des arguments que la fonction accepte avec un n-uplet de types ctypes. Les fonctions qui utilisent la convention d'appel stdcall ne peuvent être appelées qu'avec le même nombre d'arguments que la taille du n-uplet mais les fonctions qui utilisent la convention d'appel C acceptent aussi des arguments additionnels non-définis.

When a foreign function is called, each actual argument is passed to the from_param() class method of the items in the argtypes tuple, this method allows adapting the actual argument to an object that the foreign function accepts. For example, a c_char_p item in the argtypes tuple will convert a string passed as argument into a bytes object using ctypes conversion rules.

New: It is now possible to put items in argtypes which are not ctypes types, but each item must have a from_param() method which returns a value usable as argument (integer, string, ctypes instance). This allows defining adapters that can adapt custom objects as function parameters.

errcheck

Définit une fonction Python ou tout autre appelable qui sera appelé avec trois arguments ou plus :

callable(result, func, arguments)

result is what the foreign function returns, as specified by the restype attribute.

func est l'objet représentant la fonction externe elle-même. Cet accesseur permet de réutiliser le même appelable pour vérifier le résultat de plusieurs fonctions ou de faire des actions supplémentaires après leur exécution.

arguments est le n-uplet qui contient les paramètres initiaux passés à la fonction, ceci permet de spécialiser le comportement des arguments utilisés.

L'objet renvoyé par cette fonction est celui renvoyé par l'appel de la fonction externe, mais il peut aussi vérifier la valeur du résultat et lever une exception si l'appel a échoué.

exception ctypes.ArgumentError

Exception levée quand un appel à la fonction externe ne peut pas convertir un des arguments qu'elle a reçus.

On Windows, when a foreign function call raises a system exception (for example, due to an access violation), it will be captured and replaced with a suitable Python exception. Further, an auditing event ctypes.set_exception with argument code will be raised, allowing an audit hook to replace the exception with its own.

Certaines manières d'appeler des fonction externes peuvent lever des évènements d'audit ctypes.call_function avec function pointer et arguments comme arguments.

Prototypes de fonction

Il est aussi possible de créer des fonctions externes en instanciant des prototypes de fonction. Les prototypes de fonction ressemblent beaucoup aux prototypes de fonctions en C ; ils décrivent une fonction (type de retour, type des arguments, convention d'appel) sans préciser son implémentation. Les fabriques de fonctions prennent en entrée le type de retour et le type des arguments de la fonction, et peuvent être utilisées comme des décorateurs-fabrique et ainsi s'appliquer à des fonctions avec la syntaxe @décorateur. Ceci est illustré dans la section Fonctions de rappel.

ctypes.CFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)

Renvoie un prototype de fonction qui crée des fonctions qui suivent la convention d'appel standard C. Les fonctions libèreront le GIL lors de leur exécution. Si use_errno est vrai, la copie privée ctypes de la variable système errno est échangée avec la vraie valeur de errno avant et après l'appel ; use_last_error a le même effet sous Windows.

ctypes.WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)

Windows only: The returned function prototype creates functions that use the stdcall calling convention. The function will release the GIL during the call. use_errno and use_last_error have the same meaning as above.

ctypes.PYFUNCTYPE(restype, *argtypes)

Renvoie un prototype de fonction qui crée des fonctions qui suivent la convention d'appel Python. Les fonctions ne libèreront pas le GIL lors de leur exécution.

Il y a plusieurs façons d'instancier les prototypes de fonction créés par ces fabriques, selon le type et le nombre de paramètres de l'appel :

prototype(address)

Renvoie une fonction externe sur l'adresse donnée sous la forme d'un entier.

prototype(callable)

Crée une fonction appelable depuis du code C (une fonction de rappel) d'un appelable Python donné en paramètre.

prototype(func_spec[, paramflags])

Renvoie une fonction externe exposée par une bibliothèque partagée. func_spec est un couple (nom_ou_indice, bibliothèque). Le premier élément est le nom de la fonction à passer comme une chaîne ou bien son indice (dans la table des symboles) à passer comme un entier. Le second élément est l'instance de la bibliothèque partagée.

prototype(vtbl_index, name[, paramflags[, iid]])

Renvoie une fonction qui appelle une méthode COM. vtbl_index est l'indice de la fonction dans la table virtuelle, un petit entier positif. name est le nom de la méthode COM. iid est un pointeur optionnel vers l'identificateur de plateforme, qui est utilisé dans la remontée d'erreurs étendue.

COM methods use a special calling convention: They require a pointer to the COM interface as first argument, in addition to those parameters that are specified in the argtypes tuple.

Le paramètre optionnel paramflags crée une fabrique de fonction externes avec des fonctionnalités supplémentaires par rapport à celles décrites ci-dessus.

paramflags must be a tuple of the same length as argtypes.

Chaque élément de ce n-uplet contient des informations supplémentaires sur le paramètre correspondant. Ce doit être aussi un n-uplet, avec un, deux ou trois éléments.

Le premier élément est un entier qui contient une combinaison de drapeaux qui précisent le sens des paramètres (entrée ou sortie) :

1

Paramètre d'entrée.

2

Paramètre de sortie. La fonction externe va modifier cette valeur.

4

Paramètre d'entrée, valant 0 par défaut.

Le deuxième élément (optionnel) est une chaîne de caractères représentant le nom du paramètre. Si cet élément est donné, la fonction externe pourra être appelée avec des paramètres nommés.

Le troisième élément (optionnel) est la valeur par défaut du paramètre.

The following example demonstrates how to wrap the Windows MessageBoxW function so that it supports default parameters and named arguments. The C declaration from the windows header file is this:

WINUSERAPI int WINAPI
MessageBoxW(
    HWND hWnd,
    LPCWSTR lpText,
    LPCWSTR lpCaption,
    UINT uType);

L'encapsulation ctypes correspondante est alors :

>>> from ctypes import c_int, WINFUNCTYPE, windll
>>> from ctypes.wintypes import HWND, LPCWSTR, UINT
>>> prototype = WINFUNCTYPE(c_int, HWND, LPCWSTR, LPCWSTR, UINT)
>>> paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", "Hello from ctypes"), (1, "flags", 0)
>>> MessageBox = prototype(("MessageBoxW", windll.user32), paramflags)

La fonction MessageBox peut désormais être appelée des manières suivantes :

>>> MessageBox()
>>> MessageBox(text="Spam, spam, spam")
>>> MessageBox(flags=2, text="foo bar")

L'exemple qui suit traite des paramètres en sortie. La fonction win32 GetWindowRect donne les dimensions d'une fenêtre en les copiant dans une structure RECT que l'appelant doit fournir. Sa déclaration en C est :

WINUSERAPI BOOL WINAPI
GetWindowRect(
     HWND hWnd,
     LPRECT lpRect);

L'encapsulation ctypes correspondante est alors :

>>> from ctypes import POINTER, WINFUNCTYPE, windll, WinError
>>> from ctypes.wintypes import BOOL, HWND, RECT
>>> prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))
>>> paramflags = (1, "hwnd"), (2, "lprect")
>>> GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)
>>>

Les fonctions avec des paramètres en sortie renvoient automatiquement la valeur du paramètre de sortie s'il n'y en a qu'un seul, ou un n-uplet avec les valeurs de sortie de chaque paramètre s'il y en a plusieurs. Ici, la fonction GetWindowRect renvoie donc une instance de RECT quand elle est appelée.

Output parameters can be combined with the errcheck protocol to do further output processing and error checking. The win32 GetWindowRect api function returns a BOOL to signal success or failure, so this function could do the error checking, and raises an exception when the api call failed:

>>> def errcheck(result, func, args):
...     if not result:
...         raise WinError()
...     return args
...
>>> GetWindowRect.errcheck = errcheck
>>>

If the errcheck function returns the argument tuple it receives unchanged, ctypes continues the normal processing it does on the output parameters. If you want to return a tuple of window coordinates instead of a RECT instance, you can retrieve the fields in the function and return them instead, the normal processing will no longer take place:

>>> def errcheck(result, func, args):
...     if not result:
...         raise WinError()
...     rc = args[1]
...     return rc.left, rc.top, rc.bottom, rc.right
...
>>> GetWindowRect.errcheck = errcheck
>>>

Fonctions utilitaires

ctypes.addressof(obj)

Returns the address of the memory buffer as integer. obj must be an instance of a ctypes type.

Raises an auditing event ctypes.addressof with argument obj.

ctypes.alignment(obj_or_type)

Returns the alignment requirements of a ctypes type. obj_or_type must be a ctypes type or instance.

ctypes.byref(obj[, offset])

Returns a light-weight pointer to obj, which must be an instance of a ctypes type. offset defaults to zero, and must be an integer that will be added to the internal pointer value.

byref(obj, offset) corresponds to this C code:

(((char *)&obj) + offset)

The returned object can only be used as a foreign function call parameter. It behaves similar to pointer(obj), but the construction is a lot faster.

ctypes.cast(obj, type)

This function is similar to the cast operator in C. It returns a new instance of type which points to the same memory block as obj. type must be a pointer type, and obj must be an object that can be interpreted as a pointer.

ctypes.create_string_buffer(init_or_size, size=None)

This function creates a mutable character buffer. The returned object is a ctypes array of c_char.

init_or_size must be an integer which specifies the size of the array, or a bytes object which will be used to initialize the array items.

If a bytes object is specified as first argument, the buffer is made one item larger than its length so that the last element in the array is a NUL termination character. An integer can be passed as second argument which allows specifying the size of the array if the length of the bytes should not be used.

Raises an auditing event ctypes.create_string_buffer with arguments init, size.

ctypes.create_unicode_buffer(init_or_size, size=None)

This function creates a mutable unicode character buffer. The returned object is a ctypes array of c_wchar.

init_or_size must be an integer which specifies the size of the array, or a string which will be used to initialize the array items.

If a string is specified as first argument, the buffer is made one item larger than the length of the string so that the last element in the array is a NUL termination character. An integer can be passed as second argument which allows specifying the size of the array if the length of the string should not be used.

Raises an auditing event ctypes.create_unicode_buffer with arguments init, size.

ctypes.DllCanUnloadNow()

Windows only: This function is a hook which allows implementing in-process COM servers with ctypes. It is called from the DllCanUnloadNow function that the _ctypes extension dll exports.

ctypes.DllGetClassObject()

Windows only: This function is a hook which allows implementing in-process COM servers with ctypes. It is called from the DllGetClassObject function that the _ctypes extension dll exports.

ctypes.util.find_library(name)

Try to find a library and return a pathname. name is the library name without any prefix like lib, suffix like .so, .dylib or version number (this is the form used for the posix linker option -l). If no library can be found, returns None.

Le mode opératoire exact dépend du système.

ctypes.util.find_msvcrt()

Windows only: return the filename of the VC runtime library used by Python, and by the extension modules. If the name of the library cannot be determined, None is returned.

If you need to free memory, for example, allocated by an extension module with a call to the free(void *), it is important that you use the function in the same library that allocated the memory.

ctypes.FormatError([code])

Windows only: Returns a textual description of the error code code. If no error code is specified, the last error code is used by calling the Windows api function GetLastError.

ctypes.GetLastError()

Windows only: Returns the last error code set by Windows in the calling thread. This function calls the Windows GetLastError() function directly, it does not return the ctypes-private copy of the error code.

ctypes.get_errno()

Returns the current value of the ctypes-private copy of the system errno variable in the calling thread.

Raises an auditing event ctypes.get_errno with no arguments.

ctypes.get_last_error()

Windows only: returns the current value of the ctypes-private copy of the system LastError variable in the calling thread.

Raises an auditing event ctypes.get_last_error with no arguments.

ctypes.memmove(dst, src, count)

Same as the standard C memmove library function: copies count bytes from src to dst. dst and src must be integers or ctypes instances that can be converted to pointers.

ctypes.memset(dst, c, count)

Same as the standard C memset library function: fills the memory block at address dst with count bytes of value c. dst must be an integer specifying an address, or a ctypes instance.

ctypes.POINTER(type, /)

Create and return a new ctypes pointer type. Pointer types are cached and reused internally, so calling this function repeatedly is cheap. type must be a ctypes type.

ctypes.pointer(obj, /)

Create a new pointer instance, pointing to obj. The returned object is of the type POINTER(type(obj)).

Note: If you just want to pass a pointer to an object to a foreign function call, you should use byref(obj) which is much faster.

ctypes.resize(obj, size)

This function resizes the internal memory buffer of obj, which must be an instance of a ctypes type. It is not possible to make the buffer smaller than the native size of the objects type, as given by sizeof(type(obj)), but it is possible to enlarge the buffer.

ctypes.set_errno(value)

Set the current value of the ctypes-private copy of the system errno variable in the calling thread to value and return the previous value.

Raises an auditing event ctypes.set_errno with argument errno.

ctypes.set_last_error(value)

Windows only: set the current value of the ctypes-private copy of the system LastError variable in the calling thread to value and return the previous value.

Raises an auditing event ctypes.set_last_error with argument error.

ctypes.sizeof(obj_or_type)

Returns the size in bytes of a ctypes type or instance memory buffer. Does the same as the C sizeof operator.

ctypes.string_at(address, size=-1)

This function returns the C string starting at memory address address as a bytes object. If size is specified, it is used as size, otherwise the string is assumed to be zero-terminated.

Raises an auditing event ctypes.string_at with arguments address, size.

ctypes.WinError(code=None, descr=None)

Windows only: this function is probably the worst-named thing in ctypes. It creates an instance of OSError. If code is not specified, GetLastError is called to determine the error code. If descr is not specified, FormatError() is called to get a textual description of the error.

Modifié dans la version 3.3: An instance of WindowsError used to be created, which is now an alias of OSError.

ctypes.wstring_at(address, size=-1)

This function returns the wide character string starting at memory address address as a string. If size is specified, it is used as the number of characters of the string, otherwise the string is assumed to be zero-terminated.

Raises an auditing event ctypes.wstring_at with arguments address, size.

Types de données

class ctypes._CData

This non-public class is the common base class of all ctypes data types. Among other things, all ctypes type instances contain a memory block that hold C compatible data; the address of the memory block is returned by the addressof() helper function. Another instance variable is exposed as _objects; this contains other Python objects that need to be kept alive in case the memory block contains pointers.

Common methods of ctypes data types, these are all class methods (to be exact, they are methods of the metaclass):

from_buffer(source[, offset])

This method returns a ctypes instance that shares the buffer of the source object. The source object must support the writeable buffer interface. The optional offset parameter specifies an offset into the source buffer in bytes; the default is zero. If the source buffer is not large enough a ValueError is raised.

Raises an auditing event ctypes.cdata/buffer with arguments pointer, size, offset.

from_buffer_copy(source[, offset])

This method creates a ctypes instance, copying the buffer from the source object buffer which must be readable. The optional offset parameter specifies an offset into the source buffer in bytes; the default is zero. If the source buffer is not large enough a ValueError is raised.

Raises an auditing event ctypes.cdata/buffer with arguments pointer, size, offset.

from_address(address)

This method returns a ctypes type instance using the memory specified by address which must be an integer.

This method, and others that indirectly call this method, raises an auditing event ctypes.cdata with argument address.

from_param(obj)

This method adapts obj to a ctypes type. It is called with the actual object used in a foreign function call when the type is present in the foreign function's argtypes tuple; it must return an object that can be used as a function call parameter.

All ctypes data types have a default implementation of this classmethod that normally returns obj if that is an instance of the type. Some types accept other objects as well.

in_dll(library, name)

This method returns a ctypes type instance exported by a shared library. name is the name of the symbol that exports the data, library is the loaded shared library.

Common instance variables of ctypes data types:

_b_base_

Sometimes ctypes data instances do not own the memory block they contain, instead they share part of the memory block of a base object. The _b_base_ read-only member is the root ctypes object that owns the memory block.

_b_needsfree_

This read-only variable is true when the ctypes data instance has allocated the memory block itself, false otherwise.

_objects

This member is either None or a dictionary containing Python objects that need to be kept alive so that the memory block contents is kept valid. This object is only exposed for debugging; never modify the contents of this dictionary.

Types de données de base

class ctypes._SimpleCData

This non-public class is the base class of all fundamental ctypes data types. It is mentioned here because it contains the common attributes of the fundamental ctypes data types. _SimpleCData is a subclass of _CData, so it inherits their methods and attributes. ctypes data types that are not and do not contain pointers can now be pickled.

Instances have a single attribute:

value

This attribute contains the actual value of the instance. For integer and pointer types, it is an integer, for character types, it is a single character bytes object or string, for character pointer types it is a Python bytes object or string.

When the value attribute is retrieved from a ctypes instance, usually a new object is returned each time. ctypes does not implement original object return, always a new object is constructed. The same is true for all other ctypes object instances.

Fundamental data types, when returned as foreign function call results, or, for example, by retrieving structure field members or array items, are transparently converted to native Python types. In other words, if a foreign function has a restype of c_char_p, you will always receive a Python bytes object, not a c_char_p instance.

Subclasses of fundamental data types do not inherit this behavior. So, if a foreign functions restype is a subclass of c_void_p, you will receive an instance of this subclass from the function call. Of course, you can get the value of the pointer by accessing the value attribute.

These are the fundamental ctypes data types:

class ctypes.c_byte

Represents the C signed char datatype, and interprets the value as small integer. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_char

Represents the C char datatype, and interprets the value as a single character. The constructor accepts an optional string initializer, the length of the string must be exactly one character.

class ctypes.c_char_p

Represents the C char* datatype when it points to a zero-terminated string. For a general character pointer that may also point to binary data, POINTER(c_char) must be used. The constructor accepts an integer address, or a bytes object.

class ctypes.c_double

Represents the C double datatype. The constructor accepts an optional float initializer.

class ctypes.c_longdouble

Represents the C long double datatype. The constructor accepts an optional float initializer. On platforms where sizeof(long double) == sizeof(double) it is an alias to c_double.

class ctypes.c_float

Represents the C float datatype. The constructor accepts an optional float initializer.

class ctypes.c_int

Represents the C signed int datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where sizeof(int) == sizeof(long) it is an alias to c_long.

class ctypes.c_int8

Represents the C 8-bit signed int datatype. Usually an alias for c_byte.

class ctypes.c_int16

Represents the C 16-bit signed int datatype. Usually an alias for c_short.

class ctypes.c_int32

Represents the C 32-bit signed int datatype. Usually an alias for c_int.

class ctypes.c_int64

Represents the C 64-bit signed int datatype. Usually an alias for c_longlong.

class ctypes.c_long

Represents the C signed long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_longlong

Represents the C signed long long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_short

Represents the C signed short datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_size_t

Represents the C size_t datatype.

class ctypes.c_ssize_t

Represents the C ssize_t datatype.

Nouveau dans la version 3.2.

class ctypes.c_time_t

Represents the C time_t datatype.

Nouveau dans la version 3.12.

class ctypes.c_ubyte

Represents the C unsigned char datatype, it interprets the value as small integer. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_uint

Represents the C unsigned int datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where sizeof(int) == sizeof(long) it is an alias for c_ulong.

class ctypes.c_uint8

Represents the C 8-bit unsigned int datatype. Usually an alias for c_ubyte.

class ctypes.c_uint16

Represents the C 16-bit unsigned int datatype. Usually an alias for c_ushort.

class ctypes.c_uint32

Represents the C 32-bit unsigned int datatype. Usually an alias for c_uint.

class ctypes.c_uint64

Represents the C 64-bit unsigned int datatype. Usually an alias for c_ulonglong.

class ctypes.c_ulong

Represents the C unsigned long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_ulonglong

Represents the C unsigned long long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_ushort

Represents the C unsigned short datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class ctypes.c_void_p

Represents the C void* type. The value is represented as integer. The constructor accepts an optional integer initializer.

class ctypes.c_wchar

Represents the C wchar_t datatype, and interprets the value as a single character unicode string. The constructor accepts an optional string initializer, the length of the string must be exactly one character.

class ctypes.c_wchar_p

Represents the C wchar_t* datatype, which must be a pointer to a zero-terminated wide character string. The constructor accepts an integer address, or a string.

class ctypes.c_bool

Represent the C bool datatype (more accurately, _Bool from C99). Its value can be True or False, and the constructor accepts any object that has a truth value.

class ctypes.HRESULT

Windows only: Represents a HRESULT value, which contains success or error information for a function or method call.

class ctypes.py_object

Represents the C PyObject* datatype. Calling this without an argument creates a NULL PyObject* pointer.

The ctypes.wintypes module provides quite some other Windows specific data types, for example HWND, WPARAM, or DWORD. Some useful structures like MSG or RECT are also defined.

Types de données dérivés de Structure

class ctypes.Union(*args, **kw)

Abstract base class for unions in native byte order.

class ctypes.BigEndianUnion(*args, **kw)

Abstract base class for unions in big endian byte order.

Nouveau dans la version 3.11.

class ctypes.LittleEndianUnion(*args, **kw)

Abstract base class for unions in little endian byte order.

Nouveau dans la version 3.11.

class ctypes.BigEndianStructure(*args, **kw)

Abstract base class for structures in big endian byte order.

class ctypes.LittleEndianStructure(*args, **kw)

Abstract base class for structures in little endian byte order.

Structures and unions with non-native byte order cannot contain pointer type fields, or any other data types containing pointer type fields.

class ctypes.Structure(*args, **kw)

Abstract base class for structures in native byte order.

Concrete structure and union types must be created by subclassing one of these types, and at least define a _fields_ class variable. ctypes will create descriptors which allow reading and writing the fields by direct attribute accesses. These are the

_fields_

A sequence defining the structure fields. The items must be 2-tuples or 3-tuples. The first item is the name of the field, the second item specifies the type of the field; it can be any ctypes data type.

For integer type fields like c_int, a third optional item can be given. It must be a small positive integer defining the bit width of the field.

Field names must be unique within one structure or union. This is not checked, only one field can be accessed when names are repeated.

It is possible to define the _fields_ class variable after the class statement that defines the Structure subclass, this allows creating data types that directly or indirectly reference themselves:

class List(Structure):
    pass
List._fields_ = [("pnext", POINTER(List)),
                 ...
                ]

The _fields_ class variable must, however, be defined before the type is first used (an instance is created, sizeof() is called on it, and so on). Later assignments to the _fields_ class variable will raise an AttributeError.

It is possible to define sub-subclasses of structure types, they inherit the fields of the base class plus the _fields_ defined in the sub-subclass, if any.

_pack_

An optional small integer that allows overriding the alignment of structure fields in the instance. _pack_ must already be defined when _fields_ is assigned, otherwise it will have no effect. Setting this attribute to 0 is the same as not setting it at all.

_align_

An optional small integer that allows overriding the alignment of the structure when being packed or unpacked to/from memory. Setting this attribute to 0 is the same as not setting it at all.

_anonymous_

An optional sequence that lists the names of unnamed (anonymous) fields. _anonymous_ must be already defined when _fields_ is assigned, otherwise it will have no effect.

The fields listed in this variable must be structure or union type fields. ctypes will create descriptors in the structure type that allows accessing the nested fields directly, without the need to create the structure or union field.

Here is an example type (Windows):

class _U(Union):
    _fields_ = [("lptdesc", POINTER(TYPEDESC)),
                ("lpadesc", POINTER(ARRAYDESC)),
                ("hreftype", HREFTYPE)]

class TYPEDESC(Structure):
    _anonymous_ = ("u",)
    _fields_ = [("u", _U),
                ("vt", VARTYPE)]

The TYPEDESC structure describes a COM data type, the vt field specifies which one of the union fields is valid. Since the u field is defined as anonymous field, it is now possible to access the members directly off the TYPEDESC instance. td.lptdesc and td.u.lptdesc are equivalent, but the former is faster since it does not need to create a temporary union instance:

td = TYPEDESC()
td.vt = VT_PTR
td.lptdesc = POINTER(some_type)
td.u.lptdesc = POINTER(some_type)

It is possible to define sub-subclasses of structures, they inherit the fields of the base class. If the subclass definition has a separate _fields_ variable, the fields specified in this are appended to the fields of the base class.

Structure and union constructors accept both positional and keyword arguments. Positional arguments are used to initialize member fields in the same order as they are appear in _fields_. Keyword arguments in the constructor are interpreted as attribute assignments, so they will initialize _fields_ with the same name, or create new attributes for names not present in _fields_.

Tableaux et pointeurs

class ctypes.Array(*args)

Classe de base abstraite pour les arrays.

The recommended way to create concrete array types is by multiplying any ctypes data type with a non-negative integer. Alternatively, you can subclass this type and define _length_ and _type_ class variables. Array elements can be read and written using standard subscript and slice accesses; for slice reads, the resulting object is not itself an Array.

_length_

A positive integer specifying the number of elements in the array. Out-of-range subscripts result in an IndexError. Will be returned by len().

_type_

Spécifie le type de chaque élément de l'array.

Array subclass constructors accept positional arguments, used to initialize the elements in order.

class ctypes._Pointer

Private, abstract base class for pointers.

Concrete pointer types are created by calling POINTER() with the type that will be pointed to; this is done automatically by pointer().

If a pointer points to an array, its elements can be read and written using standard subscript and slice accesses. Pointer objects have no size, so len() will raise TypeError. Negative subscripts will read from the memory before the pointer (as in C), and out-of-range subscripts will probably crash with an access violation (if you're lucky).

_type_

Specifies the type pointed to.

contents

Returns the object to which to pointer points. Assigning to this attribute changes the pointer to point to the assigned object.