Extending
Extending
Version 3.7.1
A Glossaire 73
C Histoire et licence 89
C.1 Histoire du logiciel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
C.2 Conditions générales pour accéder à, ou utiliser, Python . . . . . . . . . . . . . . . . . . . . . 90
C.3 Licences et Remerciements pour les logiciels inclus . . . . . . . . . . . . . . . . . . . . . . . . 93
D Copyright 107
Index 109
i
ii
Extending and Embedding Python, Version 3.7.1
Ce document décrit comment écrire des modules en C ou C++ pour étendre l’interpréteur Python à de
nouveaux modules. En plus de définir de nouvelles fonctions, ces modules peuvent définir de nouveaux types
d’objets ainsi que leur méthodes. Ce document explique aussi comment intégrer l’interpréteur Python dans
une autre application, pour être utilisé comme langage d’extension. Enfin, ce document montre comment
compiler et lier les modules d’extension pour qu’ils puissent être chargés dynamiquement (à l’exécution) dans
l’interpréteur, si le système d’exploitation sous-jacent supporte cette fonctionnalité.
Ce document présuppose que vous avez des connaissances de base sur Python. Pour une introduction in-
formelle du langage, voyez tutorial-index. reference-index donne une définition plus formelle du langage.
library-index documente les objets types, fonctions et modules existants (tous intégrés et écrits en Python)
qui donnent au langage sa large gamme d’applications.
Pour une description dans sa totalité de l’API Python/C, voir c-api-index.
Ce guide ne couvre que les outils basiques permettant de créer des extensions fournies dans cette version de
CPython. Les outils tiers tels que Cython, cffi, SWIG et Numba offrent des approches plus simples et plus
élaborées pour créer des extensions C et C++ pour Python.
Voir aussi :
3
Extending and Embedding Python, Version 3.7.1
Cette section du guide couvre la création d’extensions C et C++ sans l’utilisation d’outils tiers. Cette section
est destinée aux créateurs de ces outils, plus que d’être une méthode recommandée pour créer votre propre
extension C.
Note : L’interface d’extension C est spécifique à CPython, et les modules d’extension ne fonctionne pas sur
les autres implémentations de Python. Dans de nombreux cas, il est possible d’éviter la rédaction des exten-
sions en C et ainsi préserver la portabilité vers d’autres implémentations. Par exemple, si vous devez appeler
une fonction de la bibliothèque C ou faire un appel système, vous devriez envisager d’utiliser le module
ctypes ou d’utiliser la bibliothèque cffi plutôt que d’écrire du code C sur mesure. Ces modules vous per-
mettent d’écrire du code Python s’interfaçant avec le code C et sont plus portables entre les implémentations
de Python que l’écriture et la compilation d’une d’extension C.
5
Extending and Embedding Python, Version 3.7.1
Commencez par créer un fichier spammodule.c. (Historiquement, si un module se nomme spam, le fichier C
contenant son implémentation est appelé spammodule.c. Si le nom du module est très long, comme spammify,
le nom du module peut être juste spammify.c.)
La première ligne de notre fichier peut être :
#include <Python.h>
qui récupère l’API Python (vous pouvez ajouter un commentaire décrivant le but du module et un avis de
droit d’auteur si vous le souhaitez).
Note : Python pouvant définir certaines définitions pré-processeur qui affectent les têtes standard sur
certains systèmes, vous devez inclure Python.h avant les en-têtes standards.
Tous les symboles exposés par Python.h sont préfixés de Py ou PY, sauf ceux qui sont définis dans les en-têtes
standard. Pour le confort, et comme ils sont largement utilisés par l’interpréteur Python, "Python.h" inclut
lui même quelques d’en-têtes standard : <stdio.h>, <string.h>, <errno.h> et <stdlib.h>. Si ce dernier
n’existe pas sur votre système, il déclare les fonctions malloc(), free() et realloc() directement.
La prochaine chose que nous ajoutons à notre fichier de module est la fonction C qui sera appelée lorsque
l’expression Python [Link](chaîne) sera évaluée (nous verrons bientôt comment elle finit par être
appelée) :
static PyObject *
spam_system(PyObject *self, PyObject *args)
{
const char *command;
int sts;
Il y a une correspondance directe de la liste des arguments en Python (par exemple, l’expression "ls -l")
aux arguments passés à la fonction C. La fonction C a toujours deux arguments, appelés par convention self
et args.
Pour les fonctions au niveau du module, l’argument self pointe sur l’objet module, pour une méthode, il
pointe sur l’instance de l’objet.
L’argument args sera un pointeur vers un tuple Python contenant les arguments. Chaque élément du tuple
correspond à un argument dans la liste des arguments de l’appel. Les arguments sont des objets Python
1. An interface for this function already exists in the standard module os — it was chosen as a simple and straightforward
example.
— afin d’en faire quelque chose dans notre fonction C, nous devons les convertir en valeurs C. La fonction
PyArg_ParseTuple() de l’API Python vérifie les types des arguments et les convertit en valeurs C. Elle
utilise un modèle sous forme de chaîne pour déterminer les types requis des arguments ainsi que les types de
variables C dans lequel stocker les valeurs converties. Nous en verront plus, plus tard.
PyArg_ParseTuple() renvoie vrai (pas zéro) si tous les arguments ont le bon type et que ses composants
ont été stockés dans les variables dont les adresses données. Il renvoie faux (zéro) si une liste d’arguments
invalide a été passée. Dans ce dernier cas, elle lève également une exception appropriée de sorte que la
fonction d’appel puisse renvoyer NULL immédiatement (comme nous l’avons vu dans l’exemple).
les fonctions construisant des objets (tels que PyLong_FromLong()) le font déjà, donc cette note ne concerne
que ceux qui appellent malloc() directement.
Notez également que, à l’exception notable de PyArg_ParseTuple() et compagnie, les fonctions qui renvoient
leur statut sous forme d’entier donnent généralement une valeur positive ou zéro en cas de succès et -1 en
cas d’échec, comme les appels du système Unix.
Enfin, lorsque vous renvoyez un code d’erreur, n’oubliez pas faire un brin de nettoyage (en appelant
Py_XDECREF() ou Py_DECREF() avec les objets que vous auriez déjà créés) !
Le choix de l’exception à lever vous incombe. Il existe des objets C correspondant à chaque exception Python,
tel que PyExc_ZeroDivisionError, que vous pouvez utiliser directement. Choisissez judicieusement vos
exceptions, typiquement n’utilisez pas PyExc_TypeError pour indiquer qu’un fichier n’a pas pu être ouvert
(qui devrait probablement être PyExc_IOError). Si quelque chose ne va pas avec la liste des arguments, la
fonction PyArg_ParseTuple() lève habituellement une exception PyExc_TypeError. Mais si vous avez un
argument dont la valeur doit être dans un intervalle particulier ou qui doit satisfaire d’autres conditions,
PyExc_ValueError sera plus appropriée.
Vous pouvez également créer une exception spécifique à votre module. Pour cela, déclarez simplement une
variable statique au début de votre fichier :
et initialisez-la dans la fonction d’initialisation de votre module (PyInit_spam()) avec un objet exception
(Passons, pour le moment, la vérification des codes d’erreur) :
PyMODINIT_FUNC
PyInit_spam(void)
{
PyObject *m;
m = PyModule_Create(&spammodule);
if (m == NULL)
return NULL;
Notez que le nom de exception, côté Python, est [Link]. La fonction PyErr_NewException() peut
créer une classe héritant de Exception (à moins qu’une autre classe ne lui soit fournie à la place de NULL),
voir bltin-exceptions.
Notez également que la variable SpamError contient une référence à la nouvelle classe créée ; ceci est in-
tentionnel ! Comme l’exception peut être retirée du module par un code externe, une référence à la classe
est nécessaire pour assurer qu’il ne sera pas rejeté, causant SpamError à devenir un pointeur défaillant. S’il
devenait un pointeur défaillant, le C code qui lève l’exception peut engendrer un rejet central ou des effets
secondaires inattendus.
Nous traiterons de l’utilisation de PyMODINIT_FUNC comme un type de retour de fonction plus tard dans
cette section.
L’exception [Link] peut être levée dans votre module d’extension en appelant PyErr_SetString()
comme montré ci-dessous :
static PyObject *
spam_system(PyObject *self, PyObject *args)
{
const char *command;
int sts;
Elle renvoie NULL (l’indicateur d’erreur pour les fonctions renvoyant des pointeurs d’objet) si une erreur est
détectée dans la liste des arguments,se fiant à l’exception définie par PyArg_ParseTuple(). Autrement,la
valeur chaîne de l’argument a été copiée dans la variable locale command. Il s’agit d’une attribution de
pointeur et vous n’êtes pas supposés modifier la chaîne qui vers laquelle il pointe (donc en C Standard, la
variable command doit être clairement déclarée comme const char *command).
La prochaine instruction est un appel à la fonction Unix system(), en lui passant la chaîne que nous venons
d’obtenir à partir de PyArg_ParseTuple() :
sts = system(command);
Notre fonction [Link]() doit renvoyer la valeur de sts comme un objet Python. Cela est effectué par
l’utilisation de la fonction PyLong_FromLong().
return PyLong_FromLong(sts);
Dans ce cas, elle renverra un objet entier. (Oui, même les entiers sont des objets dans le tas en Python !)
Si vous avez une fonction C qui ne renvoie aucun argument utile (une fonction renvoyant void), la fonction
Python correspondante doit renvoyer None. Vous aurez besoin de cette locution pour cela (qui est implémentée
par la macro Py_RETURN_NONE) :
Py_INCREF(Py_None);
return Py_None;
Py_None est le nom C pour l’objet spécial Python None. C’est un authentique objet Python plutôt qu’un
pointeur NULL, qui signifie qu’une erreur est survenue, dans la plupart des situations, comme nous l’avons
vu.
Note the third entry (METH_VARARGS). This is a flag telling the interpreter the calling convention to be used
for the C function. It should normally always be METH_VARARGS or METH_VARARGS | METH_KEYWORDS ; a value
of 0 means that an obsolete variant of PyArg_ParseTuple() is used.
When using only METH_VARARGS, the function should expect the Python-level parameters to be passed in
as a tuple acceptable for parsing via PyArg_ParseTuple() ; more information on this function is provided
below.
The METH_KEYWORDS bit may be set in the third field if keyword arguments should be passed to the function.
In this case, the C function should accept a third PyObject * parameter which will be a dictionary of
keywords. Use PyArg_ParseTupleAndKeywords() to parse the arguments to such a function.
The method table must be referenced in the module definition structure :
This structure, in turn, must be passed to the interpreter in the module’s initialization function. The initia-
lization function must be named PyInit_name(), where name is the name of the module, and should be the
only non-static item defined in the module file :
PyMODINIT_FUNC
PyInit_spam(void)
{
return PyModule_Create(&spammodule);
}
Note that PyMODINIT_FUNC declares the function as PyObject * return type, declares any special linkage
declarations required by the platform, and for C++ declares the function as extern "C".
When the Python program imports module spam for the first time, PyInit_spam() is called. (See below
for comments about embedding Python.) It calls PyModule_Create(), which returns a module object, and
inserts built-in function objects into the newly created module based upon the table (an array of PyMethodDef
structures) found in the module definition. PyModule_Create() returns a pointer to the module object that
it creates. It may abort with a fatal error for certain errors, or return NULL if the module could not be
initialized satisfactorily. The init function must return the module object to its caller, so that it then gets
inserted into [Link].
When embedding Python, the PyInit_spam() function is not called automatically unless there’s an entry in
the PyImport_Inittab table. To add the module to the initialization table, use PyImport_AppendInittab(),
optionally followed by an import of the module :
int
main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
...
PyMem_RawFree(program);
return 0;
}
Note : Removing entries from [Link] or importing compiled modules into multiple interpreters within
a process (or following a fork() without an intervening exec()) can create problems for some extension
modules. Extension module authors should exercise caution when initializing internal data structures.
A more substantial example module is included in the Python source distribution as Modules/xxmodule.c.
This file may be used as a template or simply read as an example.
Note : Unlike our spam example, xxmodule uses multi-phase initialization (new in Python 3.5), where
a PyModuleDef structure is returned from PyInit_spam, and creation of the module is left to the import
machinery. For details on multi-phase initialization, see PEP 489.
If you can’t use dynamic loading, or if you want to make your module a permanent part of the Python
interpreter, you will have to change the configuration setup and rebuild the interpreter. Luckily, this is very
simple on Unix : just place your file (spammodule.c for example) in the Modules/ directory of an unpacked
source distribution, add a line to the file Modules/[Link] describing your file :
spam spammodule.o
and rebuild the interpreter by running make in the toplevel directory. You can also run make in the Modules/
subdirectory, but then you must first rebuild Makefile there by running “make Makefile”. (This is necessary
each time you change the Setup file.)
If your module requires additional libraries to link with, these can be listed on the line in the configuration
file as well, for instance :
static PyObject *
my_set_callback(PyObject *dummy, PyObject *args)
{
PyObject *result = NULL;
PyObject *temp;
This function must be registered with the interpreter using the METH_VARARGS flag ; this is described in
section The Module’s Method Table and Initialization Function. The PyArg_ParseTuple() function and its
arguments are documented in section Extracting Parameters in Extension Functions.
The macros Py_XINCREF() and Py_XDECREF() increment/decrement the reference count of an object and
are safe in the presence of NULL pointers (but note that temp will not be NULL in this context). More info
on them in section Reference Counts.
Later, when it is time to call the function, you call the C function PyObject_CallObject(). This function
has two arguments, both pointers to arbitrary Python objects : the Python function, and the argument list.
The argument list must always be a tuple object, whose length is the number of arguments. To call the
Python function with no arguments, pass in NULL, or an empty tuple ; to call it with one argument, pass
a singleton tuple. Py_BuildValue() returns a tuple when its format string consists of zero or more format
codes between parentheses. For example :
int arg;
PyObject *arglist;
PyObject *result;
...
arg = 123;
...
/* Time to call the callback */
arglist = Py_BuildValue("(i)", arg);
result = PyObject_CallObject(my_callback, arglist);
Py_DECREF(arglist);
PyObject_CallObject() returns a Python object pointer : this is the return value of the Python func-
tion. PyObject_CallObject() is « reference-count-neutral » with respect to its arguments. In the example
a new tuple was created to serve as the argument list, which is Py_DECREF()-ed immediately after the
PyObject_CallObject() call.
The return value of PyObject_CallObject() is « new » : either it is a brand new object, or it is an existing
object whose reference count has been incremented. So, unless you want to save it in a global variable, you
should somehow Py_DECREF() the result, even (especially !) if you are not interested in its value.
Before you do this, however, it is important to check that the return value isn’t NULL. If it is, the Python
function terminated by raising an exception. If the C code that called PyObject_CallObject() is called
from Python, it should now return an error indication to its Python caller, so the interpreter can print a
stack trace, or the calling Python code can handle the exception. If this is not possible or desirable, the
exception should be cleared by calling PyErr_Clear(). For example :
if (result == NULL)
return NULL; /* Pass error back */
...use result...
Py_DECREF(result);
Depending on the desired interface to the Python callback function, you may also have to provide an argument
list to PyObject_CallObject(). In some cases the argument list is also provided by the Python program,
through the same interface that specified the callback function. It can then be saved and used in the same
manner as the function object. In other cases, you may have to construct a new tuple to pass as the argument
list. The simplest way to do this is to call Py_BuildValue(). For example, if you want to pass an integral
event code, you might use the following code :
PyObject *arglist;
...
arglist = Py_BuildValue("(l)", eventcode);
result = PyObject_CallObject(my_callback, arglist);
Py_DECREF(arglist);
if (result == NULL)
return NULL; /* Pass error back */
/* Here maybe use the result */
Py_DECREF(result);
Note the placement of Py_DECREF(arglist) immediately after the call, before the error check ! Also note
that strictly speaking this code is not complete : Py_BuildValue() may run out of memory, and this should
be checked.
You may also call a function with keyword arguments by using PyObject_Call(), which supports arguments
and keyword arguments. As in the above example, we use Py_BuildValue() to construct the dictionary.
PyObject *dict;
...
dict = Py_BuildValue("{s:i}", "name", val);
result = PyObject_Call(my_callback, NULL, dict);
Py_DECREF(dict);
if (result == NULL)
return NULL; /* Pass error back */
/* Here maybe use the result */
Py_DECREF(result);
The arg argument must be a tuple object containing an argument list passed from Python to a C function.
The format argument must be a format string, whose syntax is explained in arg-parsing in the Python/C
API Reference Manual. The remaining arguments must be addresses of variables whose type is determined
by the format string.
Note that while PyArg_ParseTuple() checks that the Python arguments have the required types, it cannot
check the validity of the addresses of C variables passed to the call : if you make mistakes there, your code
will probably crash or at least overwrite random bits in memory. So be careful !
Notez que n’importe quelles références sur un objet Python qui sont données à l’appelant sont des références
empruntées ; ne décrémentez pas leur compteur de références !
Some example calls :
int ok;
int i, j;
long k, l;
const char *s;
(suite sur la page suivante)
{
const char *file;
const char *mode = "r";
int bufsize = 0;
ok = PyArg_ParseTuple(args, "s|si", &file, &mode, &bufsize);
/* A string, and optionally another string and an integer */
/* Possible Python calls:
f('spam')
f('spam', 'w')
f('spam', 'wb', 100000) */
}
{
int left, top, right, bottom, h, v;
ok = PyArg_ParseTuple(args, "((ii)(ii))(ii)",
&left, &top, &right, &bottom, &h, &v);
/* A rectangle and a point */
/* Possible Python call:
f(((0, 0), (400, 300)), (10, 10)) */
}
{
Py_complex c;
ok = PyArg_ParseTuple(args, "D:myfunction", &c);
/* a complex, also providing a function name for errors */
/* Possible Python call: myfunction(1+2j) */
}
The arg and format parameters are identical to those of the PyArg_ParseTuple() function. The kwdict
parameter is the dictionary of keywords received as the third parameter from the Python runtime. The
kwlist parameter is a NULL-terminated list of strings which identify the parameters ; the names are matched
with the type information from format from left to right. On success, PyArg_ParseTupleAndKeywords()
returns true, otherwise it returns false and raises an appropriate exception.
Note : Nested tuples cannot be parsed when using keyword arguments ! Keyword parameters passed in
which are not present in the kwlist will cause TypeError to be raised.
Here is an example module which uses keywords, based on an example by Geoff Philbrick (phil-
brick@[Link]) :
#include "Python.h"
static PyObject *
keywdarg_parrot(PyObject *self, PyObject *args, PyObject *keywds)
{
int voltage;
const char *state = "a stiff";
const char *action = "voom";
const char *type = "Norwegian Blue";
Py_RETURN_NONE;
}
It recognizes a set of format units similar to the ones recognized by PyArg_ParseTuple(), but the arguments
(which are input to the function, not output) must not be pointers, just values. It returns a new Python
object, suitable for returning from a C function called from Python.
One difference with PyArg_ParseTuple() : while the latter requires its first argument to be a tuple (since
Python argument lists are always represented as tuples internally), Py_BuildValue() does not always build
a tuple. It builds a tuple only if its format string contains two or more format units. If the format string is
empty, it returns None ; if it contains exactly one format unit, it returns whatever object is described by that
format unit. To force it to return a tuple of size 0 or one, parenthesize the format string.
Examples (to the left the call, to the right the resulting Python value) :
Py_BuildValue("") None
Py_BuildValue("i", 123) 123
Py_BuildValue("iii", 123, 456, 789) (123, 456, 789)
Py_BuildValue("s", "hello") 'hello'
Py_BuildValue("y", "hello") b'hello'
Py_BuildValue("ss", "hello", "world") ('hello', 'world')
Py_BuildValue("s#", "hello", 4) 'hell'
Py_BuildValue("y#", "hello", 4) b'hell'
Py_BuildValue("()") ()
Py_BuildValue("(i)", 123) (123,)
Py_BuildValue("(ii)", 123, 456) (123, 456)
Py_BuildValue("(i,i)", 123, 456) (123, 456)
Py_BuildValue("[i,i]", 123, 456) [123, 456]
Py_BuildValue("{s:i,s:i}",
"abc", 123, "def", 456) {'abc': 123, 'def': 456}
Py_BuildValue("((ii)(ii)) (ii)",
1, 2, 3, 4, 5, 6) (((1, 2), (3, 4)), (5, 6))
is called using freed memory. It has the same bad consequences as referencing uninitialized data — core
dumps, wrong results, mysterious crashes.
Common causes of memory leaks are unusual paths through the code. For instance, a function may allocate
a block of memory, do some calculation, and then free the block again. Now a change in the requirements for
the function may add a test to the calculation that detects an error condition and can return prematurely
from the function. It’s easy to forget to free the allocated memory block when taking this premature exit,
especially when it is added later to the code. Such leaks, once introduced, often go undetected for a long
time : the error exit is taken only in a small fraction of all calls, and most modern machines have plenty of
virtual memory, so the leak only becomes apparent in a long-running process that uses the leaking function
frequently. Therefore, it’s important to prevent leaks from happening by having a coding convention or
strategy that minimizes this kind of errors.
Since Python makes heavy use of malloc() and free(), it needs a strategy to avoid memory leaks as well
as the use of freed memory. The chosen method is called reference counting. The principle is simple : every
object contains a counter, which is incremented when a reference to the object is stored somewhere, and
which is decremented when a reference to it is deleted. When the counter reaches zero, the last reference to
the object has been deleted and the object is freed.
An alternative strategy is called automatic garbage collection. (Sometimes, reference counting is also refer-
red to as a garbage collection strategy, hence my use of « automatic » to distinguish the two.) The big
advantage of automatic garbage collection is that the user doesn’t need to call free() explicitly. (Another
claimed advantage is an improvement in speed or memory usage — this is no hard fact however.) The di-
sadvantage is that for C, there is no truly portable automatic garbage collector, while reference counting
can be implemented portably (as long as the functions malloc() and free() are available — which the C
Standard guarantees). Maybe some day a sufficiently portable automatic garbage collector will be available
for C. Until then, we’ll have to live with reference counts.
While Python uses the traditional reference counting implementation, it also offers a cycle detector that
works to detect reference cycles. This allows applications to not worry about creating direct or indirect
circular references ; these are the weakness of garbage collection implemented using only reference counting.
Reference cycles consist of objects which contain (possibly indirect) references to themselves, so that each
object in the cycle has a reference count which is non-zero. Typical reference counting implementations are
not able to reclaim the memory belonging to any objects in a reference cycle, or referenced from the objects
in the cycle, even though there are no further references to the cycle itself.
The cycle detector is able to detect garbage cycles and can reclaim them. The gc module exposes a way
to run the detector (the collect() function), as well as configuration interfaces and the ability to disable
the detector at runtime. The cycle detector is considered an optional component ; though it is included by
default, it can be disabled at build time using the --without-cycle-gc option to the configure script on
Unix platforms (including Mac OS X). If the cycle detector is disabled in this way, the gc module will not
be available.
It is also possible to borrow 2 a reference to an object. The borrower of a reference should not call
Py_DECREF(). The borrower must not hold on to the object longer than the owner from which it was borro-
wed. Using a borrowed reference after the owner has disposed of it risks using freed memory and should be
avoided completely 3 .
The advantage of borrowing over owning a reference is that you don’t need to take care of disposing of the
reference on all possible paths through the code — in other words, with a borrowed reference you don’t run
the risk of leaking when a premature exit is taken. The disadvantage of borrowing over owning is that there
are some subtle situations where in seemingly correct code a borrowed reference can be used after the owner
from which it was borrowed has in fact disposed of it.
A borrowed reference can be changed into an owned reference by calling Py_INCREF(). This does not affect
the status of the owner from which the reference was borrowed — it creates a new owned reference, and
gives full owner responsibilities (the new owner must dispose of the reference properly, as well as the previous
owner).
Ownership Rules
Whenever an object reference is passed into or out of a function, it is part of the function’s interface
specification whether ownership is transferred with the reference or not.
Most functions that return a reference to an object pass on ownership with the reference. In particular, all
functions whose function it is to create a new object, such as PyLong_FromLong() and Py_BuildValue(),
pass ownership to the receiver. Even if the object is not actually new, you still receive ownership of a new
reference to that object. For instance, PyLong_FromLong() maintains a cache of popular values and can
return a reference to a cached item.
Many functions that extract objects from other objects also transfer ownership with the reference, for instance
PyObject_GetAttrString(). The picture is less clear, here, however, since a few common routines are
exceptions : PyTuple_GetItem(), PyList_GetItem(), PyDict_GetItem(), and PyDict_GetItemString()
all return references that you borrow from the tuple, list or dictionary.
The function PyImport_AddModule() also returns a borrowed reference, even though it may actually create
the object it returns : this is possible because an owned reference to the object is stored in [Link].
When you pass an object reference into another function, in general, the function borrows the reference from
you — if it needs to store it, it will use Py_INCREF() to become an independent owner. There are exactly
two important exceptions to this rule : PyTuple_SetItem() and PyList_SetItem(). These functions take
over ownership of the item passed to them — even if they fail ! (Note that PyDict_SetItem() and friends
don’t take over ownership — they are « normal. »)
When a C function is called from Python, it borrows references to its arguments from the caller. The caller
owns a reference to the object, so the borrowed reference’s lifetime is guaranteed until the function returns.
Only when such a borrowed reference must be stored or passed on, it must be turned into an owned reference
by calling Py_INCREF().
The object reference returned from a C function that is called from Python must be an owned reference —
ownership is transferred from the function to its caller.
Thin Ice
There are a few situations where seemingly harmless use of a borrowed reference can lead to problems. These
all have to do with implicit invocations of the interpreter, which can cause the owner of a reference to dispose
of it.
2. The metaphor of « borrowing » a reference is not completely correct : the owner still has a copy of the reference.
3. Checking that the reference count is at least 1 does not work — the reference count itself could be in freed memory
and may thus be reused for another object !
The first and most important case to know about is using Py_DECREF() on an unrelated object while borro-
wing a reference to a list item. For instance :
void
bug(PyObject *list)
{
PyObject *item = PyList_GetItem(list, 0);
PyList_SetItem(list, 1, PyLong_FromLong(0L));
PyObject_Print(item, stdout, 0); /* BUG! */
}
This function first borrows a reference to list[0], then replaces list[1] with the value 0, and finally prints
the borrowed reference. Looks harmless, right ? But it’s not !
Let’s follow the control flow into PyList_SetItem(). The list owns references to all its items, so when item
1 is replaced, it has to dispose of the original item 1. Now let’s suppose the original item 1 was an instance
of a user-defined class, and let’s further suppose that the class defined a __del__() method. If this class
instance has a reference count of 1, disposing of it will call its __del__() method.
Since it is written in Python, the __del__() method can execute arbitrary Python code. Could it perhaps
do something to invalidate the reference to item in bug() ? You bet ! Assuming that the list passed into
bug() is accessible to the __del__() method, it could execute a statement to the effect of del list[0],
and assuming this was the last reference to that object, it would free the memory associated with it, thereby
invalidating item.
The solution, once you know the source of the problem, is easy : temporarily increment the reference count.
The correct version of the function reads :
void
no_bug(PyObject *list)
{
PyObject *item = PyList_GetItem(list, 0);
Py_INCREF(item);
PyList_SetItem(list, 1, PyLong_FromLong(0L));
PyObject_Print(item, stdout, 0);
Py_DECREF(item);
}
This is a true story. An older version of Python contained variants of this bug and someone spent a consi-
derable amount of time in a C debugger to figure out why his __del__() methods would fail…
The second case of problems with a borrowed reference is a variant involving threads. Normally, multiple
threads in the Python interpreter can’t get in each other’s way, because there is a global lock protec-
ting Python’s entire object space. However, it is possible to temporarily release this lock using the macro
Py_BEGIN_ALLOW_THREADS, and to re-acquire it using Py_END_ALLOW_THREADS. This is common around blo-
cking I/O calls, to let other threads use the processor while waiting for the I/O to complete. Obviously, the
following function has the same problem as the previous one :
void
bug(PyObject *list)
{
PyObject *item = PyList_GetItem(list, 0);
Py_BEGIN_ALLOW_THREADS
...some blocking I/O call...
(suite sur la page suivante)
NULL Pointers
In general, functions that take object references as arguments do not expect you to pass them NULL pointers,
and will dump core (or cause later core dumps) if you do so. Functions that return object references generally
return NULL only to indicate that an exception occurred. The reason for not testing for NULL arguments
is that functions often pass the objects they receive on to other function — if each function were to test for
NULL, there would be a lot of redundant tests and the code would run more slowly.
It is better to test for NULL only at the « source : » when a pointer that may be NULL is received, for
example, from malloc() or from a function that may raise an exception.
The macros Py_INCREF() and Py_DECREF() do not check for NULL pointers — however, their variants
Py_XINCREF() and Py_XDECREF() do.
The macros for checking for a particular object type (Pytype_Check()) don’t check for NULL pointers —
again, there is much code that calls several of these in a row to test an object against various different
expected types, and this would generate redundant tests. There are no variants with NULL checking.
The C function calling mechanism guarantees that the argument list passed to C functions (args in the
examples) is never NULL — in fact it guarantees that it is always a tuple 4 .
It is a severe error to ever let a NULL pointer « escape » to the Python user.
in order to avoid name clashes with other extension modules (as discussed in section The Module’s Method
Table and Initialization Function). And it means that symbols that should be accessible from other extension
modules must be exported in a different way.
Python provides a special mechanism to pass C-level information (pointers) from one extension module to
another one : Capsules. A Capsule is a Python data type which stores a pointer (void *). Capsules can only
be created and accessed via their C API, but they can be passed around like any other Python object. In
particular, they can be assigned to a name in an extension module’s namespace. Other extension modules
can then import this module, retrieve the value of this name, and then retrieve the pointer from the Capsule.
There are many ways in which Capsules can be used to export the C API of an extension module. Each
function could get its own Capsule, or all C API pointers could be stored in an array whose address is
published in a Capsule. And the various tasks of storing and retrieving the pointers can be distributed in
different ways between the module providing the code and the client modules.
Whichever method you choose, it’s important to name your Capsules properly. The function
PyCapsule_New() takes a name parameter (const char *) ; you’re permitted to pass in a NULL name,
but we strongly encourage you to specify a name. Properly named Capsules provide a degree of runtime
type-safety ; there is no feasible way to tell one unnamed Capsule from another.
In particular, Capsules used to expose C APIs should be given a name following this convention :
[Link]
The convenience function PyCapsule_Import() makes it easy to load a C API provided via a Capsule,
but only if the Capsule’s name matches this convention. This behavior gives C API users a high degree of
certainty that the Capsule they load contains the correct C API.
The following example demonstrates an approach that puts most of the burden on the writer of the exporting
module, which is appropriate for commonly used library modules. It stores all C API pointers (just one in the
example !) in an array of void pointers which becomes the value of a Capsule. The header file corresponding
to the module provides a macro that takes care of importing the module and retrieving its C API pointers ;
client modules only have to call this macro before accessing the C API.
The exporting module is a modification of the spam module from section Un exemple simple. The function
[Link]() does not call the C library function system() directly, but a function PySpam_System(),
which would of course do something more complicated in reality (such as adding « spam » to every command).
This function PySpam_System() is also exported to other extension modules.
The function PySpam_System() is a plain C function, declared static like everything else :
static int
PySpam_System(const char *command)
{
return system(command);
}
static PyObject *
spam_system(PyObject *self, PyObject *args)
{
const char *command;
int sts;
#include "Python.h"
#define SPAM_MODULE
#include "spammodule.h"
The #define is used to tell the header file that it is being included in the exporting module, not a client
module. Finally, the module’s initialization function must take care of initializing the C API pointer array :
PyMODINIT_FUNC
PyInit_spam(void)
{
PyObject *m;
static void *PySpam_API[PySpam_API_pointers];
PyObject *c_api_object;
m = PyModule_Create(&spammodule);
if (m == NULL)
return NULL;
if (c_api_object != NULL)
PyModule_AddObject(m, "_C_API", c_api_object);
return m;
}
Note that PySpam_API is declared static ; otherwise the pointer array would disappear when PyInit_spam()
terminates !
The bulk of the work is in the header file spammodule.h, which looks like this :
#ifndef Py_SPAMMODULE_H
#define Py_SPAMMODULE_H
#ifdef __cplusplus
extern "C" {
#endif
/* C API functions */
#define PySpam_System_NUM 0
(suite sur la page suivante)
#ifdef SPAM_MODULE
/* This section is used when compiling spammodule.c */
#else
/* This section is used in modules that use spammodule's API */
#define PySpam_System \
(*(PySpam_System_RETURN (*)PySpam_System_PROTO) PySpam_API[PySpam_System_NUM])
#endif
#ifdef __cplusplus
}
#endif
#endif /* !defined(Py_SPAMMODULE_H) */
All that a client module must do in order to have access to the function PySpam_System() is to call the
function (or rather macro) import_spam() in its initialization function :
PyMODINIT_FUNC
PyInit_client(void)
{
PyObject *m;
m = PyModule_Create(&clientmodule);
if (m == NULL)
return NULL;
if (import_spam() < 0)
return NULL;
/* additional initialization can happen here */
(suite sur la page suivante)
The main disadvantage of this approach is that the file spammodule.h is rather complicated. However, the
basic structure is the same for each function that is exported, so it has to be learned only once.
Finally it should be mentioned that Capsules offer additional functionality, which is especially useful for
memory allocation and deallocation of the pointer stored in a Capsule. The details are described in the Py-
thon/C API Reference Manual in the section capsules and in the implementation of Capsules (files Include/
pycapsule.h and Objects/pycapsule.c in the Python source code distribution).
Notes
Note : What we’re showing here is the traditional way of defining static extension types. It should
be adequate for most uses. The C API also allows defining heap-allocated extension types using the
PyType_FromSpec() function, which isn’t covered in this tutorial.
#include <Python.h>
typedef struct {
PyObject_HEAD
/* Type-specific fields go here. */
} CustomObject;
PyMODINIT_FUNC
PyInit_custom(void)
{
PyObject *m;
if (PyType_Ready(&CustomType) < 0)
return NULL;
m = PyModule_Create(&custommodule);
if (m == NULL)
return NULL;
Py_INCREF(&CustomType);
PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
return m;
}
Now that’s quite a bit to take in at once, but hopefully bits will seem familiar from the previous chapter.
This file defines three things :
1. What a Custom object contains : this is the CustomObject struct, which is allocated once for each
Custom instance.
2. How the Custom type behaves : this is the CustomType struct, which defines a set of flags and function
pointers that the interpreter inspects when specific operations are requested.
3. How to initialize the custom module : this is the PyInit_custom function and the associated
custommodule struct.
The first bit is :
typedef struct {
PyObject_HEAD
} CustomObject;
This is what a Custom object will contain. PyObject_HEAD is mandatory at the start of each object struct
and defines a field called ob_base of type PyObject, containing a pointer to a type object and a reference
count (these can be accessed using the macros Py_REFCNT and Py_TYPE respectively). The reason for the
macro is to abstract away the layout and to enable additional fields in debug builds.
Note : There is no semicolon above after the PyObject_HEAD macro. Be wary of adding one by accident :
some compilers will complain.
Of course, objects generally store additional data besides the standard PyObject_HEAD boilerplate ; for
example, here is the definition for standard Python floats :
typedef struct {
PyObject_HEAD
double ob_fval;
} PyFloatObject;
Note : We recommend using C99-style designated initializers as above, to avoid listing all the PyTypeObject
fields that you don’t care about and also to avoid caring about the fields” declaration order.
The actual definition of PyTypeObject in object.h has many more fields than the definition above. The
remaining fields will be filled with zeros by the C compiler, and it’s common practice to not specify them
explicitly unless you need them.
We’re going to pick it apart, one field at a time :
PyVarObject_HEAD_INIT(NULL, 0)
This line is mandatory boilerplate to initialize the ob_base field mentioned above.
.tp_name = "[Link]",
The name of our type. This will appear in the default textual representation of our objects and in some error
messages, for example :
Note that the name is a dotted name that includes both the module name and the name of the type within
the module. The module in this case is custom and the type is Custom, so we set the type name to custom.
Custom. Using the real dotted import path is important to make your type compatible with the pydoc and
pickle modules.
.tp_basicsize = sizeof(CustomObject),
.tp_itemsize = 0,
This is so that Python knows how much memory to allocate when creating new Custom instances.
tp_itemsize is only used for variable-sized objects and should otherwise be zero.
Note : If you want your type to be subclassable from Python, and your type has the same tp_basicsize as
its base type, you may have problems with multiple inheritance. A Python subclass of your type will have to
list your type first in its __bases__, or else it will not be able to call your type’s __new__() method without
getting an error. You can avoid this problem by ensuring that your type has a larger value for tp_basicsize
than its base type does. Most of the time, this will be true anyway, because either your base type will be
object, or else you will be adding data members to your base type, and therefore increasing its size.
All types should include this constant in their flags. It enables all of the members defined until at least
Python 3.3. If you need further members, you will need to OR the corresponding flags.
We provide a doc string for the type in tp_doc.
.tp_doc = "Custom objects",
To enable object creation, we have to provide a tp_new handler. This is the equivalent of the Python method
__new__(), but has to be specified explicitly. In this case, we can just use the default implementation
provided by the API function PyType_GenericNew().
.tp_new = PyType_GenericNew,
Everything else in the file should be familiar, except for some code in PyInit_custom() :
if (PyType_Ready(&CustomType) < 0)
return;
This initializes the Custom type, filling in a number of members to the appropriate default values, including
ob_type that we initially set to NULL.
PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
This adds the type to the module dictionary. This allows us to create Custom instances by calling the Custom
class :
>>> import custom
>>> mycustom = [Link]()
That’s it ! All that remains is to build it ; put the above code in a file called custom.c and :
from [Link] import setup, Extension
setup(name="custom", version="1.0",
ext_modules=[Extension("custom", ["custom.c"])])
at a shell should produce a file [Link] in a subdirectory ; move to that directory and fire up Python —
you should be able to import custom and play around with Custom objects.
That wasn’t so hard, was it ?
Of course, the current Custom type is pretty uninteresting. It has no data and doesn’t do anything. It can’t
even be subclassed.
Note : While this documentation showcases the standard distutils module for building C extensions,
it is recommended in real-world use cases to use the newer and better-maintained setuptools library.
Documentation on how to do this is out of scope for this document and can be found in the Python
Packaging User’s Guide.
#include <Python.h>
#include "structmember.h"
typedef struct {
PyObject_HEAD
PyObject *first; /* first name */
PyObject *last; /* last name */
int number;
} CustomObject;
static void
Custom_dealloc(CustomObject *self)
{
Py_XDECREF(self->first);
Py_XDECREF(self->last);
Py_TYPE(self)->tp_free((PyObject *) self);
}
static PyObject *
Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
CustomObject *self;
self = (CustomObject *) type->tp_alloc(type, 0);
if (self != NULL) {
self->first = PyUnicode_FromString("");
if (self->first == NULL) {
Py_DECREF(self);
return NULL;
}
self->last = PyUnicode_FromString("");
if (self->last == NULL) {
Py_DECREF(self);
return NULL;
}
self->number = 0;
}
return (PyObject *) self;
}
static int
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"first", "last", "number", NULL};
PyObject *first = NULL, *last = NULL, *tmp;
(suite sur la page suivante)
if (first) {
tmp = self->first;
Py_INCREF(first);
self->first = first;
Py_XDECREF(tmp);
}
if (last) {
tmp = self->last;
Py_INCREF(last);
self->last = last;
Py_XDECREF(tmp);
}
return 0;
}
static PyObject *
Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
{
if (self->first == NULL) {
PyErr_SetString(PyExc_AttributeError, "first");
return NULL;
}
if (self->last == NULL) {
PyErr_SetString(PyExc_AttributeError, "last");
return NULL;
}
return PyUnicode_FromFormat("%S %S", self->first, self->last);
}
PyMODINIT_FUNC
PyInit_custom2(void)
{
PyObject *m;
if (PyType_Ready(&CustomType) < 0)
return NULL;
m = PyModule_Create(&custommodule);
if (m == NULL)
return NULL;
Py_INCREF(&CustomType);
PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
return m;
}
This include provides declarations that we use to handle attributes, as described a bit later.
The Custom type now has three data attributes in its C struct, first, last, and number. The first and last
variables are Python strings containing first and last names. The number attribute is a C integer.
The object structure is updated accordingly :
typedef struct {
PyObject_HEAD
PyObject *first; /* first name */
PyObject *last; /* last name */
(suite sur la page suivante)
Because we now have data to manage, we have to be more careful about object allocation and deallocation.
At a minimum, we need a deallocation method :
static void
Custom_dealloc(CustomObject *self)
{
Py_XDECREF(self->first);
Py_XDECREF(self->last);
Py_TYPE(self)->tp_free((PyObject *) self);
}
This method first clears the reference counts of the two Python attributes. Py_XDECREF() correctly handles
the case where its argument is NULL (which might happen here if tp_new failed midway). It then calls the
tp_free member of the object’s type (computed by Py_TYPE(self)) to free the object’s memory. Note that
the object’s type might not be CustomType, because the object may be an instance of a subclass.
Note : The explicit cast to destructor above is needed because we defined Custom_dealloc to take a
CustomObject * argument, but the tp_dealloc function pointer expects to receive a PyObject * argument.
Otherwise, the compiler will emit a warning. This is object-oriented polymorphism, in C !
We want to make sure that the first and last names are initialized to empty strings, so we provide a tp_new
implementation :
static PyObject *
Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
CustomObject *self;
self = (CustomObject *) type->tp_alloc(type, 0);
if (self != NULL) {
self->first = PyUnicode_FromString("");
if (self->first == NULL) {
Py_DECREF(self);
return NULL;
}
self->last = PyUnicode_FromString("");
if (self->last == NULL) {
Py_DECREF(self);
return NULL;
}
self->number = 0;
}
return (PyObject *) self;
}
.tp_new = Custom_new,
The tp_new handler is responsible for creating (as opposed to initializing) objects of the type. It is exposed in
Python as the __new__() method. It is not required to define a tp_new member, and indeed many extension
types will simply reuse PyType_GenericNew() as done in the first version of the Custom type above. In this
case, we use the tp_new handler to initialize the first and last attributes to non-NULL default values.
tp_new is passed the type being instantiated (not necessarily CustomType, if a subclass is instantiated) and
any arguments passed when the type was called, and is expected to return the instance created. tp_new
handlers always accept positional and keyword arguments, but they often ignore the arguments, leaving the
argument handling to initializer (a.k.a. tp_init in C or __init__ in Python) methods.
Note : tp_new shouldn’t call tp_init explicitly, as the interpreter will do it itself.
Since memory allocation may fail, we must check the tp_alloc result against NULL before proceeding.
Note : We didn’t fill the tp_alloc slot ourselves. Rather PyType_Ready() fills it for us by inheriting it
from our base class, which is object by default. Most types use the default allocation strategy.
Note : If you are creating a co-operative tp_new (one that calls a base type’s tp_new or __new__()), you
must not try to determine what method to call using method resolution order at runtime. Always statically
determine what type you are going to call, and call its tp_new directly, or via type->tp_base->tp_new. If
you do not do this, Python subclasses of your type that also inherit from other Python-defined classes may
not work correctly. (Specifically, you may not be able to create instances of such subclasses without getting
a TypeError.)
We also define an initialization function which accepts arguments to provide initial values for our instance :
static int
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"first", "last", "number", NULL};
PyObject *first = NULL, *last = NULL, *tmp;
if (first) {
tmp = self->first;
Py_INCREF(first);
self->first = first;
Py_XDECREF(tmp);
}
if (last) {
(suite sur la page suivante)
The tp_init slot is exposed in Python as the __init__() method. It is used to initialize an object after
it’s created. Initializers always accept positional and keyword arguments, and they should return either 0 on
success or -1 on error.
Unlike the tp_new handler, there is no guarantee that tp_init is called at all (for example, the pickle
module by default doesn’t call __init__() on unpickled instances). It can also be called multiple times.
Anyone can call the __init__() method on our objects. For this reason, we have to be extra careful when
assigning the new attribute values. We might be tempted, for example to assign the first member like this :
if (first) {
Py_XDECREF(self->first);
Py_INCREF(first);
self->first = first;
}
But this would be risky. Our type doesn’t restrict the type of the first member, so it could be any kind of
object. It could have a destructor that causes code to be executed that tries to access the first member ;
or that destructor could release the Global interpreter Lock and let arbitrary code run in other threads that
accesses and modifies our object.
To be paranoid and protect ourselves against this possibility, we almost always reassign members before
decrementing their reference counts. When don’t we have to do this ?
— when we absolutely know that the reference count is greater than 1 ;
— when we know that deallocation of the object 1 will neither release the GIL nor cause any calls back
into our type’s code ;
— when decrementing a reference count in a tp_dealloc handler on a type which doesn’t support cyclic
garbage collection 2 .
We want to expose our instance variables as attributes. There are a number of ways to do that. The simplest
way is to define member definitions :
1. This is true when we know that the object is a basic type, like a string or a float.
2. We relied on this in the tp_dealloc handler in this example, because our type doesn’t support garbage collection.
.tp_members = Custom_members,
Each member definition has a member name, type, offset, access flags and documentation string. See the
Generic Attribute Management section below for details.
A disadvantage of this approach is that it doesn’t provide a way to restrict the types of objects that can be
assigned to the Python attributes. We expect the first and last names to be strings, but any Python objects
can be assigned. Further, the attributes can be deleted, setting the C pointers to NULL. Even though we
can make sure the members are initialized to non-NULL values, the members can be set to NULL if the
attributes are deleted.
We define a single method, [Link](), that outputs the objects name as the concatenation of the first
and last names.
static PyObject *
Custom_name(CustomObject *self)
{
if (self->first == NULL) {
PyErr_SetString(PyExc_AttributeError, "first");
return NULL;
}
if (self->last == NULL) {
PyErr_SetString(PyExc_AttributeError, "last");
return NULL;
}
return PyUnicode_FromFormat("%S %S", self->first, self->last);
}
The method is implemented as a C function that takes a Custom (or Custom subclass) instance as the first
argument. Methods always take an instance as the first argument. Methods often take positional and keyword
arguments as well, but in this case we don’t take any and don’t need to accept a positional argument tuple
or keyword argument dictionary. This method is equivalent to the Python method :
def name(self):
return "%s %s" % ([Link], [Link])
Note that we have to check for the possibility that our first and last members are NULL. This is because
they can be deleted, in which case they are set to NULL. It would be better to prevent deletion of these
attributes and to restrict the attribute values to be strings. We’ll see how to do that in the next section.
Now that we’ve defined the method, we need to create an array of method definitions :
(note that we used the METH_NOARGS flag to indicate that the method is expecting no arguments other than
self )
and assign it to the tp_methods slot :
.tp_methods = Custom_methods,
Finally, we’ll make our type usable as a base class for subclassing. We’ve written our methods carefully so
far so that they don’t make any assumptions about the type of the object being created or used, so all we
need to do is to add the Py_TPFLAGS_BASETYPE to our class flag definition :
We rename PyInit_custom() to PyInit_custom2(), update the module name in the PyModuleDef struct,
and update the full class name in the PyTypeObject struct.
Finally, we update our [Link] file to build the new module :
#include <Python.h>
#include "structmember.h"
typedef struct {
PyObject_HEAD
PyObject *first; /* first name */
PyObject *last; /* last name */
int number;
} CustomObject;
static void
Custom_dealloc(CustomObject *self)
{
Py_XDECREF(self->first);
Py_XDECREF(self->last);
Py_TYPE(self)->tp_free((PyObject *) self);
}
static PyObject *
Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
CustomObject *self;
self = (CustomObject *) type->tp_alloc(type, 0);
if (self != NULL) {
self->first = PyUnicode_FromString("");
if (self->first == NULL) {
Py_DECREF(self);
(suite sur la page suivante)
static int
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"first", "last", "number", NULL};
PyObject *first = NULL, *last = NULL, *tmp;
if (first) {
tmp = self->first;
Py_INCREF(first);
self->first = first;
Py_DECREF(tmp);
}
if (last) {
tmp = self->last;
Py_INCREF(last);
self->last = last;
Py_DECREF(tmp);
}
return 0;
}
static PyObject *
Custom_getfirst(CustomObject *self, void *closure)
{
Py_INCREF(self->first);
return self->first;
}
static int
(suite sur la page suivante)
static PyObject *
Custom_getlast(CustomObject *self, void *closure)
{
Py_INCREF(self->last);
return self->last;
}
static int
Custom_setlast(CustomObject *self, PyObject *value, void *closure)
{
PyObject *tmp;
if (value == NULL) {
PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
return -1;
}
if (!PyUnicode_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"The last attribute value must be a string");
return -1;
}
tmp = self->last;
Py_INCREF(value);
self->last = value;
Py_DECREF(tmp);
return 0;
}
static PyObject *
Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
{
return PyUnicode_FromFormat("%S %S", self->first, self->last);
}
PyMODINIT_FUNC
PyInit_custom3(void)
{
PyObject *m;
if (PyType_Ready(&CustomType) < 0)
return NULL;
m = PyModule_Create(&custommodule);
if (m == NULL)
return NULL;
Py_INCREF(&CustomType);
PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
return m;
}
To provide greater control, over the first and last attributes, we’ll use custom getter and setter functions.
Here are the functions for getting and setting the first attribute :
static PyObject *
Custom_getfirst(CustomObject *self, void *closure)
{
Py_INCREF(self->first);
return self->first;
}
static int
Custom_setfirst(CustomObject *self, PyObject *value, void *closure)
{
PyObject *tmp;
if (value == NULL) {
PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
return -1;
}
if (!PyUnicode_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"The first attribute value must be a string");
return -1;
}
tmp = self->first;
Py_INCREF(value);
self->first = value;
Py_DECREF(tmp);
return 0;
}
The getter function is passed a Custom object and a « closure », which is a void pointer. In this case, the
closure is ignored. (The closure supports an advanced usage in which definition data is passed to the getter
and setter. This could, for example, be used to allow a single set of getter and setter functions that decide
the attribute to get or set based on data in the closure.)
The setter function is passed the Custom object, the new value, and the closure. The new value may be
NULL, in which case the attribute is being deleted. In our setter, we raise an error if the attribute is deleted
or if its new value is not a string.
We create an array of PyGetSetDef structures :
.tp_getset = Custom_getsetters,
The last item in a PyGetSetDef structure is the « closure » mentioned above. In this case, we aren’t using
a closure, so we just pass NULL.
We also need to update the tp_init handler to only allow strings 3 to be passed :
static int
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"first", "last", "number", NULL};
PyObject *first = NULL, *last = NULL, *tmp;
if (first) {
tmp = self->first;
Py_INCREF(first);
self->first = first;
Py_DECREF(tmp);
}
if (last) {
tmp = self->last;
Py_INCREF(last);
self->last = last;
Py_DECREF(tmp);
}
return 0;
}
With these changes, we can assure that the first and last members are never NULL so we can remove
checks for NULL values in almost all cases. This means that most of the Py_XDECREF() calls can be converted
to Py_DECREF() calls. The only place we can’t change these calls is in the tp_dealloc implementation, where
there is the possibility that the initialization of these members failed in tp_new.
We also rename the module initialization function and module name in the initialization function, as we did
before, and we add an extra definition to the [Link] file.
>>> l = []
>>> [Link](l)
>>> del l
3. We now know that the first and last members are strings, so perhaps we could be less careful about decrementing their
reference counts, however, we accept instances of string subclasses. Even though deallocating normal strings won’t call back
into our objects, we can’t guarantee that deallocating an instance of a string subclass won’t call back into our objects.
In this example, we create a list that contains itself. When we delete it, it still has a reference from itself.
Its reference count doesn’t drop to zero. Fortunately, Python’s cyclic garbage collector will eventually figure
out that the list is garbage and free it.
In the second version of the Custom example, we allowed any kind of object to be stored in the first or
last attributes 4 . Besides, in the second and third versions, we allowed subclassing Custom, and subclasses
may add arbitrary attributes. For any of those two reasons, Custom objects can participate in cycles :
To allow a Custom instance participating in a reference cycle to be properly detected and collected by the
cyclic GC, our Custom type needs to fill two additional slots and to enable a flag that enables these slots :
#include <Python.h>
#include "structmember.h"
typedef struct {
PyObject_HEAD
PyObject *first; /* first name */
PyObject *last; /* last name */
int number;
} CustomObject;
static int
Custom_traverse(CustomObject *self, visitproc visit, void *arg)
{
Py_VISIT(self->first);
Py_VISIT(self->last);
return 0;
}
static int
Custom_clear(CustomObject *self)
{
Py_CLEAR(self->first);
Py_CLEAR(self->last);
return 0;
}
static void
Custom_dealloc(CustomObject *self)
{
PyObject_GC_UnTrack(self);
Custom_clear(self);
Py_TYPE(self)->tp_free((PyObject *) self);
}
static PyObject *
(suite sur la page suivante)
4. Also, even with our attributes restricted to strings instances, the user could pass arbitrary str subclasses and therefore
still create reference cycles.
static int
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"first", "last", "number", NULL};
PyObject *first = NULL, *last = NULL, *tmp;
if (first) {
tmp = self->first;
Py_INCREF(first);
self->first = first;
Py_DECREF(tmp);
}
if (last) {
tmp = self->last;
Py_INCREF(last);
self->last = last;
Py_DECREF(tmp);
}
return 0;
}
static int
Custom_setfirst(CustomObject *self, PyObject *value, void *closure)
{
if (value == NULL) {
PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
return -1;
}
if (!PyUnicode_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"The first attribute value must be a string");
return -1;
}
Py_INCREF(value);
Py_CLEAR(self->first);
self->first = value;
return 0;
}
static PyObject *
Custom_getlast(CustomObject *self, void *closure)
{
Py_INCREF(self->last);
return self->last;
}
static int
Custom_setlast(CustomObject *self, PyObject *value, void *closure)
{
if (value == NULL) {
PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
return -1;
}
if (!PyUnicode_Check(value)) {
PyErr_SetString(PyExc_TypeError,
"The last attribute value must be a string");
return -1;
}
Py_INCREF(value);
Py_CLEAR(self->last);
self->last = value;
return 0;
}
static PyObject *
Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
{
return PyUnicode_FromFormat("%S %S", self->first, self->last);
}
PyMODINIT_FUNC
PyInit_custom4(void)
{
PyObject *m;
if (PyType_Ready(&CustomType) < 0)
return NULL;
m = PyModule_Create(&custommodule);
if (m == NULL)
(suite sur la page suivante)
Py_INCREF(&CustomType);
PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
return m;
}
First, the traversal method lets the cyclic GC know about subobjects that could participate in cycles :
static int
Custom_traverse(CustomObject *self, visitproc visit, void *arg)
{
int vret;
if (self->first) {
vret = visit(self->first, arg);
if (vret != 0)
return vret;
}
if (self->last) {
vret = visit(self->last, arg);
if (vret != 0)
return vret;
}
return 0;
}
For each subobject that can participate in cycles, we need to call the visit() function, which is passed to
the traversal method. The visit() function takes as arguments the subobject and the extra argument arg
passed to the traversal method. It returns an integer value that must be returned if it is non-zero.
Python provides a Py_VISIT() macro that automates calling visit functions. With Py_VISIT(), we can
minimize the amount of boilerplate in Custom_traverse :
static int
Custom_traverse(CustomObject *self, visitproc visit, void *arg)
{
Py_VISIT(self->first);
Py_VISIT(self->last);
return 0;
}
Note : The tp_traverse implementation must name its arguments exactly visit and arg in order to use
Py_VISIT().
Second, we need to provide a method for clearing any subobjects that can participate in cycles :
static int
Custom_clear(CustomObject *self)
{
Py_CLEAR(self->first);
Py_CLEAR(self->last);
(suite sur la page suivante)
Notice the use of the Py_CLEAR() macro. It is the recommended and safe way to clear data attributes of
arbitrary types while decrementing their reference counts. If you were to call Py_XDECREF() instead on the
attribute before setting it to NULL, there is a possibility that the attribute’s destructor would call back into
code that reads the attribute again (especially if there is a reference cycle).
PyObject *tmp;
tmp = self->first;
self->first = NULL;
Py_XDECREF(tmp);
Nevertheless, it is much easier and less error-prone to always use Py_CLEAR() when deleting an attribute.
Don’t try to micro-optimize at the expense of robustness !
The deallocator Custom_dealloc may call arbitrary code when clearing attributes. It means the circular GC
can be triggered inside the function. Since the GC assumes reference count is not zero, we need to untrack the
object from the GC by calling PyObject_GC_UnTrack() before clearing members. Here is our reimplemented
deallocator using PyObject_GC_UnTrack() and Custom_clear :
static void
Custom_dealloc(CustomObject *self)
{
PyObject_GC_UnTrack(self);
Custom_clear(self);
Py_TYPE(self)->tp_free((PyObject *) self);
}
That’s pretty much it. If we had written custom tp_alloc or tp_free handlers, we’d need to modify them
for cyclic garbage collection. Most extensions will use the versions automatically provided.
#include <Python.h>
typedef struct {
PyListObject list;
int state;
} SubListObject;
static PyObject *
SubList_increment(SubListObject *self, PyObject *unused)
{
self->state++;
return PyLong_FromLong(self->state);
}
static int
SubList_init(SubListObject *self, PyObject *args, PyObject *kwds)
{
if (PyList_Type.tp_init((PyObject *) self, args, kwds) < 0)
return -1;
self->state = 0;
return 0;
}
PyMODINIT_FUNC
PyInit_sublist(void)
{
PyObject *m;
SubListType.tp_base = &PyList_Type;
if (PyType_Ready(&SubListType) < 0)
return NULL;
m = PyModule_Create(&sublistmodule);
if (m == NULL)
return NULL;
Py_INCREF(&SubListType);
PyModule_AddObject(m, "SubList", (PyObject *) &SubListType);
return m;
}
As you can see, the source code closely resembles the Custom examples in previous sections. We will break
down the main differences between them.
typedef struct {
PyListObject list;
int state;
} SubListObject;
The primary difference for derived type objects is that the base type’s object structure must be the first
value. The base type will already include the PyObject_HEAD() at the beginning of its structure.
When a Python object is a SubList instance, its PyObject * pointer can be safely cast to both PyListObject
* and SubListObject * :
static int
SubList_init(SubListObject *self, PyObject *args, PyObject *kwds)
{
if (PyList_Type.tp_init((PyObject *) self, args, kwds) < 0)
return -1;
self->state = 0;
return 0;
}
We see above how to call through to the __init__ method of the base type.
This pattern is important when writing a type with custom tp_new and tp_dealloc members. The tp_new
handler should not actually create the memory for the object with its tp_alloc, but let the base class handle
it by calling its own tp_new.
The PyTypeObject struct supports a tp_base specifying the type’s concrete base class. Due to cross-platform
compiler issues, you can’t fill that field directly with a reference to PyList_Type ; it should be done later in
the module initialization function :
PyMODINIT_FUNC
PyInit_sublist(void)
(suite sur la page suivante)
m = PyModule_Create(&sublistmodule);
if (m == NULL)
return NULL;
Py_INCREF(&SubListType);
PyModule_AddObject(m, "SubList", (PyObject *) &SubListType);
return m;
}
Before calling PyType_Ready(), the type structure must have the tp_base slot filled in. When we are deriving
an existing type, it is not necessary to fill out the tp_alloc slot with PyType_GenericNew() – the allocation
function from the base type will be inherited.
After that, calling PyType_Ready() and adding the type object to the module is the same as with the basic
Custom examples.
Notes
destructor tp_dealloc;
printfunc tp_print;
getattrfunc tp_getattr;
setattrfunc tp_setattr;
PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
or tp_reserved (Python 3) */
reprfunc tp_repr;
PyNumberMethods *tp_as_number;
PySequenceMethods *tp_as_sequence;
PyMappingMethods *tp_as_mapping;
/* rich comparisons */
richcmpfunc tp_richcompare;
/* Iterators */
getiterfunc tp_iter;
iternextfunc tp_iternext;
destructor tp_finalize;
} PyTypeObject;
Now that’s a lot of methods. Don’t worry too much though – if you have a type you want to define, the
chances are very good that you will only implement a handful of these.
As you probably expect by now, we’re going to go over this and give more information about the various
handlers. We won’t go in the order they are defined in the structure, because there is a lot of historical
baggage that impacts the ordering of the fields. It’s often easiest to find an example that includes the fields
you need and then change the values to suit your new type.
const char *tp_name; /* For printing */
The name of the type – as mentioned in the previous chapter, this will appear in various places, almost
entirely for diagnostic purposes. Try to choose something that will be helpful in such a situation !
Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
These fields tell the runtime how much memory to allocate when new objects of this type are created.
Python has some built-in support for variable length structures (think : strings, tuples) which is where the
tp_itemsize field comes in. This will be dealt with later.
const char *tp_doc;
Here you can put a string (or its address) that you want returned when the Python script references obj.
__doc__ to retrieve the doc string.
Now we come to the basic type methods – the ones most extension types will implement.
This function is called when the reference count of the instance of your type is reduced to zero and the
Python interpreter wants to reclaim it. If your type has memory to free or other clean-up to perform, you
can put it here. The object itself needs to be freed here as well. Here is an example of this function :
static void
newdatatype_dealloc(newdatatypeobject *obj)
{
free(obj->obj_UnderlyingDatatypePtr);
Py_TYPE(obj)->tp_free(obj);
}
One important requirement of the deallocator function is that it leaves any pending exceptions alone. This
is important since deallocators are frequently called as the interpreter unwinds the Python stack ; when
the stack is unwound due to an exception (rather than normal returns), nothing is done to protect the
deallocators from seeing that an exception has already been set. Any actions which a deallocator performs
which may cause additional Python code to be executed may detect that an exception has been set. This can
lead to misleading errors from the interpreter. The proper way to protect against this is to save a pending
exception before performing the unsafe action, and restoring it when done. This can be done using the
PyErr_Fetch() and PyErr_Restore() functions :
static void
my_dealloc(PyObject *obj)
{
MyObject *self = (MyObject *) obj;
PyObject *cbresult;
if (self->my_callback != NULL) {
PyObject *err_type, *err_value, *err_traceback;
Py_DECREF(self->my_callback);
}
Py_TYPE(obj)->tp_free((PyObject*)self);
}
Note : There are limitations to what you can safely do in a deallocator function. First, if your type
supports garbage collection (using tp_traverse and/or tp_clear), some of the object’s members can have
been cleared or finalized by the time tp_dealloc is called. Second, in tp_dealloc, your object is in an
unstable state : its reference count is equal to zero. Any call to a non-trivial object or API (as in the example
above) might end up calling tp_dealloc again, causing a double free and a crash.
Starting with Python 3.4, it is recommended not to put any complex finalization code in tp_dealloc, and
instead use the new tp_finalize type method.
Voir aussi :
PEP 442 explains the new finalization scheme.
The tp_repr handler should return a string object containing a representation of the instance for which it
is called. Here is a simple example :
static PyObject *
newdatatype_repr(newdatatypeobject * obj)
{
(suite sur la page suivante)
If no tp_repr handler is specified, the interpreter will supply a representation that uses the type’s tp_name
and a uniquely-identifying value for the object.
The tp_str handler is to str() what the tp_repr handler described above is to repr() ; that is, it is
called when Python code calls str() on an instance of your object. Its implementation is very similar to the
tp_repr function, but the resulting string is intended for human consumption. If tp_str is not specified,
the tp_repr handler is used instead.
Here is a simple example :
static PyObject *
newdatatype_str(newdatatypeobject * obj)
{
return PyUnicode_FromFormat("Stringified_newdatatype{{size:%d}}",
obj->obj_UnderlyingDatatypePtr->size);
}
If accessing attributes of an object is always a simple operation (this will be explained shortly), there are
generic implementations which can be used to provide the PyObject* version of the attribute management
functions. The actual need for type-specific attribute handlers almost completely disappeared starting with
Python 2.2, though there are many examples which have not been updated to use some of the new generic
mechanism that is available.
When PyType_Ready() is called, it uses three tables referenced by the type object to create descriptors
which are placed in the dictionary of the type object. Each descriptor controls access to one attribute of the
instance object. Each of the tables is optional ; if all three are NULL, instances of the type will only have
attributes that are inherited from their base type, and should leave the tp_getattro and tp_setattro fields
NULL as well, allowing the base type to handle attributes.
The tables are declared as three fields of the type object :
If tp_methods is not NULL, it must refer to an array of PyMethodDef structures. Each entry in the table is
an instance of this structure :
One entry should be defined for each method provided by the type ; no entries are needed for methods
inherited from a base type. One additional entry is needed at the end ; it is a sentinel that marks the end of
the array. The ml_name field of the sentinel must be NULL.
The second table is used to define attributes which map directly to data stored in the instance. A variety of
primitive C types are supported, and access may be read-only or read-write. The structures in the table are
defined as :
For each entry in the table, a descriptor will be constructed and added to the type which will be able to
extract a value from the instance structure. The type field should contain one of the type codes defined in
the structmember.h header ; the value will be used to determine how to convert Python values to and from
C values. The flags field is used to store flags which control how the attribute can be accessed.
The following flag constants are defined in structmember.h ; they may be combined using bitwise-OR.
Constante Signification
READONLY Never writable.
READ_RESTRICTED Not readable in restricted mode.
WRITE_RESTRICTED Not writable in restricted mode.
RESTRICTED Not readable or writable in restricted mode.
An interesting advantage of using the tp_members table to build descriptors that are used at runtime is that
any attribute defined this way can have an associated doc string simply by providing the text in the table.
An application can use the introspection API to retrieve the descriptor from the class object, and get the
doc string using its __doc__ attribute.
As with the tp_methods table, a sentinel entry with a name value of NULL is required.
static PyObject *
newdatatype_getattr(newdatatypeobject *obj, char *name)
{
if (strcmp(name, "data") == 0)
{
return PyLong_FromLong(obj->data);
}
PyErr_Format(PyExc_AttributeError,
"'%.50s' object has no attribute '%.400s'",
tp->tp_name, name);
return NULL;
}
The tp_setattr handler is called when the __setattr__() or __delattr__() method of a class instance
would be called. When an attribute should be deleted, the third parameter will be NULL. Here is an example
that simply raises an exception ; if this were really all you wanted, the tp_setattr handler should be set to
NULL.
static int
newdatatype_setattr(newdatatypeobject *obj, char *name, PyObject *v)
{
PyErr_Format(PyExc_RuntimeError, "Read-only attribute: %s", name);
return -1;
}
The tp_richcompare handler is called when comparisons are needed. It is analogous to the rich comparison
methods, like __lt__(), and also called by PyObject_RichCompare() and PyObject_RichCompareBool().
This function is called with two Python objects and the operator as arguments, where the operator is one of
Py_EQ, Py_NE, Py_LE, Py_GT, Py_LT or Py_GT. It should compare the two objects with respect to the specified
operator and return Py_True or Py_False if the comparison is successful, Py_NotImplemented to indicate
that comparison is not implemented and the other object’s comparison method should be tried, or NULL if
an exception was set.
Here is a sample implementation, for a datatype that is considered equal if the size of an internal pointer is
equal :
static PyObject *
newdatatype_richcmp(PyObject *obj1, PyObject *obj2, int op)
{
PyObject *result;
int c, size1, size2;
size1 = obj1->obj_UnderlyingDatatypePtr->size;
size2 = obj2->obj_UnderlyingDatatypePtr->size;
switch (op) {
case Py_LT: c = size1 < size2; break;
case Py_LE: c = size1 <= size2; break;
case Py_EQ: c = size1 == size2; break;
case Py_NE: c = size1 != size2; break;
case Py_GT: c = size1 > size2; break;
case Py_GE: c = size1 >= size2; break;
}
result = c ? Py_True : Py_False;
Py_INCREF(result);
return result;
}
PyNumberMethods *tp_as_number;
PySequenceMethods *tp_as_sequence;
PyMappingMethods *tp_as_mapping;
If you wish your object to be able to act like a number, a sequence, or a mapping object, then you
place the address of a structure that implements the C type PyNumberMethods, PySequenceMethods, or
PyMappingMethods, respectively. It is up to you to fill in this structure with appropriate values. You can
find examples of the use of each of these in the Objects directory of the Python source distribution.
hashfunc tp_hash;
This function, if you choose to provide it, should return a hash number for an instance of your data type.
Here is a simple example :
static Py_hash_t
newdatatype_hash(newdatatypeobject *obj)
{
Py_hash_t result;
result = obj->some_size + 32767 * obj->some_number;
if (result == -1)
result = -2;
return result;
}
Py_hash_t is a signed integer type with a platform-varying width. Returning -1 from tp_hash indicates an
error, which is why you should be careful to avoid returning it when hash computation is successful, as seen
above.
ternaryfunc tp_call;
This function is called when an instance of your data type is « called », for example, if obj1 is an instance
of your data type and the Python script contains obj1('hello'), the tp_call handler is invoked.
This function takes three arguments :
1. self is the instance of the data type which is the subject of the call. If the call is obj1('hello'), then
self is obj1.
2. args is a tuple containing the arguments to the call. You can use PyArg_ParseTuple() to extract the
arguments.
3. kwds is a dictionary of keyword arguments that were passed. If this is non-NULL and you support
keyword arguments, use PyArg_ParseTupleAndKeywords() to extract the arguments. If you do not
want to support keyword arguments and this is non-NULL, raise a TypeError with a message saying
that keyword arguments are not supported.
Here is a toy tp_call implementation :
static PyObject *
newdatatype_call(newdatatypeobject *self, PyObject *args, PyObject *kwds)
{
PyObject *result;
const char *arg1;
const char *arg2;
const char *arg3;
/* Iterators */
getiterfunc tp_iter;
iternextfunc tp_iternext;
These functions provide support for the iterator protocol. Both handlers take exactly one parameter, the
instance for which they are being called, and return a new reference. In the case of an error, they should set an
exception and return NULL. tp_iter corresponds to the Python __iter__() method, while tp_iternext
corresponds to the Python __next__() method.
Any iterable object must implement the tp_iter handler, which must return an iterator object. Here the
same guidelines apply as for Python classes :
— For collections (such as lists and tuples) which can support multiple independent iterators, a new
iterator should be created and returned by each call to tp_iter.
— Objects which can only be iterated over once (usually due to side effects of iteration, such as file
objects) can implement tp_iter by returning a new reference to themselves – and should also therefore
implement the tp_iternext handler.
Any iterator object should implement both tp_iter and tp_iternext. An iterator’s tp_iter handler should
return a new reference to the iterator. Its tp_iternext handler should return a new reference to the next
object in the iteration, if there is one. If the iteration has reached the end, tp_iternext may return NULL
without setting an exception, or it may set StopIteration in addition to returning NULL ; avoiding the
exception can yield slightly better performance. If an actual error occurs, tp_iternext should always set an
exception and return NULL.
typedef struct {
PyObject_HEAD
PyObject *weakreflist; /* List of weak references */
} TrivialObject;
The only further addition is that tp_dealloc needs to clear any weak references (by calling
PyObject_ClearWeakRefs()) if the field is non-NULL :
static void
Trivial_dealloc(TrivialObject *self)
{
/* Clear weakrefs first before calling any destructors */
(suite sur la page suivante)
if (!PyObject_TypeCheck(some_object, &MyType)) {
PyErr_SetString(PyExc_TypeError, "arg #1 not a mything");
return NULL;
}
Voir aussi :
def initfunc_name(name):
try:
suffix = b'_' + [Link]('ascii')
except UnicodeEncodeError:
suffix = b'U_' + [Link]('punycode').replace(b'-', b'_')
return b'PyInit' + suffix
Il est possible d’exporter plusieurs modules depuis une seule bibliothèque partagée en définissant plusieurs
fonctions d’initialisation. Cependant pour les importer, un lien symbolique doit être créé pour chacun, ou
un importer personnalisé, puisque par défaut seule la fonction correspondant au nom du fichier est cherchée.
Voir le chapitre « Multiple modules in one library » dans la PEP 489 pour plus d’informations.
module1 = Extension('demo',
sources = ['demo.c'])
compilera demo.c, et produira un module d’extension nommé demo dans le dossier build. En fonction du
système, le fichier du module peut se retrouver dans build/[Link], et son nom peut être [Link] ou
[Link].
Dans le fichier [Link], tout est exécuté en appelant la fonction setup. Elle prend un nombre variable d’ar-
guments nommés, dont l’exemple précédent n’utilise qu’une partie. L’exemple précise des méta-informations
pour construire les paquets, et définir le contenu du paquet. Normalement un paquet contient des modules
additionnels, comme des modules sources, documentation, sous paquets, etc. Referez-vous à la documenta-
tion de distutils dans distutils-index pour en apprendre plus sur les fonctionnalités de distutils. Cette section
n’explique que la construction de modules d’extension.
Il est classique de pré-calculer les arguments à la fonction setup(), pour plus de lisibilité. Dans l’exemple
ci-dessus, l’argument ext_modules à setup() est une liste de modules d’extension, chacun est une instance
de la classe Extension. Dans l’exemple, l’instance définit une extension nommée demo construite par la
compilation d’un seul fichier source demo.c.
Dans la plupart des cas, construire une extension est plus complexe à cause des bibliothèques et définitions
de préprocesseurs dont la compilation pourrait dépendre. C’est ce qu’on remarque dans l’exemple plus bas.
module1 = Extension('demo',
define_macros = [('MAJOR_VERSION', '1'),
('MINOR_VERSION', '0')],
include_dirs = ['/usr/local/include'],
libraries = ['tcl83'],
library_dirs = ['/usr/local/lib'],
sources = ['demo.c'])
(suite sur la page suivante)
Dans cet exemple, la fonction setup() est appelée avec quelques autres méta-informations, ce qui est re-
commandé pour distribuer des paquets. En ce qui concerne l’extension, sont définis quelques macros pré-
processeur, dossiers pour les en-têtes et bibliothèques. En fonction du compilateur, distutils peut donner ces
informations de manière différente. Par exemple, sur Unix, ça peut ressembler aux commandes :
,→2.2/demo.o
Ces lignes ne sont qu’à titre d’exemple, les utilisateurs de distutils doivent avoir confiance en distutils qui
fera les appels correctement.
Les mainteneurs de modules voudront produire des paquets source, pour ce faire ils exécuteront :
Dans certains cas, des fichiers supplémentaires doivent être inclus dans une distribution source : c’est possible
via un fichier [Link], c.f. manifest.
Si la distribution source a été construite avec succès, les mainteneurs peuvent créer une distribution binaire.
En fonction de la plateforme, une des commandes suivantes peut être utilisée.
est utile tant pour le développeur Windows qui apprend à construire des extensions Python que pour le
développeur Unix souhaitant produire des logiciels pouvant être construits sur Unix et Windows.
Les auteurs de modules sont invités à utiliser l’approche distutils pour construire des modules d’extension, au
lieu de celle décrite dans cette section. Vous aurez toujours besoin du compilateur C utilisé pour construire
Python ; typiquement Microsoft Visual C++.
Note : Cette page mentionne plusieurs noms de fichiers comprenant un numéro de version Python encodé.
Ces noms de fichiers sont construits sous le format de version XY ; en pratique, 'X' représente le numéro
de version majeure et 'Y' représente le numéro de version mineure de la version Python avec laquelle vous
travaillez. Par exemple, si vous utilisez Python 2.2.1, XY correspond à 22.
[Link] construira aussi [Link]. Vous transmettez [Link] au lieur pour B et C. [Link] ne contient pas de code ;
il contient uniquement des informations qui seront utilisées lors de l’exécution pour accéder au code de A.
Sur Windows, utiliser une bibliothèque d’import est comme utiliser import spam ; cela vous donne accès
aux noms des spams, mais ne crée par de copie séparée. Sur Unix, se lier à une bibliothèque est plus comme
from spam import * ; cela créé une copie séparée.
La première commande a créé trois fichiers : [Link], [Link] et [Link]. [Link] ne contient pas
de fonctions Python (telles que PyArg_ParseTuple()), mais il sait comment trouver le code Python grâce à
[Link].
La seconde commande a créé [Link] (et .obj et .lib), qui sait comment trouver les fonctions nécessaires
dans spam, ainsi qu’à partir de l’exécutable Python.
Chaque identificateur n’est pas exporté vers la table de conversion. Si vous voulez que tout
autre module (y compris Python) soit capable de voir vos identificateurs, vous devez préci-
ser _declspec(dllexport), comme dans void _declspec(dllexport) initspam(void) ou PyObject
_declspec(dllexport) *NiGetSpamData(void).
Developer Studio apportera beaucoup de bibliothèques d’import dont vous n’avez pas vraiment besoin,
augmentant d’environ 100ko votre exécutable. Pour s’en débarrasser, allez dans les Paramètres du Projet,
onglet Lien, pour préciser ignorer les bibliothèques par défaut. Et la [Link] correcte à la liste des
bibliothèques.
Parfois, plutôt que de créer une extension qui s’exécute dans l’interpréteur Python comme application prin-
cipale, il est préférable d’intégrer l’interpréteur Python dans une application plus large. Cette section donne
quelques informations nécessaires au succès de cette opération.
c-api-index Les détails sur l’interface entre Python et le C sont donnés dans ce manuel. Pléthore infor-
mations s’y trouvent.
65
Extending and Embedding Python, Version 3.7.1
#include <Python.h>
int
main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
if (Py_FinalizeEx() < 0) {
exit(120);
}
PyMem_RawFree(program);
return 0;
}
C’est la fonction Py_SetProgramName() qui devrait être appelée en premier, avant Py_Initialize(),
afin d’informer l’interpréteur des chemins vers ses bibliothèques. Ensuite l’interpréteur est initialisé par
Py_Initialize(), suivi de l’exécution de Python codé en dur affichant la date et l’heure, puis, l’appel à
Py_FinalizeEx() éteint l’interpréteur, engendrant ainsi la fin du programme. Dans un vrai programme,
vous pourriez vouloir lire le script Python depuis une autre source, peut être depuis un éditeur de texte, un
fichier, ou une base de donnée. Récupérer du code Python depuis un fichier se fait via PyRun_SimplFile(),
qui vous économise le travail d’allouer de la mémoire et de charger le contenu du fichier.
Tel que vous le voyez, les conversions sont simplement inversées pour s’adapter au différentes directions de
transfert inter-langage. La seule différence est la fonction que vous appelez entre les deux conversions de
données. Lors de l’extension, vous appelez une fonction C, lors de l’intégration vous appelez une fonction
Python.
Ce chapitre ne couvrira pas la conversion des données de Python vers le C ni l’inverse. Aussi, un usage
correct des références, ainsi que savoir gérer les erreurs sont considérés acquis. Ces aspects étant identiques
à l’extension de l’interpréteur, vous pouvez vous référer aux chapitres précédents.
#include <Python.h>
int
main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue;
int i;
if (argc < 3) {
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
return 1;
}
Py_Initialize();
pName = PyUnicode_DecodeFSDefault(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, argv[2]);
/* pFunc is a new reference */
Ce code charge un script Python en utilisant argv[1], et appelle une fonction dont le nom est dans argv[2].
Ses arguments entiers sont les autres valeurs de argv. Si vous compilez et liez ce programme (appelons
l’exécutable call), et l’appelez pour exécuter un script Python, tel que :
def multiply(a,b):
print("Will compute", a, "times", b)
c = 0
for i in range(0, a):
c = c + b
return c
Bien que le programme soit plutôt gros pour ses fonctionnalités, la plupart du code n’est que conversion
de données entre Python et C, aussi que pour rapporter les erreurs. La partie intéressante, qui concerne
Py_Initialize();
pName = PyUnicode_DecodeFSDefault(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Après avoir initialisé l’interpréteur, le script est chargé en utilisant PyImport_Import(). Cette fonction
prend une chaîne Python pour argument, elle même construite en utilisant la fonction de conversion
PyUnicode_FromString().
Une fois le script chargé, le nom recherché est obtenu en utilisant PyObject_GetAttrString(). Si le nom
existe, et que l’objet récupéré peut être appelé, vous pouvez présumer sans risque que c’est une fonction.
Le programme continue, classiquement, par la construction de n-uplet d’arguments. L’appel à la fonction
Python est alors effectué avec :
Après l’exécution de la fonction, pValue est soit NULL, soit une référence sur la valeur donnée par la fonction.
Assurez-vous de libérer la référence après avoir utilisé la valeur.
static PyObject*
PyInit_emb(void)
{
return PyModule_Create(&EmbModule);
}
Insérez le code ci-dessus juste avant la fonction main(). Ajoutez aussi les deux instructions suivantes avant
l’appel à Py_Initialize() :
numargs = argc;
PyImport_AppendInittab("emb", &PyInit_emb);
Ces deux lignes initialisent la variable numarg, et rend la fonction [Link]() accessible à l’interpréteur
intégré. Avec ces ajouts, le script Python petit maintenant faire des choses comme
import emb
print("Number of arguments", [Link]())
Dans un cas réel, les méthodes exposeraient une API de l’application a Python.
$ /opt/bin/python3.4-config --cflags
-I/opt/include/python3.4m -I/opt/include/python3.4m -DNDEBUG -g -fwrapv -O3 -Wall -
,→Wstrict-prototypes
— pythonX.Y-config --ldflags vous donnera les drapeaux recommandés lors de l’édition de lien :
$ /opt/bin/python3.4-config --ldflags
-L/opt/lib/python3.4/config-3.4m -lpthread -ldl -lutil -lm -lpython3.4m -Xlinker -
,→export-dynamic
Note : Pour éviter la confusion entre différentes installations de Python, (et plus spécialement entre celle
de votre système et votre version compilée), il est recommandé d’utiliser un chemin absolu vers pythonX.
Y -config, comme dans l’exemple précédent.
Si cette procédure ne fonctionne pas pour vous (il n’est pas garanti qu’elle fonctionne pour toutes les pla-
teformes Unix, mais nous traiteront volontiers les rapports de bugs), vous devrez lire la documentation de
votre système sur la liaison dynamique (dynamic linking) et / ou examiner le Makefile de Python (utilisez
sysconfig.get_makefile_filename() pour trouver son emplacement) et les options de compilation. Dans
ce cas, le module sysconfig est un outil utile pour extraire automatiquement les valeurs de configuration
que vous voudrez combiner ensemble. Par exemple :
Glossaire
>>> L’invite de commande utilisée par défaut dans l’interpréteur interactif. On la voit souvent dans des
exemples de code qui peuvent être exécutés interactivement dans l’interpréteur.
... L’invite de commande utilisée par défaut dans l’interpréteur interactif lorsqu’on entre un bloc de
code indenté, dans des délimiteurs fonctionnant par paires (parenthèses, crochets, accolades, triple
guillemets), ou après un décorateur.
2to3 Outil qui essaie de convertir du code pour Python 2.x en code pour Python 3.x en gérant la
plupart des incompatibilités qui peuvent être détectées en analysant la source et parcourant son arbre
syntaxique.
2to3 est disponible dans la bibliothèque standard sous le nom de lib2to3 ; un point d’entrée indé-
pendant est fourni via Tools/scripts/2to3. Cf. 2to3-reference.
classe de base abstraite Les classes de base abstraites (ABC, suivant l’abréviation anglaise Abstract
Base Class) complètent le duck-typing en fournissant un moyen de définir des interfaces pour les cas où
d’autres techniques comme hasattr() seraient inélégantes ou subitement fausses (par exemple avec
les méthodes magiques). Les ABC introduisent des sous-classes virtuelles qui n’héritent pas d’une
classe mais qui sont quand même reconnues par isinstance() ou issubclass() (voir la documen-
tation du module abc). Python contient de nombreuses ABC pour les structures de données (dans le
module [Link]), les nombres (dans le module numbers), les flux (dans le module io) et les
chercheurs-chargeurs du système d’importation (dans le module [Link]). Vous pouvez créer
vos propres ABC avec le module abc.
annotation Étiquette associée à une variable, un attribut de classe, un paramètre de fonction ou une
valeur de retour. Elle est utilisé par convention comme type hint.
Les annotations de variables locales ne sont pas accessibles au moment de l’exécution, mais les anno-
tations de variables globales, d’attributs de classe et de fonctions sont stockées dans l’attribut spécial
__annotations__ des modules, classes et fonctions, respectivement.
Voir variable annotation, function annotation, PEP 484 et PEP 526, qui décrivent cette fonction-
nalité.
argument Valeur, donnée à une fonction ou à une méthode lors de son appel. Il existe deux types
d’arguments :
— argument nommé : un argument précédé d’un identifiant (comme name=) ou un dictionnaire précédé
de **, lors d’un appel de fonction. Par exemple, 3 et 5 sont tous les deux des arguments nommés
dans l’appel à complex() ici :
73
Extending and Embedding Python, Version 3.7.1
complex(real=3, imag=5)
complex(**{'real': 3, 'imag': 5})
— argument positionnel : Un argument qui n’est pas nommé. Les arguments positionnels apparaissent
au début de la liste des arguments, ou donnés sous forme d’un itérable précédé par *. Par exemple,
3 et 5 sont tous les deux des arguments positionnels dans les appels suivants :
complex(3, 5)
complex(*(3, 5))
Les arguments se retrouvent dans le corps de la fonction appelée parmi les variables locales. Voir
la section calls à propos des règles dictant cette affectation. Syntaxiquement, toute expression est
acceptée comme argument, et c’est la valeur résultante de l’expression qui sera affectée à la variable
locale.
Voir aussi parameter dans le glossaire, la question Différence entre argument et paramètre de la FAQ
et la PEP 362.
gestionnaire de contexte asynchrone (asynchronous context manager en anglais) Objet contrôlant
l’environnement à l’intérieur d’une instruction with en définissant les méthodes __aenter__() et
__aexit__(). A été Introduit par la PEP 492.
générateur asynchrone Fonction qui renvoie un asynchronous generator iterator. Cela ressemble à une
coroutine définie par async def, sauf qu’elle contient une ou des expressions yield produisant ainsi
uns série de valeurs utilisables dans une boucle async for.
Générateur asynchrone fait généralement référence à une fonction, mais peut faire référence à un
itérateur de générateur asynchrone dans certains contextes. Dans les cas où le sens voulu n’est pas
clair, utiliser l’ensemble des termes lève l’ambiguïté.
Un générateur asynchrone peut contenir des expressions await ainsi que des instructions async for,
et async with.
itérateur de générateur asynchrone Objet créé par une fonction asynchronous generator.
C’est un asynchronous iterator qui, lorsqu’il est appelé via la méthode __anext__() renvoie un objet
awaitable qui exécutera le corps de la fonction du générateur asynchrone jusqu’au prochain yield.
Chaque yield suspend temporairement l’exécution, en gardant en mémoire l’endroit et l’état de
l’exécution (ce qui inclut les variables locales et les try en cours). Lorsque l’exécution de l’itérateur
de générateur asynchrone reprend avec un nouvel awaitable renvoyé par __anext__(), elle repart de
là où elle s’était arrêtée. Voir la PEP 492 et la PEP 525.
itérable asynchrone Objet qui peut être utilisé dans une instruction async for. Sa méthode
__aiter__() doit renvoyer un asynchronous iterator. A été introduit par la PEP 492.
itérateur asynchrone Objet qui implémente les méthodes __aiter__() et __anext__(). __anext__
doit renvoyer un objet awaitable. Tant que la méthode __anext__() produit des objets awaitable, le
async for appelant les consomme. L’itérateur asynchrone lève une exception StopAsyncIteration
pour signifier la fin de l’itération. A été introduit par la PEP 492.
attribut Valeur associée à un objet et désignée par son nom via une notation utilisant des points. Par
exemple, si un objet o possède un attribut a, il sera référencé par o.a.
awaitable Objet pouvant être utilisé dans une expression await. Peut être une coroutine ou un objet
avec une méthode __await__(). Voir aussi la PEP 492.
BDFL Dictateur bienveillant à vie (Benevolent Dictator For Life en anglais). Pseudonyme de Guido
van Rossum, le créateur de Python.
fichier binaire Un file object capable de lire et d’écrire des bytes-like objects. Des fichiers binaires sont,
par exemple, les fichiers ouverts en mode binaire ('rb', 'wb', ou 'rb+'), [Link], sys.
[Link], les instances de [Link] ou de [Link].
Consultez fichier texte, un objet fichier capable de lire et d’écrire des objets str.
74 Annexe A. Glossaire
Extending and Embedding Python, Version 3.7.1
Objet bytes-compatible Un objet gérant les bufferobjects et pouvant exporter un tampon (buffer en
anglais) C-contiguous. Cela inclut les objets bytes, bytearray et [Link], ainsi que beaucoup
d’objets memoryview. Les objets bytes-compatibles peuvent être utilisés pour diverses opérations sur
des données binaires, comme la compression, la sauvegarde dans un fichier binaire ou l’envoi sur le
réseau.
Certaines opérations nécessitent de travailler sur des données binaires variables. La documentation
parle de ceux-ci comme des read-write bytes-like objects. Par exemple, bytearray ou une memoryview
d’un bytearray en font partie. D’autres opérations nécessitent de travailler sur des données bi-
naires stockées dans des objets immuables (« read-only bytes-like objects »), par exemples bytes
ou memoryview d’un objet byte.
code intermédiaire (bytecode) Le code source, en Python, est compilé en un code intermédiaire (byte-
code en anglais), la représentation interne à CPython d’un programme Python. Le code intermédiaire
est mis en cache dans un fichier .pyc de manière à ce qu’une seconde exécution soit plus rapide (la
compilation en code intermédiaire a déjà été faite). On dit que ce langage intermédiaire est exécuté
sur une virtual machine qui exécute des instructions machine pour chaque instruction du code in-
termédiaire. Notez que le code intermédiaire n’a pas vocation à fonctionner sur différentes machines
virtuelles Python ou à être stable entre différentes versions de Python.
La documentation du module dis fournit une liste des instructions du code intermédiaire.
classe Modèle pour créer des objets définis par l’utilisateur. Une définition de classe (class) contient
normalement des définitions de méthodes qui agissent sur les instances de la classe.
variable de classe Une variable définie dans une classe et destinée à être modifiée uniquement au niveau
de la classe (c’est-à-dire, pas dans une instance de la classe).
coercition Conversion implicite d’une instance d’un type vers un autre lors d’une opération dont les
deux opérandes doivent être de même type. Par exemple int(3.15) convertit explicitement le nombre
à virgule flottante en nombre entier 3. Mais dans l’opération 3 + 4.5, les deux opérandes sont d’un
type différent, alors qu’elles doivent avoir le même type pour être additionnées (sinon une exception
TypeError serait levée). Sans coercition, toutes les opérandes, même de types compatibles, devraient
être converties (on parle aussi de cast) explicitement par le développeur, par exemple : float(3) +
4.5 au lieu du simple 3 + 4.5.
nombre complexe Extension des nombres réels familiers, dans laquelle tous les nombres sont exprimés
sous la forme d’une somme d’une partie réelle et d’une partie imaginaire. Les nombres imaginaires
sont les nombres réels multipliés par l’unité imaginaire (la racine carrée de -1, souvent écrite i en
mathématiques ou j par les ingénieurs). Python comprend nativement les nombres complexes, écrits
avec cette dernière notation : la partie imaginaire est écrite avec un suffixe j, exemple, 3+1j. Pour
utiliser les équivalents complexes de math, utilisez cmath. Les nombres complexes sont un concept
assez avancé en mathématiques. Si vous ne connaissez pas ce concept, vous pouvez tranquillement les
ignorer.
gestionnaire de contexte Objet contrôlant l’environnement à l’intérieur d’un bloc with en définissant
les méthodes __enter__() et __exit__(). Consultez la PEP 343.
contigu Un tampon (buffer en anglais) est considéré comme contigu s’il est soit C-contigu soit Fortran-
contigu. Les tampons de dimension zéro sont C-contigus et Fortran-contigus. Pour un tableau à une
dimension, ses éléments doivent être placés en mémoire l’un à côté de l’autre, dans l’ordre croissant de
leur indice, en commençant à zéro. Pour qu’un tableau multidimensionnel soit C-contigu, le dernier
indice doit être celui qui varie le plus rapidement lors du parcours de ses éléments dans l’ordre de leur
adresse mémoire. À l’inverse, dans les tableaux Fortran-contigu, c’est le premier indice qui doit varier
le plus rapidement.
coroutine Les coroutines sont une forme généralisées des fonctions. On entre dans une fonction en un
point et on en sort en un autre point. On peut entrer, sortir et reprendre l’exécution d’une coroutine
en plusieurs points. Elles peuvent être implémentées en utilisant l’instruction async def. Voir aussi
la PEP 492.
fonction coroutine Fonction qui renvoie un objet coroutine. Une fonction coroutine peut être définie
par l’instruction async def et peut contenir les mots clés await, async for ainsi que async with.
75
Extending and Embedding Python, Version 3.7.1
def f(...):
...
f = staticmethod(f)
@staticmethod
def f(...):
...
Quoique moins fréquemment utilisé, le même concept existe pour les classes. Consultez la documen-
tation définitions de fonctions et définitions de classes pour en savoir plus sur les décorateurs.
descripteur N’importe quel objet définissant les méthodes __get__(), __set__(), ou __delete__().
Lorsque l’attribut d’une classe est un descripteur, son comportement spécial est déclenché lors de la
recherche des attributs. Normalement, lorsque vous écrivez a.b pour obtenir, affecter ou effacer un
attribut, Python recherche l’objet nommé b dans le dictionnaire de la classe de a. Mais si b est un
descripteur, c’est la méthode de ce descripteur qui est alors appelée. Comprendre les descripteurs est
requis pour avoir une compréhension approfondie de Python, ils sont la base de nombre de ses carac-
téristiques notamment les fonctions, méthodes, propriétés, méthodes de classes, méthodes statiques
et les références aux classes parentes.
Pour plus d’informations sur les méthodes des descripteurs, consultez descriptors.
dictionnaire Structure de donnée associant des clés à des valeurs. Les clés peuvent être n’importe
quel objet possédant les méthodes __hash__() et __eq__(). En Perl, les dictionnaires sont appelés
« hash ».
vue de dictionnaire Objets retournés par les méthodes [Link](), [Link]() et dict.
items(). Ils fournissent des vues dynamiques des entrées du dictionnaire, ce qui signifie que lorsque le
dictionnaire change, la vue change. Pour transformer une vue en vraie liste, utilisez list(dictview).
Voir dict-views.
docstring Première chaîne littérale qui apparaît dans l’expression d’une classe, fonction, ou module.
Bien qu’ignorée à l’exécution, elles est reconnue par le compilateur et placée dans l’attribut __doc__
de la classe, de la fonction ou du module. Comme cette chaîne est disponible par introspection, c’est
l’endroit idéal pour documenter l’objet.
duck-typing Style de programmation qui ne prend pas en compte le type d’un objet pour déterminer
s’il respecte une interface, mais qui appelle simplement la méthode ou l’attribut (Si ça a un bec
et que ça cancane, ça doit être un canard, duck signifie canard en anglais). En se concentrant sur
les interfaces plutôt que les types, du code bien construit améliore sa flexibilité en autorisant des
substitutions polymorphiques. Le duck-typing évite de vérifier les types via type() ou isinstance(),
Notez cependant que le duck-typing peut travailler de pair avec les classes de base abstraites. À la
place, le duck-typing utilise plutôt hasattr() ou la programmation EAFP.
EAFP Il est plus simple de demander pardon que demander la permission (Easier to Ask for Forgiveness
than Permission en anglais). Ce style de développement Python fait l’hypothèse que le code est valide
et traite les exceptions si cette hypothèse s’avère fausse. Ce style, propre et efficace, est caractérisé
par la présence de beaucoup de mots clés try et except. Cette technique de programmation contraste
avec le style LBYL utilisé couramment dans les langages tels que C.
76 Annexe A. Glossaire
Extending and Embedding Python, Version 3.7.1
expression Suite logique de termes et chiffres conformes à la syntaxe Python dont l’évaluation fournit
une valeur. En d’autres termes, une expression est une suite d’éléments tels que des noms, opérateurs,
littéraux, accès d’attributs, méthodes ou fonctions qui aboutissent à une valeur. Contrairement à
beaucoup d’autres langages, les différentes constructions du langage ne sont pas toutes des expressions.
On trouve également des instructions qui ne peuvent pas être utilisées comme expressions, tel que if.
Les affectations sont également des instructions et non des expressions.
module d’extension Module écrit en C ou C++, utilisant l’API C de Python pour interagir avec
Python et le code de l’utilisateur.
f-string Chaîne littérale préfixée de 'f' ou 'F'. Les « f-strings » sont un raccourci pour formatted string
literals. Voir la PEP 498.
objet fichier Objet exposant une ressource via une API orientée fichier (avec les méthodes read() ou
write()). En fonction de la manière dont il a été créé, un objet fichier peut interfacer l’accès à
un fichier sur le disque ou à un autre type de stockage ou de communication (typiquement l’entrée
standard, la sortie standard, un tampon en mémoire, une socket réseau, …). Les objets fichiers sont
aussi appelés file-like-objects ou streams.
Il existe en réalité trois catégories de fichiers objets : les fichiers binaires bruts, les fichiers binaires
avec tampon (buffer) et les fichiers textes. Leurs interfaces sont définies dans le module io. Le moyen
le plus simple et direct de créer un objet fichier est d’utiliser la fonction open().
objet fichier-compatible Synonyme de objet fichier.
chercheur Objet qui essaie de trouver un chargeur pour le module en cours d’importation.
Depuis Python 3.3, il existe deux types de chercheurs : les chercheurs dans les méta-chemins à utiliser
avec sys.meta_path ; les chercheurs d’entrée dans path à utiliser avec sys.path_hooks.
Voir les PEP 302, PEP 420 et PEP 451 pour plus de détails.
division entière Division mathématique arrondissant à l’entier inférieur. L’opérateur de la division
entière est //. Par exemple l’expression 11 // 4 vaut 2, contrairement à 11 / 4 qui vaut 2.75.
Notez que (-11) // 4 vaut -3 car l’arrondi se fait à l’entier inférieur. Voir la PEP 328.
fonction Suite d’instructions qui renvoie une valeur à son appelant. On peut lui passer des arguments
qui pourront être utilisés dans le corps de la fonction. Voir aussi paramètre, méthode et function.
annotation de fonction annotation d’un paramètre de fonction ou valeur de retour
Les annotations de fonctions sont généralement utilisées pour des indications de types : par exemple,
cette fonction devrait prendre deux arguments int et devrait également avoir une valeur de retour
de type int
77
Extending and Embedding Python, Version 3.7.1
générateur Fonction qui renvoie un itérateur de générateur. Cela ressemble à une fonction normale, en
dehors du fait qu’elle contient une ou des expressions yield produisant une série de valeurs utilisable
dans une boucle for ou récupérées une à une via la fonction next().
Fait généralement référence à une fonction générateur mais peut faire référence à un itérateur de
générateur dans certains contextes. Dans les cas où le sens voulu n’est pas clair, utiliser les termes
complets lève l’ambigüité.
itérateur de générateur Objet créé par une fonction générateur.
Chaque yield suspend temporairement l’exécution, en se rappelant l’endroit et l’état de l’exécution
(y compris les variables locales et les try en cours). Lorsque l’itérateur de générateur reprend, il repart
là où il en était (contrairement à une fonction qui prendrait un nouveau départ à chaque invocation).
expression génératrice Expression qui donne un itérateur. Elle ressemble à une expression normale,
suivie d’une expression for définissant une variable de boucle, un intervalle et une expression if
optionnelle. Toute cette expression génère des valeurs pour la fonction qui l’entoure :
fonction générique Fonction composée de plusieurs fonctions implémentant les mêmes opérations pour
différents types. L’implémentation à utiliser est déterminée lors de l’appel par l’algorithme de répar-
tition.
Voir aussi single dispatch, le décorateur [Link]() et la PEP 443.
GIL Voir global interpreter lock.
verrou global de l’interpréteur (global interpreter lock en anglais) Mécanisme utilisé par l’interpré-
teur CPython pour s’assurer qu’un seul fil d’exécution (thread en anglais) n’exécute le bytecode à la
fois. Cela simplifie l’implémentation de CPython en rendant le modèle objet (incluant des parties
critiques comme la classe native dict) implicitement protégé contre les accès concourants. Verrouiller
l’interpréteur entier rend plus facile l’implémentation de multiples fils d’exécution (multi-thread en
anglais), au détriment malheureusement de beaucoup du parallélisme possible sur les machines ayant
plusieurs processeurs.
Cependant, certains modules d’extension, standards ou non, sont conçus de manière à libérer le GIL
lorsqu’ils effectuent des tâches lourdes tel que la compression ou le hachage. De la même manière, le
GIL est toujours libéré lors des entrées / sorties.
Les tentatives précédentes d’implémenter un interpréteur Python avec une granularité de verrouillage
plus fine ont toutes échouées, à cause de leurs mauvaises performances dans le cas d’un processeur
unique. Il est admis que corriger ce problème de performance induit mènerait à une implémentation
beaucoup plus compliquée et donc plus coûteuse à maintenir.
pyc utilisant le hachage Un fichier de cache de code intermédiaire (bytecode en anglais) qui utilise le
hachage plutôt que l’heure de dernière modification du fichier source correspondant pour déterminer
sa validité. Voir pyc-invalidation.
hachable Un objet est hachable s’il a une empreinte (hash) qui ne change jamais (il doit donc implémen-
ter une méthode __hash__()) et s’il peut être comparé à d’autres objets (avec la méthode __eq__()).
Les objets hachables dont la comparaison par __eq__ est vraie doivent avoir la même empreinte.
La hachabilité permet à un objet d’être utilisé comme clé de dictionnaire ou en tant que membre d’un
ensemble (type set), car ces structures de données utilisent ce hash.
Tous les types immuables natifs de Python sont hachables, mais les conteneurs muables (comme les
listes ou les dictionnaires) ne le sont pas. Toutes les instances de classes définies par les utilisateurs
sont hachables par défaut. Elles sont toutes considérées différentes (sauf avec elles-mêmes) et leur
valeur de hachage est calculée à partir de leur id().
IDLE Environnement de développement intégré pour Python. IDLE est un éditeur basique et un inter-
préteur livré avec la distribution standard de Python.
immuable Objet dont la valeur ne change pas. Les nombres, les chaînes et les n-uplets sont immuables.
Ils ne peuvent être modifiés. Un nouvel objet doit être créé si une valeur différente doit être stockée.
78 Annexe A. Glossaire
Extending and Embedding Python, Version 3.7.1
Ils jouent un rôle important quand une valeur de hash constante est requise, typiquement en clé de
dictionnaire.
chemin des imports Liste de entrées dans lesquelles le chercheur basé sur les chemins cherche les
modules à importer. Typiquement, lors d’une importation, cette liste vient de [Link] ; pour les
sous-paquets, elle peut aussi venir de l’attribut __path__ du paquet parent.
importer Processus rendant le code Python d’un module disponible dans un autre.
importateur Objet qui trouve et charge un module, en même temps un chercheur et un chargeur.
interactif Python a un interpréteur interactif, ce qui signifie que vous pouvez écrire des expressions et
des instructions à l’invite de l’interpréteur. L’interpréteur Python va les exécuter immédiatement et
vous en présenter le résultat. Démarrez juste python (probablement depuis le menu principal de votre
ordinateur). C’est un moyen puissant pour tester de nouvelles idées ou étudier de nouveaux modules
(souvenez-vous de help(x)).
interprété Python est un langage interprété, en opposition aux langages compilés, bien que la frontière
soit floue en raison de la présence d’un compilateur en code intermédiaire. Cela signifie que les fichiers
sources peuvent être exécutés directement, sans avoir à compiler un fichier exécutable intermédiaire.
Les langages interprétés ont généralement un cycle de développement / débogage plus court que les
langages compilés. Cependant, ils s’exécutent généralement plus lentement. Voir aussi interactif .
arrêt de l’interpréteur Lorsqu’on lui demande de s’arrêter, l’interpréteur Python entre dans une phase
spéciale où il libère graduellement les ressources allouées, comme les modules ou quelques structures
de données internes. Il fait aussi quelques appels au ramasse-miettes. Cela peut déclencher l’exécution
de code dans des destructeurs ou des fonctions de rappels de weakrefs. Le code exécuté lors de l’arrêt
peut rencontrer quelques exception puisque les ressources auxquelles il fait appel pourraient ne plus
fonctionner, (typiquement les modules des bibliothèques ou le mécanisme de warning).
La principale raison d’arrêt de l’interpréteur est que le module __main__ ou le script en cours d’exé-
cution a terminé de s’exécuter.
itérable Objet capable de renvoyer ses éléments un à un. Par exemple, tous les types séquence (comme
list, str, et tuple), quelques autres types comme dict, objets fichiers ou tout objet d’une classe
ayant une méthode __iter__() ou __getitem__() qui implémente la sémantique d’une Sequence.
Les itérables peuvent être utilisés dans des boucles for et à beaucoup d’autres endroits où une
séquence est requise (zip(), map(), …). Lorsqu’un itérable est passé comme argument à la fonction
native iter(), celle-ci fournit en retour un itérateur sur cet itérable. Cet itérateur n’est valable que
pour une seule passe sur le jeu de valeurs. Lors de l’utilisation d’itérables, il n’est habituellement pas
nécessaire d’appeler iter() ou de s’occuper soi-même des objets itérateurs. L’instruction for le fait
automatiquement pour vous, créant une variable temporaire anonyme pour garder l’itérateur durant
la boucle. Voir aussi itérateur, séquence et générateur.
itérateur Objet représentant un flux de donnée. Des appels successifs à la méthode __next__() de
l’itérateur (ou le passer à la fonction native next()) donne successivement les objets du flux. Lorsque
plus aucune donnée n’est disponible, une exception StopIteration est levée. À ce point, l’itéra-
teur est épuisé et tous les appels suivants à sa méthode __next__() lèveront encore une exception
StopIteration. Les itérateurs doivent avoir une méthode __iter__() qui renvoie l’objet itérateur
lui même, de façon à ce que chaque itérateur soit aussi itérable et puisse être utilisé dans la plupart
des endroits où d’autres itérables sont attendus. Une exception notable est un code qui tente plusieurs
itérations complètes. Un objet conteneur, (tel que list) produit un nouvel itérateur neuf à chaque fois
qu’il est passé à la fonction iter() ou s’il est utilisé dans une boucle for. Faire ceci sur un itérateur
donnerait simplement le même objet itérateur épuisé utilisé dans son itération précédente, le faisant
ressembler à un conteneur vide.
Vous trouverez davantage d’informations dans typeiter.
fonction clé Une fonction clé est un objet appelable qui renvoie une valeur à fins de tri ou de classement.
Par exemple, la fonction [Link]() est utilisée pour générer une clé de classement prenant
en compte les conventions de classement spécifiques aux paramètres régionaux courants.
79
Extending and Embedding Python, Version 3.7.1
Plusieurs outils dans Python acceptent des fonctions clés pour déterminer comment les éléments sont
classés ou groupés. On peut citer les fonctions min(), max(), sorted(), [Link](), [Link](),
[Link](), [Link]() et [Link]().
Il existe plusieurs moyens de créer une fonction clé. Par exemple, la méthode [Link]() peut
servir de fonction clé pour effectuer des recherches insensibles à la casse. Aussi, il est possible de créer
des fonctions clés avec des expressions lambda, comme lambda r: (r[0], r[2]). Vous noterez que
le module operator propose des constructeurs de fonctions clefs : attrgetter(), itemgetter() et
methodcaller(). Voir Comment Trier pour des exemples de création et d’utilisation de fonctions
clefs.
argument nommé Voir argument.
lambda Fonction anonyme sous la forme d’une expression et ne contenant qu’une seule expression,
exécutée lorsque la fonction est appelée. La syntaxe pour créer des fonctions lambda est : lambda
[parameters]: expression
LBYL Regarde devant avant de tomber, (Look before you leap en anglais). Ce style de programmation
consiste à vérifier des conditions avant d’effectuer des appels ou des accès. Ce style contraste avec le
style EAFP et se caractérise par la présence de beaucoup d’instructions if.
Dans un environnement avec plusieurs fils d’exécution (multi-threaded en anglais), le style LBYL peut
engendrer un séquencement critique (race condition en anglais) entre le « regarde » et le « tomber ».
Par exemple, le code if key in mapping: return mapping[key] peut échouer si un autre fil d’exé-
cution supprime la clé key du mapping après le test mais avant l’accès. Ce problème peut être résolu
avec des verrous (locks) ou avec l’approche EAFP.
list Un type natif de sequence dans Python. En dépit de son nom, une list ressemble plus à un tableau
(array dans la plupart des langages) qu’à une liste chaînée puisque les accès se font en O(1).
liste en compréhension (ou liste en intension) Écriture concise pour manipuler tout ou partie
des éléments d’une séquence et renvoyer une liste contenant les résultats. result = ['{:#04x}'.
format(x) for x in range(256) if x % 2 == 0] génère la liste composée des nombres pairs de 0
à 255 écrits sous formes de chaînes de caractères et en hexadécimal (0x…). La clause if est optionnelle.
Si elle est omise, tous les éléments du range(256) seront utilisés.
chargeur Objet qui charge un module. Il doit définir une méthode nommée load_module(). Un chargeur
est typiquement donné par un chercheur. Voir la PEP 302 pour plus de détails et [Link].
Loader pour sa classe de base abstraite.
Tableau de correspondances (mapping en anglais) Conteneur permettant de rechercher des élé-
ments à partir de clés et implémentant les méthodes spécifiées dans les classes de base abs-
traites [Link] ou [Link]. Les classes suivantes sont
des exemples de tableaux de correspondances : dict, [Link], collections.
OrderedDict et [Link].
chercheur dans les méta-chemins Un chercheur renvoyé par une recherche dans sys.meta_path. Les
chercheurs dans les méta-chemins ressemblent, mais sont différents des chercheurs d’entrée dans path.
Voir [Link] pour les méthodes que les chercheurs dans les méta-chemins
doivent implémenter.
métaclasse Classe d’une classe. Les définitions de classe créent un nom pour la classe, un dictionnaire
de classe et une liste de classes parentes. La métaclasse a pour rôle de réunir ces trois paramètres pour
construire la classe. La plupart des langages orientés objet fournissent une implémentation par défaut.
La particularité de Python est la possibilité de créer des métaclasses personnalisées. La plupart des
utilisateurs n’aura jamais besoin de cet outil, mais lorsque le besoin survient, les métaclasses offrent
des solutions élégantes et puissantes. Elles sont utilisées pour journaliser les accès à des propriétés,
rendre sûr les environnements multi-threads, suivre la création d’objets, implémenter des singletons
et bien d’autres tâches.
Plus d’informations sont disponibles dans : metaclasses.
méthode Fonction définie à l’intérieur d’une classe. Lorsqu’elle est appelée comme un attribut d’une
instance de cette classe, la méthode reçoit l’instance en premier argument (qui, par convention, est
habituellement nommé self). Voir function et nested scope.
80 Annexe A. Glossaire
Extending and Embedding Python, Version 3.7.1
ordre de résolution des méthodes L’ordre de résolution des méthodes (MRO pour Method Resolu-
tion Order en anglais) est, lors de la recherche d’un attribut dans les classes parentes, la façon dont
l’interpréteur Python classe ces classes parentes. Voir The Python 2.3 Method Resolution Order pour
plus de détails sur l’algorithme utilisé par l’interpréteur Python depuis la version 2.3.
module Objet utilisé pour organiser une portion unitaire de code en Python. Les modules ont un espace
de noms et peuvent contenir n’importe quels objets Python. Charger des modules est appelé importer.
Voir aussi paquet.
spécificateur de module Espace de noms contenant les informations, relatives à l’importation, utilisées
pour charger un module. C’est une instance de la classe [Link].
MRO Voir ordre de résolution des méthodes.
muable Un objet muable peut changer de valeur tout en gardant le même id(). Voir aussi immuable.
n-uplet nommé (named-tuple en anglais) Classe qui, comme un n-uplet (tuple en anglais), a ses éléments
accessibles par leur indice. Et en plus, les éléments sont accessibles par leur nom. Par exemple, time.
localtime() donne un objet ressemblant à un n-uplet, dont year est accessible par son indice : t[0]
ou par son nom : t.tm_year).
Un n-uplet nommé peut être un type natif tel que time.struct_time ou il peut être construit comme
une simple classe. Un n-uplet nommé complet peut aussi être créé via la fonction collections.
namedtuple(). Cette dernière approche fournit automatiquement des fonctionnalités supplémentaires,
tel qu’une représentation lisible comme Employee(name='jones', title='programmer').
espace de noms L’endroit où une variable est stockée. Les espaces de noms sont implémentés avec des
dictionnaires. Il existe des espaces de noms globaux, natifs ou imbriqués dans les objets (dans les
méthodes). Les espaces de noms favorisent la modularité car ils permettent d’éviter les conflits de
noms. Par exemple, les fonctions [Link] et [Link]() sont différenciées par leurs espaces
de nom. Les espaces de noms aident aussi à la lisibilité et la maintenabilité en rendant clair quel
module implémente une fonction. Par exemple, écrire [Link]() ou [Link]() affiche
clairement que ces fonctions sont implémentées respectivement dans les modules random et itertools.
paquet-espace de noms Un paquet tel que défini dans la PEP 421 qui ne sert qu’à contenir des
sous-paquets. Les paquets-espace de noms peuvent n’avoir aucune représentation physique et, plus
spécifiquement, ne sont pas comme un paquet classique puisqu’ils n’ont pas de fichier __init__.py.
Voir aussi module.
portée imbriquée Possibilité de faire référence à une variable déclarée dans une définition englobante.
Typiquement, une fonction définie à l’intérieur d’une autre fonction a accès aux variables de cette
dernière. Souvenez-vous cependant que cela ne fonctionne que pour accéder à des variables, pas pour
les assigner. Les variables locales sont lues et assignées dans l’espace de noms le plus proche. Tout
comme les variables globales qui sont stockés dans l’espace de noms global, le mot clef nonlocal
permet d’écrire dans l’espace de noms dans lequel est déclarée la variable.
nouvelle classe Ancien nom pour l’implémentation actuelle des classes, pour tous les objets. Dans les
anciennes versions de Python, seules les nouvelles classes pouvaient utiliser les nouvelles fonctionnalités
telles que __slots__, les descripteurs, les propriétés, __getattribute__(), les méthodes de classe et
les méthodes statiques.
objet N’importe quelle donnée comportant des états (sous forme d’attributs ou d’une valeur) et un com-
portement (des méthodes). C’est aussi (object) l’ancêtre commun à absolument toutes les nouvelles
classes.
paquet module Python qui peut contenir des sous-modules ou des sous-paquets. Techniquement, un
paquet est un module qui possède un attribut __path__.
Voir aussi paquet classique et namespace package.
paramètre Entité nommée dans la définition d’une fonction (ou méthode), décrivant un argument (ou
dans certains cas des arguments) que la fonction accepte. Il existe cinq sortes de paramètres :
— positional-or-keyword : l’argument peut être passé soit par sa position, soit en tant que argument
nommé. C’est le type de paramètre par défaut. Par exemple, foo et bar dans l’exemple suivant :
81
Extending and Embedding Python, Version 3.7.1
82 Annexe A. Glossaire
Extending and Embedding Python, Version 3.7.1
API provisoire Une API provisoire est une API qui n’offre aucune garantie de rétrocompatibilité (la
bibliothèque standard exige la rétrocompatibilité). Bien que des changements majeurs d’une telle
interface ne soient pas attendus, tant qu’elle est étiquetée provisoire, des changement cassant la
rétrocompatibilité (y compris sa suppression complète) peuvent survenir si les développeurs principaux
le jugent nécessaire. Ces modifications ne surviendront que si de sérieux problèmes sont découverts et
qu’ils n’avaient pas été identifiés avant l’ajout de l’API.
Même pour les API provisoires, les changement cassant la rétrocompatibilité sont considérées comme
des « solutions de dernier recours ». Tout ce qui est possible sera fait pour tenter de résoudre les
problème en conservant la rétrocompatibilité.
Ce processus permet à la bibliothèque standard de continuer à évoluer avec le temps, sans se bloquer
longtemps sur des erreurs d’architecture. Voir la PEP 411 pour plus de détails.
paquet provisoire Voir provisional API .
Python 3000 Surnom donné à la série des Python 3.x (très vieux surnom donné à l’époque où Python
3 représentait un futur lointain). Aussi abrégé Py3k.
Pythonique Idée, ou bout de code, qui colle aux idiomes de Python plutôt qu’aux concepts communs
rencontrés dans d’autres langages. Par exemple, il est idiomatique en Python de parcourir les éléments
d’un itérable en utilisant for. Beaucoup d’autres langages n’ont pas cette possibilité, donc les gens
qui ne sont pas habitués à Python utilisent parfois un compteur numérique à la place :
for i in range(len(food)):
print(food[i])
nom qualifié Nom, comprenant des points, montrant le « chemin » de l’espace de noms global d’un
module vers une classe, fonction ou méthode définie dans ce module, tel que défini dans la PEP 3155.
Pour les fonctions et classes de premier niveau, le nom qualifié est le même que le nom de l’objet :
>>> class C:
... class D:
... def meth(self):
... pass
...
>>> C.__qualname__
'C'
>>> C.D.__qualname__
'C.D'
>>> [Link].__qualname__
'[Link]'
Lorsqu’il est utilisé pour nommer des modules, le nom qualifié complet (fully qualified name - FQN
en anglais) signifie le chemin complet (séparé par des points) vers le module, incluant tous les paquets
parents. Par exemple : [Link] :
83
Extending and Embedding Python, Version 3.7.1
paquet classique paquet traditionnel, tel qu’un dossier contenant un fichier __init__.py.
Voir aussi paquet-espace de noms.
__slots__ Déclaration dans une classe qui économise de la mémoire en pré-allouant de l’espace pour les
attributs des instances et qui élimine le dictionnaire (des attributs) des instances. Bien que populaire,
cette technique est difficile à maîtriser et devrait être réservée à de rares cas où un grand nombre
d’instances dans une application devient un sujet critique pour la mémoire.
séquence itérable qui offre un accès efficace à ses éléments par un indice sous forme de nombre entier
via la méthode spéciale __getitem__() et qui définit une méthode __len__() donnant sa taille. Voici
quelques séquences natives : list, str, tuple, et bytes. Notez que dict possède aussi une méthode
__getitem__() et une méthode __len__(), mais il est considéré comme un mapping plutôt qu’une
séquence, car ses accès se font par une clé arbitraire immuable plutôt qu’un nombre entier.
La classe abstraite de base [Link] définit une interface plus riche qui va au-
delà des simples __getitem__() et __len__(), en ajoutant count(), index(), __contains__() et
__reversed__(). Les types qui implémentent cette interface étendue peuvent s’enregistrer explicite-
ment en utilisant register().
distribution simple Forme de distribution, comme les fonction génériques, où l’implémentation est
choisie en fonction du type d’un seul argument.
tranche (slice en anglais), un objet contenant habituellement une portion de séquence. Une tranche est
créée en utilisant la notation [] avec des : entre les nombres lorsque plusieurs sont fournis, comme
dans variable_name[1:3:5]. Cette notation utilise des objets slice en interne.
méthode spéciale (special method en anglais) Méthode appelée implicitement par Python pour exécuter
une opération sur un type, comme une addition. De telles méthodes ont des noms commençant et
terminant par des doubles tirets bas. Les méthodes spéciales sont documentées dans specialnames.
instruction Une instruction (statement en anglais) est un composant d’un « bloc » de code. Une ins-
truction est soit une expression, soit une ou plusieurs constructions basées sur un mot-clé, comme if,
while ou for.
struct sequence Un n-uplet (tuple en anglais) dont les éléments sont nommés. Les struct sequences
exposent une interface similaire au n-uplet nommé car on peut accéder à leurs éléments par un nom
d’attribut ou par un indice. Cependant, elles n’ont aucune des méthodes du n-uplet nommé : ni
[Link]._make() ou _asdict(). Par exemple sys.float_info ou les valeurs
données par [Link]() sont des struct sequence.
encodage de texte Codec (codeur-décodeur) qui convertit des chaînes de caractères Unicode en octets
(classe bytes).
fichier texte file object capable de lire et d’écrire des objets str. Souvent, un fichier texte (text file
en anglais) accède en fait à un flux de donnée en octets et gère l”text encoding automatiquement.
Des exemples de fichiers textes sont les fichiers ouverts en mode texte ('r' ou 'w'), [Link],
[Link] et les instances de [Link].
Voir aussi binary file pour un objet fichier capable de lire et d’écrire bytes-like objects.
chaîne entre triple guillemets Chaîne qui est délimitée par trois guillemets simples (') ou trois guille-
mets doubles ("). Bien qu’elle ne fournisse aucune fonctionnalité qui ne soit pas disponible avec une
chaîne entre guillemets, elle est utile pour de nombreuses raisons. Elle vous autorise à insérer des
guillemets simples et doubles dans une chaîne sans avoir à les protéger et elle peut s’étendre sur
plusieurs lignes sans avoir à terminer chaque ligne par un \. Elle est ainsi particulièrement utile pour
les chaînes de documentation (docstrings).
type Le type d’un objet Python détermine quel genre d’objet c’est. Tous les objets ont un type. Le type
d’un objet peut être obtenu via son attribut __class__ ou via type(obj).
alias de type Synonyme d’un type, créé en affectant le type à un identifiant.
Les alias de types sont utiles pour simplifier les indications de types. Par exemple :
84 Annexe A. Glossaire
Extending and Embedding Python, Version 3.7.1
def remove_gray_shades(
colors: List[Tuple[int, int, int]]) -> List[Tuple[int, int, int]]:
pass
class C:
field: 'annotation'
Les annotations de variables sont généralement utilisées pour des indications de types : par exemple,
cette variable devrait prendre des valeurs de type int
count: int = 0
85
Extending and Embedding Python, Version 3.7.1
86 Annexe A. Glossaire
ANNEXE B
Ces documents sont générés à partir de sources en reStructuredText par Sphinx, un analyseur de documents
spécialement conçu pour la documentation Python.
Le développement de la documentation et de ses outils est entièrement basé sur le volontariat, tout comme
Python. Si vous voulez contribuer, allez voir la page reporting-bugs qui contient des informations pour vous
y aider. Les nouveaux volontaires sont toujours les bienvenus !
Merci beaucoup à :
— Fred L. Drake, Jr., créateur des outils originaux de la documentation Python et rédacteur de la plupart
de son contenu ;
— le projet Docutils pour avoir créé reStructuredText et la suite d’outils Docutils ;
— Fredrik Lundh pour son projet Alternative Python Reference, dont Sphinx a pris beaucoup de bonnes
idées.
87
Extending and Embedding Python, Version 3.7.1
Histoire et licence
89
Extending and Embedding Python, Version 3.7.1
Note : Compatible GPL ne signifie pas que nous distribuons Python sous licence GPL. Toutes les licences
Python, excepté la licence GPL, vous permettent la distribution d’une version modifiée sans rendre open
source ces changements. La licence « compatible GPL » rend possible la diffusion de Python avec un autre
logiciel qui est lui, diffusé sous la licence GPL ; les licences « non compatible GPL » ne le peuvent pas.
Merci aux nombreux bénévoles qui ont travaillé sous la direction de Guido pour rendre ces versions possibles.
2. Subject to the terms and conditions of this License Agreement, PSF hereby
grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,
analyze, test, perform and/or display publicly, prepare derivative works,
distribute, and otherwise use Python 3.7.1 alone or in any derivative
version, provided, however, that PSF's License Agreement and PSF's notice of
copyright, i.e., "Copyright © 2001-2018 Python Software Foundation; All Rights
Reserved" are retained in Python 3.7.1 alone or in any derivative version
prepared by Licensee.
5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON 3.7.1
FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF
MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 3.7.1, OR ANY DERIVATIVE
THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
2. Subject to the terms and conditions of this BeOpen Python License Agreement,
BeOpen hereby grants Licensee a non-exclusive, royalty-free, world-wide license
to reproduce, analyze, test, perform and/or display publicly, prepare derivative
works, distribute, and otherwise use the Software alone or in any derivative
version, provided, however, that the BeOpen Python License is retained in the
Software, alone or in any derivative version prepared by Licensee.
4. BEOPEN SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF THE SOFTWARE FOR
ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF USING,
MODIFYING OR DISTRIBUTING THE SOFTWARE, OR ANY DERIVATIVE THEREOF, EVEN IF
ADVISED OF THE POSSIBILITY THEREOF.
2. Subject to the terms and conditions of this License Agreement, CNRI hereby
grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,
(suite sur la page suivante)
4. CNRI is making Python 1.6.1 available to Licensee on an "AS IS" basis. CNRI
MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
BUT NOT LIMITATION, CNRI MAKES NO AND DISCLAIMS ANY REPRESENTATION OR WARRANTY
OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
PYTHON 1.6.1 WILL NOT INFRINGE ANY THIRD PARTY RIGHTS.
5. CNRI SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON 1.6.1 FOR
ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF
MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 1.6.1, OR ANY DERIVATIVE
THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided that
the above copyright notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting documentation, and that
the name of Stichting Mathematisch Centrum or CWI not be used in advertising or
publicity pertaining to distribution of the software without specific, written
prior permission.
THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
Permission to use, copy, modify, and distribute this Python software and
its associated documentation for any purpose without fee is hereby
granted, provided that the above copyright notice appears in all copies,
and that both that copyright notice and this permission notice appear in
supporting documentation, and that the name of neither Automatrix,
Bioreason or Mojam Media be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
C.3.7 Appel de procédures distantes en XML (RPC, pour Remote Procedure Call)
Le module [Link] contient la note suivante :
The XML-RPC client interface is
SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.
C.3.8 test_epoll
Le module test_epoll contient la note suivante :
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
C.3.10 SipHash24
Le fichier Python/pyhash.c contiens une implémentation par Marek Majkowski de l’algorithme SipHash24
de Dan Bernstein. Il contient la note suivante :
<MIT License>
Copyright (c) 2013 Marek Majkowski <marek@[Link]>
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
(suite sur la page suivante)
Original location:
[Link]
C.3.12 OpenSSL
Les modules hashlib, posix, ssl, et crypt utilisent la bibliothèque OpenSSL pour améliorer les perfor-
mances, si elle est disponible via le système d’exploitation. Aussi les outils d’installation sur Windows et
Mac OS X peuvent inclure une copie des bibliothèques d’OpenSSL, donc on colle une copie de la licence
d’OpenSSL ici :
LICENSE ISSUES
==============
The OpenSSL toolkit stays under a dual license, i.e. both the conditions of
the OpenSSL License and the original SSLeay license apply to the toolkit.
See below for the actual license texts. Actually both licenses are BSD-style
Open Source licenses. In case of any license issues related to OpenSSL
please contact openssl-core@[Link].
OpenSSL License
(suite sur la page suivante)
/* ====================================================================
* Copyright (c) 1998-2008 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. ([Link]
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* openssl-core@[Link].
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit ([Link]
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@[Link]). This product includes software written by Tim
(suite sur la page suivante)
C.3.13 expat
Le module pyexpat est compilé avec une copie des sources d”expat, sauf si la compilation est configurée avec
--with-system-expat :
Copyright (c) 1998, 1999, 2000 Thai Open Source Software Center Ltd
and Clark Cooper
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
C.3.14 libffi
Le module _ctypes est compilé en utilisant une copie des sources de la libffi, sauf si la compilation est
configurée avec --with-system-libffi :
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
C.3.15 zlib
Le module zlib est compilé en utilisant une copie du code source de zlib si la version de zlib trouvée sur le
système est trop vieille pour être utilisée :
Copyright (C) 1995-2011 Jean-loup Gailly and Mark Adler
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
C.3.16 cfuhash
L’implémentation des dictionnaires, utilisée par le module tracemalloc est basée sur le projet cfuhash :
Copyright (c) 2005 Don Owens
All rights reserved.
C.3.17 libmpdec
Le module _decimal est construit en incluant une copie de la bibliothèque libmpdec, sauf si elle est compilée
avec --with-system-libmpdec :
Copyright (c) 2008-2016 Stefan Krah. All rights reserved.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
(suite sur la page suivante)
Copyright
Voir Histoire et licence pour des informations complètes concernant la licence et les permissions.
107
Extending and Embedding Python, Version 3.7.1
Non-alphabetical D
..., 73 deallocation, object, 52
2to3, 73 décorateur, 76
>>>, 73 descripteur, 76
__future__, 77 dictionnaire, 76
__slots__, 84 distribution simple, 84
division entière, 77
A docstring, 76
alias de type, 84 duck-typing, 76
annotation, 73
annotation de fonction, 77 E
annotation de variable, 85 EAFP, 76
API provisoire, 83 encodage de texte, 84
argument, 73 entrée de chemin, 82
argument nommé, 80 environnement virtuel, 85
argument positionnel, 82 espace de noms, 81
arrêt de l’interpréteur, 79 expression, 77
attribut, 74 expression génératrice, 78
awaitable, 74
F
B fichier binaire, 74
BDFL, 74 fichier texte, 84
finalization, of objects, 52
C fonction, 77
C-contiguous, 75 fonction clé, 79
chaîne entre triple guillemets, 84 fonction coroutine, 75
chargeur, 80 fonction de base
chemin des imports, 79 repr, 53
chercheur, 77 fonction générique, 78
chercheur basé sur les chemins, 82 Fortran contiguous, 75
chercheur dans les méta-chemins, 80 f-string, 77
chercheur de chemins, 82
classe, 75 G
classe de base abstraite, 73 générateur, 78
code intermédiaire (bytecode), 75 générateur asynchrone, 74
coercition, 75 generator, 77
contigu, 75 generator expression, 78
coroutine, 75 gestionnaire de contexte, 75
CPython, 76 gestionnaire de contexte asynchrone, 74
109
Extending and Embedding Python, Version 3.7.1
H P
hachable, 78 paquet, 81
paquet classique, 84
I paquet provisoire, 83
IDLE, 78 paquet-espace de noms, 81
immuable, 78 paramètre, 81
importateur, 79 PEP, 82
importer, 79 Philbrick, Geoff, 16
indication de type, 85 point d’entrée pour la recherche dans path, 82
instruction, 84 portée imbriquée, 81
interactif, 79 portion, 82
interprété, 79 PyArg_ParseTuple(), 14
itérable, 79 PyArg_ParseTupleAndKeywords(), 15
itérable asynchrone, 74 pyc utilisant le hachage, 78
itérateur, 79 PyErr_Fetch(), 52
itérateur asynchrone, 74 PyErr_Restore(), 52
itérateur de générateur, 78 PyInit_modulename (fonction C), 60
itérateur de générateur asynchrone, 74 PyObject_CallObject(), 13
Python 3000, 83
L Python Enhancement Proposals
lambda, 80 PEP 1, 82
LBYL, 80 PEP 278, 85
Le zen de Python, 85 PEP 302, 77, 80
list, 80 PEP 328, 77
liste en compréhension (ou liste en intension), 80 PEP 343, 75
PEP 362, 74, 82
M PEP 411, 83
machine virtuelle, 85 PEP 420, 77, 82
métaclasse, 80 PEP 421, 81
méthode, 80 PEP 442, 53
méthode spéciale, 84 PEP 443, 78
module, 81 PEP 451, 77
module d’extension, 77 PEP 484, 73, 85
MRO, 81 PEP 489, 11, 61
muable, 81 PEP 492, 7476
PEP 498, 77
N PEP 519, 82
nom qualifié, 83 PEP 525, 74
nombre complexe, 75 PEP 526, 73, 85
nombre de références, 83 PEP 3116, 85
nouvelle classe, 81 PEP 3155, 83
n-uplet nommé, 81 Pythonique, 83
PYTHONPATH, 60
O
object
R
deallocation, 52 ramasse-miettes, 77
finalization, 52 READ_RESTRICTED, 55
objet, 81 READONLY, 55
Objet bytes-compatible, 75 repr
objet fichier, 77 fonction de base, 53
objet fichier-compatible, 77 RESTRICTED, 55
objet simili-chemin, 82 retours à la ligne universels, 85
110 Index
Extending and Embedding Python, Version 3.7.1
S
séquence, 84
spécificateur de module, 81
string
object representation, 53
struct sequence, 84
T
Tableau de correspondances, 80
tranche, 84
type, 84
V
variable de classe, 75
variable d’environnement
PYTHONPATH, 60
verrou global de l’interpréteur, 78
vue de dictionnaire, 76
W
WRITE_RESTRICTED, 55
Index 111