4. Construire des extensions C et C++
*************************************

Une extension C pour CPython est une bibliothèque partagée (Un ".so"
sur Linux, un ".pyd" sur Windows), qui expose une *fonction
d'initialisation*.

To be importable, the shared library must be available on
"PYTHONPATH", and must be named after the module name, with an
appropriate extension. When using setuptools, the correct filename is
generated automatically.

La fonction d'initialisation doit avoir le prototype :

PyObject *PyInit_modulename(void)

Elle doit donner soit un module entièrement initialisé, soit une
instance de "PyModuleDef". Voir Initializing C modules pour plus de
détails.

For modules with ASCII-only names, the function must be named
"PyInit_*<name>*", with "<name>" replaced by the name of the module.
When using Multi-phase initialization, non-ASCII module names are
allowed. In this case, the initialization function name is
"PyInitU_*<name>*", with "<name>" encoded using Python's *punycode*
encoding with hyphens replaced by underscores. In Python:

   def initfunc_name(name):
       try:
           suffix = b'_' + name.encode('ascii')
       except UnicodeEncodeError:
           suffix = b'U_' + name.encode('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.


4.1. Building C and C++ Extensions with setuptools
==================================================

Python 3.12 and newer no longer come with distutils. Please refer to
the "setuptools" documentation at
https://setuptools.readthedocs.io/en/latest/setuptools.html to learn
more about how build and distribute C/C++ extensions with setuptools.
