2. Tutoriel : définir des types dans des extensions
***************************************************

Python permet à l'auteur d'un module d'extension C de définir de
nouveaux types qui peuvent être manipulés depuis du code Python, à la
manière des types natifs "str" et "list". Les implémentations de tous
les types d'extension ont des similarités, mais quelques subtilités
doivent être abordées avant de commencer.


2.1. Les bases
==============

*CPython* considère que tous les objets Python sont des variables de
type PyObject*, qui sert de type de base pour tous les objets Python.
La structure de "PyObject" ne contient que le *compteur de références*
et un pointeur vers un objet de type « type de l'objet ». C'est ici
que tout se joue : l'objet type détermine quelle fonction C doit être
appelée par l'interpréteur quand, par exemple, un attribut est
recherché sur un objet, quand une méthode est appelée, ou quand
l'objet est multiplié par un autre objet. Ces fonctions C sont
appelées des « méthodes de type ».

Donc, pour définir un nouveau type dans une extension, vous devez
créer un nouvel objet type.

This sort of thing can only be explained by example, so here's a
minimal, but complete, module that defines a new type named "Custom"
inside a C extension module "custom":

Note:

  ce qui est montré ici est la manière traditionnelle de définir des
  types d'extension *statiques*, et cela convient dans la majorité des
  cas. L'API C permet aussi de définir des types alloués sur le tas,
  via la fonction "PyType_FromSpec()", mais ce n'est pas couvert par
  ce tutoriel.

   #define PY_SSIZE_T_CLEAN
   #include <Python.h>

   typedef struct {
       PyObject_HEAD
       /* Type-specific fields go here. */
   } CustomObject;

   static PyTypeObject CustomType = {
       .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
       .tp_name = "custom.Custom",
       .tp_doc = PyDoc_STR("Custom objects"),
       .tp_basicsize = sizeof(CustomObject),
       .tp_itemsize = 0,
       .tp_flags = Py_TPFLAGS_DEFAULT,
       .tp_new = PyType_GenericNew,
   };

   static int
   custom_module_exec(PyObject *m)
   {
       if (PyType_Ready(&CustomType) < 0) {
           return -1;
       }

       if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
           return -1;
       }

       return 0;
   }

   static PyModuleDef_Slot custom_module_slots[] = {
       {Py_mod_exec, custom_module_exec},
       // Just use this while using static types
       {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
       {0, NULL}
   };

   static PyModuleDef custom_module = {
       .m_base = PyModuleDef_HEAD_INIT,
       .m_name = "custom",
       .m_doc = "Example module that creates an extension type.",
       .m_size = 0,
       .m_slots = custom_module_slots,
   };

   PyMODINIT_FUNC
   PyInit_custom(void)
   {
       return PyModuleDef_Init(&custom_module);
   }

C'est un peu long, mais vous devez déjà reconnaître quelques morceaux
expliqués au chapitre précédent. Ce fichier définit trois choses :

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 define and execute the "custom" module: this is the
   "PyInit_custom" function and the associated "custom_module" struct
   for defining the module, and the "custom_module_exec" function to
   set up a fresh module object.

Commençons par :

   typedef struct {
       PyObject_HEAD
   } CustomObject;

C'est ce qu'un objet "Custom" contient. "PyObject_HEAD" est
obligatoirement au début de chaque structure d'objet et définit un
champ appelé "ob_base" de type "PyObject", contenant un pointeur vers
un objet type et un compteur de références (on peut y accéder en
utilisant les macros "Py_TYPE" et "Py_REFCNT", respectivement). La
raison d'être de ces macros est d'abstraire l'agencement de la
structure, et ainsi de permettre l'ajout de champs en mode débogage.

Note:

  il n'y a pas de point-virgule après la macro "PyObject_HEAD".
  Attention à ne pas l'ajouter par accident : certains compilateurs
  pourraient s'en plaindre.

Bien sûr, les objets ajoutent généralement des données supplémentaires
après l'entête standard "PyObject_HEAD". Par exemple voici la
définition du type standard Python "float" :

   typedef struct {
       PyObject_HEAD
       double ob_fval;
   } PyFloatObject;

La deuxième partie est la définition de l'objet type

   static PyTypeObject CustomType = {
       .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
       .tp_name = "custom.Custom",
       .tp_doc = PyDoc_STR("Custom objects"),
       .tp_basicsize = sizeof(CustomObject),
       .tp_itemsize = 0,
       .tp_flags = Py_TPFLAGS_DEFAULT,
       .tp_new = PyType_GenericNew,
   };

Note:

  nous recommandons d'utiliser la syntaxe d'initialisation nommée
  (C99) pour remplir la structure, comme ci-dessus, afin d'éviter
  d'avoir à lister les champs de "PyTypeObject" dont vous n'avez pas
  besoin, et de ne pas vous soucier de leur ordre.

La définition de "PyTypeObject" dans "object.h" contient en fait bien
plus de champs que la définition ci-dessus. Les champs restants sont
mis à zéro par le compilateur C, et c'est une pratique répandue de ne
pas spécifier les champs dont vous n'avez pas besoin.

Regardons les champs de cette structure, un par un :

   .ob_base = PyVarObject_HEAD_INIT(NULL, 0)

Cette ligne, obligatoire, initialise le champ "ob_base" mentionné
précédemment.

   .tp_name = "custom.Custom",

C'est le nom de notre type. Il apparaît dans la représentation
textuelle par défaut de nos objets, ainsi que dans quelques messages
d'erreur, par exemple :

   >>> "" + custom.Custom()
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   TypeError: can only concatenate str (not "custom.Custom") to str

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.

Nous définissons les drapeaux de la classe à "Py_TPFLAGS_DEFAULT" :

   .tp_flags = Py_TPFLAGS_DEFAULT,

Chaque type doit inclure cette constante dans ses options : elle
active tous les membres définis jusqu'à au moins Python 3.3. Si vous
avez besoin de plus de membres, vous pouvez la combiner à d'autres
constantes avec un *OU* binaire.

Nous fournissons une *docstring* pour ce type via le membre "tp_doc".

   .tp_doc = PyDoc_STR("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 "custom_module_exec()":

   if (PyType_Ready(&CustomType) < 0) {
       return -1;
   }

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".

   if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
       return -1;
   }

This adds the type to the module dictionary.  This allows us to create
"Custom" instances by calling the "Custom" class:

   >>> import custom
   >>> mycustom = custom.Custom()

That's it!  All that remains is to build it; put the above code in a
file called "custom.c",

   [build-system]
   requires = ["setuptools"]
   build-backend = "setuptools.build_meta"

   [project]
   name = "custom"
   version = "1"

in a file called "pyproject.toml", and

   from setuptools import Extension, setup
   setup(ext_modules=[Extension("custom", ["custom.c"])])

dans un fichier appelé "setup.py" ; puis en tapant

   $ python -m pip install .

in a shell should produce a file "custom.so" in a subdirectory and
install it; now fire up Python --- you should be able to "import
custom" and play around with "Custom" objects.

Ce n'était pas si difficile, n'est-ce pas ?

Bien sûr, le type personnalisé actuel est assez inintéressant. Il n'a
pas de données et ne fait rien. Il ne peut même pas être sous-classé.


2.2. ajout de données et de méthodes à l'exemple basique
========================================================

Let's extend the basic example to add some data and methods.  Let's
also make the type usable as a base class. We'll create a new module,
"custom2" that adds these capabilities:

   #define PY_SSIZE_T_CLEAN
   #include <Python.h>
   #include <stddef.h> /* for offsetof() */

   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;

       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
                                        &first, &last,
                                        &self->number))
           return -1;

       if (first) {
           Py_XSETREF(self->first, Py_NewRef(first));
       }
       if (last) {
           Py_XSETREF(self->last, Py_NewRef(last));
       }
       return 0;
   }

   static PyMemberDef Custom_members[] = {
       {"first", Py_T_OBJECT_EX, offsetof(CustomObject, first), 0,
        "first name"},
       {"last", Py_T_OBJECT_EX, offsetof(CustomObject, last), 0,
        "last name"},
       {"number", Py_T_INT, offsetof(CustomObject, number), 0,
        "custom number"},
       {NULL}  /* Sentinel */
   };

   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);
   }

   static PyMethodDef Custom_methods[] = {
       {"name", (PyCFunction) Custom_name, METH_NOARGS,
        "Return the name, combining the first and last name"
       },
       {NULL}  /* Sentinel */
   };

   static PyTypeObject CustomType = {
       .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
       .tp_name = "custom2.Custom",
       .tp_doc = PyDoc_STR("Custom objects"),
       .tp_basicsize = sizeof(CustomObject),
       .tp_itemsize = 0,
       .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
       .tp_new = Custom_new,
       .tp_init = (initproc) Custom_init,
       .tp_dealloc = (destructor) Custom_dealloc,
       .tp_members = Custom_members,
       .tp_methods = Custom_methods,
   };

   static int
   custom_module_exec(PyObject *m)
   {
       if (PyType_Ready(&CustomType) < 0) {
           return -1;
       }

       if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
           return -1;
       }

       return 0;
   }

   static PyModuleDef_Slot custom_module_slots[] = {
       {Py_mod_exec, custom_module_exec},
       {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
       {0, NULL}
   };

   static PyModuleDef custom_module = {
       .m_base = PyModuleDef_HEAD_INIT,
       .m_name = "custom2",
       .m_doc = "Example module that creates an extension type.",
       .m_size = 0,
       .m_slots = custom_module_slots,
   };

   PyMODINIT_FUNC
   PyInit_custom2(void)
   {
       return PyModuleDef_Init(&custom_module);
   }

Cette version du module comporte un certain nombre de modifications.

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.

La structure de l'objet est mise à jour en conséquence

   typedef struct {
       PyObject_HEAD
       PyObject *first; /* first name */
       PyObject *last;  /* last name */
       int number;
   } CustomObject;

Comme nous avons maintenant des données à gérer, nous devons faire
plus attention à l'allocation et à la libération d'objets. Au minimum,
nous avons besoin d'une méthode de dés-allocation

   static void
   Custom_dealloc(CustomObject *self)
   {
       Py_XDECREF(self->first);
       Py_XDECREF(self->last);
       Py_TYPE(self)->tp_free((PyObject *) self);
   }

qui est assignée au membre "tp_dealloc"

   .tp_dealloc = (destructor) Custom_dealloc,

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:

  La conversion explicite en "destructor" ci-dessus est nécessaire car
  nous avons défini "Custom_dealloc" pour prendre un argument
  "CustomObject *", mais le pointeur de fonction "tp_dealloc" s'attend
  à recevoir un argument "PyObject *". Sinon, le compilateur émet un
  avertissement. C'est du polymorphisme orienté objet, en C !

Nous voulons nous assurer que le prénom et le nom sont initialisés
avec des chaînes vides, nous fournissons donc une implémentation
"tp_new"

   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;
   }

et installez-le dans le membre "tp_new" :

   .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" reçoit le type en cours d'instanciation (pas nécessairement
"CustomType", si une sous-classe est instanciée) et tous les arguments
passés lorsque le type a été appelé, et devrait renvoyer l'instance
créée. Les gestionnaires "tp_new" acceptent toujours les arguments
positionnels et nommés, mais ils ignorent souvent les arguments,
laissant la gestion des arguments aux méthodes d'initialisation (alias
"tp_init" en C ou "__init__" en Python).

Note:

  "tp_new" ne doit pas appeler "tp_init" explicitement, car
  l'interpréteur le fera lui-même.

L'implémentation "tp_new" appelle l'emplacement "tp_alloc" pour
allouer de la mémoire :

   self = (CustomObject *) type->tp_alloc(type, 0);

Puisque l'allocation de mémoire peut échouer, nous devons vérifier le
résultat "tp_alloc" par rapport à "NULL" avant de continuer.

Note:

  Nous n'avons pas rempli l'emplacement "tp_alloc" nous-mêmes. C'est
  "PyType_Ready()" qui le remplit pour nous car il en hérite de notre
  classe mère, qui est "object" par défaut. La plupart des types
  utilisent la stratégie d'allocation par défaut.

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".)

Nous définissons également une fonction d'initialisation qui accepte
des arguments pour fournir des valeurs initiales pour notre 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 (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
                                        &first, &last,
                                        &self->number))
           return -1;

       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;
   }

en remplissant l'emplacement "tp_init".

   .tp_init = (initproc) Custom_init,

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;
   }

Mais ce serait risqué. Notre type ne limite pas le type du membre
"first", il peut donc s'agir de n'importe quel type d'objet. Il
pourrait avoir un destructeur qui provoque l'exécution de code
essayant d'accéder au membre "first" ; ou ce destructeur pourrait
libérer le *Global interpreter Lock* et laisser du code arbitraire
s'exécuter dans d'autres threads qui accèdent et modifient notre
objet.

Dans une optique paranoïaque et se prémunir contre cette éventualité,
on réaffecte presque toujours les membres avant de décrémenter leur
compteur de références. Quand ne devons-nous pas faire cela ?

* lorsque l'on est sûr que le compteur de références est supérieur à 1
  ;

* lorsque nous savons que la libération de la mémoire de l'objet [1]
  ne libérera pas le *GIL* ni ne provoquera de rappel dans le code de
  notre type ;

* lors de la décrémentation d'un compteur de références dans un
  gestionnaire "tp_dealloc" sur un type qui ne prend pas en charge le
  ramasse-miettes cyclique [2].

Nous voulons exposer nos variables d'instance en tant qu'attributs. Il
existe plusieurs façons de le faire. Le moyen le plus simple consiste
à définir des définitions de membres :

   static PyMemberDef Custom_members[] = {
       {"first", Py_T_OBJECT_EX, offsetof(CustomObject, first), 0,
        "first name"},
       {"last", Py_T_OBJECT_EX, offsetof(CustomObject, last), 0,
        "last name"},
       {"number", Py_T_INT, offsetof(CustomObject, number), 0,
        "custom number"},
       {NULL}  /* Sentinel */
   };

et placer les définitions dans l'emplacement "tp_members"

   .tp_members = Custom_members,

Chaque définition de membre possède un nom, un type, un décalage, des
indicateurs d'accès et une chaîne de documentation. Voir la section
Gestion des attributs génériques ci-dessous pour plus de détails.

Un inconvénient de cette approche est qu'elle ne permet pas de
restreindre les types d'objets pouvant être affectés aux attributs
Python. Nous nous attendons à ce que le prénom et le nom soient des
chaînes, mais tous les objets Python peuvent être affectés. De plus,
les attributs peuvent être supprimés, en définissant les pointeurs C
sur "NULL". Même si nous pouvons nous assurer que les membres sont
initialisés avec des valeurs non "NULL", les membres peuvent être
définis sur "NULL" si les attributs sont supprimés.

We define a single method, "Custom.name()", that outputs the objects
name as the concatenation of the first and last names.

   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);
   }

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" % (self.first, self.last)

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.

Maintenant que nous avons défini la méthode, nous devons créer un
tableau de définitions de méthode

   static PyMethodDef Custom_methods[] = {
       {"name", (PyCFunction) Custom_name, METH_NOARGS,
        "Return the name, combining the first and last name"
       },
       {NULL}  /* Sentinel */
   };

(notez que nous avons utilisé le drapeau "METH_NOARGS" pour indiquer
que la méthode n'attend aucun argument autre que *self*)

et assignons-le à l'emplacement "tp_methods"

   .tp_methods = Custom_methods,

Enfin, nous rendons notre type utilisable comme classe de base pour le
sous-classement. Nous avons écrit nos méthodes avec soin jusqu'à
présent afin qu'elles ne fassent aucune hypothèse sur le type de
l'objet créé ou utilisé, donc tout ce que nous avons à faire est
d'ajouter la macro "Py_TPFLAGS_BASETYPE" à notre définition
d'indicateur de classe :

   .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,

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 "setup.py" file to include the new module,

   from setuptools import Extension, setup
   setup(ext_modules=[
       Extension("custom", ["custom.c"]),
       Extension("custom2", ["custom2.c"]),
   ])

and then we re-install so that we can "import custom2":

   $ python -m pip install .


2.3. Contrôle précis sur les attributs de données
=================================================

In this section, we'll provide finer control over how the "first" and
"last" attributes are set in the "Custom" example. In the previous
version of our module, the instance variables "first" and "last" could
be set to non-string values or even deleted. We want to make sure that
these attributes always contain strings.

   #define PY_SSIZE_T_CLEAN
   #include <Python.h>
   #include <stddef.h> /* for offsetof() */

   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;

       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|UUi", kwlist,
                                        &first, &last,
                                        &self->number))
           return -1;

       if (first) {
           Py_SETREF(self->first, Py_NewRef(first));
       }
       if (last) {
           Py_SETREF(self->last, Py_NewRef(last));
       }
       return 0;
   }

   static PyMemberDef Custom_members[] = {
       {"number", Py_T_INT, offsetof(CustomObject, number), 0,
        "custom number"},
       {NULL}  /* Sentinel */
   };

   static PyObject *
   Custom_getfirst(CustomObject *self, void *closure)
   {
       return Py_NewRef(self->first);
   }

   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_SETREF(self->first, Py_NewRef(value));
       return 0;
   }

   static PyObject *
   Custom_getlast(CustomObject *self, void *closure)
   {
       return Py_NewRef(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_SETREF(self->last, Py_NewRef(value));
       return 0;
   }

   static PyGetSetDef Custom_getsetters[] = {
       {"first", (getter) Custom_getfirst, (setter) Custom_setfirst,
        "first name", NULL},
       {"last", (getter) Custom_getlast, (setter) Custom_setlast,
        "last name", NULL},
       {NULL}  /* Sentinel */
   };

   static PyObject *
   Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
   {
       return PyUnicode_FromFormat("%S %S", self->first, self->last);
   }

   static PyMethodDef Custom_methods[] = {
       {"name", (PyCFunction) Custom_name, METH_NOARGS,
        "Return the name, combining the first and last name"
       },
       {NULL}  /* Sentinel */
   };

   static PyTypeObject CustomType = {
       .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
       .tp_name = "custom3.Custom",
       .tp_doc = PyDoc_STR("Custom objects"),
       .tp_basicsize = sizeof(CustomObject),
       .tp_itemsize = 0,
       .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
       .tp_new = Custom_new,
       .tp_init = (initproc) Custom_init,
       .tp_dealloc = (destructor) Custom_dealloc,
       .tp_members = Custom_members,
       .tp_methods = Custom_methods,
       .tp_getset = Custom_getsetters,
   };

   static int
   custom_module_exec(PyObject *m)
   {
       if (PyType_Ready(&CustomType) < 0) {
           return -1;
       }

       if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
           return -1;
       }

       return 0;
   }

   static PyModuleDef_Slot custom_module_slots[] = {
       {Py_mod_exec, custom_module_exec},
       {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
       {0, NULL}
   };

   static PyModuleDef custom_module = {
       .m_base = PyModuleDef_HEAD_INIT,
       .m_name = "custom3",
       .m_doc = "Example module that creates an extension type.",
       .m_size = 0,
       .m_slots = custom_module_slots,
   };

   PyMODINIT_FUNC
   PyInit_custom3(void)
   {
       return PyModuleDef_Init(&custom_module);
   }

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.

Nous créons un tableau de structures "PyGetSetDef"

   static PyGetSetDef Custom_getsetters[] = {
       {"first", (getter) Custom_getfirst, (setter) Custom_setfirst,
        "first name", NULL},
       {"last", (getter) Custom_getlast, (setter) Custom_setlast,
        "last name", NULL},
       {NULL}  /* Sentinel */
   };

et l'enregistrons dans l'emplacement "tp_getset"

   .tp_getset = Custom_getsetters,

Le dernier élément d'une structure "PyGetSetDef" est la fermeture
mentionnée ci-dessus. Dans ce cas, nous n'utilisons pas de fermeture,
nous passons donc simplement "NULL".

Nous supprimons également les définitions de membre pour ces attributs
:

   static PyMemberDef Custom_members[] = {
       {"number", Py_T_INT, offsetof(CustomObject, number), 0,
        "custom number"},
       {NULL}  /* Sentinel */
   };

Nous devons également mettre à jour le gestionnaire "tp_init" pour
autoriser uniquement le passage des chaînes [3]

   static int
   Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
   {
       static char *kwlist[] = {"first", "last", "number", NULL};
       PyObject *first = NULL, *last = NULL, *tmp;

       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|UUi", kwlist,
                                        &first, &last,
                                        &self->number))
           return -1;

       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;
   }

Avec ces modifications, nous pouvons garantir que les membres "first"
et "last" ne sont jamais "NULL", nous pouvons donc supprimer les
vérifications des valeurs "NULL" dans presque tous les cas. Cela
signifie que la plupart des appels "Py_XDECREF()" peuvent être
convertis en appels "Py_DECREF()". Le seul endroit où nous ne pouvons
pas modifier ces appels est dans l'implémentation de "tp_dealloc", où
il est possible que l'initialisation de ces membres ait échoué dans
"tp_new".

Nous renommons également la fonction d'initialisation du module et le
nom du module dans la fonction d'initialisation, comme nous l'avons
fait précédemment, et nous ajoutons une définition supplémentaire au
fichier "setup.py".


2.4. Prise en charge du ramasse-miettes cyclique
================================================

Python a un *ramasse-miettes cyclique* qui peut identifier les objets
inutiles même lorsque leur compteur de références n'est pas nul. Cela
peut se produire lorsque des objets sont impliqués dans des cycles.
Par exemple, considérons :

   >>> l = []
   >>> l.append(l)
   >>> del l

Dans cet exemple, nous créons une liste qui se contient elle-même.
Lorsque nous la supprimons, il existe toujours une référence à elle-
même. Son compteur de références ne tombe pas à zéro. Heureusement, le
ramasse-miettes cyclique de Python finira par comprendre que la liste
est un déchet et la libérera.

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:

   >>> import custom3
   >>> class Derived(custom3.Custom): pass
   ...
   >>> n = Derived()
   >>> n.some_attribute = n

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:

   #define PY_SSIZE_T_CLEAN
   #include <Python.h>
   #include <stddef.h> /* for offsetof() */

   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 *
   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;

       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|UUi", kwlist,
                                        &first, &last,
                                        &self->number))
           return -1;

       if (first) {
           Py_SETREF(self->first, Py_NewRef(first));
       }
       if (last) {
           Py_SETREF(self->last, Py_NewRef(last));
       }
       return 0;
   }

   static PyMemberDef Custom_members[] = {
       {"number", Py_T_INT, offsetof(CustomObject, number), 0,
        "custom number"},
       {NULL}  /* Sentinel */
   };

   static PyObject *
   Custom_getfirst(CustomObject *self, void *closure)
   {
       return Py_NewRef(self->first);
   }

   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_XSETREF(self->first, Py_NewRef(value));
       return 0;
   }

   static PyObject *
   Custom_getlast(CustomObject *self, void *closure)
   {
       return Py_NewRef(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_XSETREF(self->last, Py_NewRef(value));
       return 0;
   }

   static PyGetSetDef Custom_getsetters[] = {
       {"first", (getter) Custom_getfirst, (setter) Custom_setfirst,
        "first name", NULL},
       {"last", (getter) Custom_getlast, (setter) Custom_setlast,
        "last name", NULL},
       {NULL}  /* Sentinel */
   };

   static PyObject *
   Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
   {
       return PyUnicode_FromFormat("%S %S", self->first, self->last);
   }

   static PyMethodDef Custom_methods[] = {
       {"name", (PyCFunction) Custom_name, METH_NOARGS,
        "Return the name, combining the first and last name"
       },
       {NULL}  /* Sentinel */
   };

   static PyTypeObject CustomType = {
       .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
       .tp_name = "custom4.Custom",
       .tp_doc = PyDoc_STR("Custom objects"),
       .tp_basicsize = sizeof(CustomObject),
       .tp_itemsize = 0,
       .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
       .tp_new = Custom_new,
       .tp_init = (initproc) Custom_init,
       .tp_dealloc = (destructor) Custom_dealloc,
       .tp_traverse = (traverseproc) Custom_traverse,
       .tp_clear = (inquiry) Custom_clear,
       .tp_members = Custom_members,
       .tp_methods = Custom_methods,
       .tp_getset = Custom_getsetters,
   };

   static int
   custom_module_exec(PyObject *m)
   {
       if (PyType_Ready(&CustomType) < 0) {
           return -1;
       }

       if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
           return -1;
       }

       return 0;
   }

   static PyModuleDef_Slot custom_module_slots[] = {
       {Py_mod_exec, custom_module_exec},
       {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
       {0, NULL}
   };

   static PyModuleDef custom_module = {
       .m_base = PyModuleDef_HEAD_INIT,
       .m_name = "custom4",
       .m_doc = "Example module that creates an extension type.",
       .m_size = 0,
       .m_slots = custom_module_slots,
   };

   PyMODINIT_FUNC
   PyInit_custom4(void)
   {
       return PyModuleDef_Init(&custom_module);
   }

Tout d'abord, la méthode de parcours (*Custom_traverse*) permet au
ramasse-miettes cyclique de connaître les sous-objets qui pourraient
conduire à des 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 fournit une macro "Py_VISIT()" qui automatise l'appel des
fonctions de visite. Avec "Py_VISIT()", nous pouvons minimiser la
quantité de code générique dans "Custom_traverse"

   static int
   Custom_traverse(CustomObject *self, visitproc visit, void *arg)
   {
       Py_VISIT(self->first);
       Py_VISIT(self->last);
       return 0;
   }

Note:

  l'implémentation "tp_traverse" doit nommer ses arguments exactement
  *visit* et *arg* afin d'utiliser "Py_VISIT()".

Deuxièmement, nous devons fournir une méthode pour effacer tous les
sous-objets qui peuvent conduire à des cycles

   static int
   Custom_clear(CustomObject *self)
   {
       Py_CLEAR(self->first);
       Py_CLEAR(self->last);
       return 0;
   }

Notez l'utilisation de la macro "Py_CLEAR()". C'est le moyen
recommandé et sûr d'effacer les attributs de données de types
arbitraires tout en décrémentant leur compteur de références. Si vous
deviez appeler "Py_XDECREF()" à la place sur l'attribut avant de le
définir sur "NULL", il est possible que le destructeur de l'attribut
appelle du code qui lit à nouveau l'attribut (*surtout* s'il existe un
cycle de références).

Note:

  vous pouvez émuler "Py_CLEAR()" en écrivant

     PyObject *tmp;
     tmp = self->first;
     self->first = NULL;
     Py_XDECREF(tmp);

  Néanmoins, il est beaucoup plus facile et moins sujet aux erreurs de
  toujours utiliser "Py_CLEAR()" lors de la suppression d'un attribut.
  N'essayez pas de micro-optimiser au détriment de la robustesse !

La fonction de libération de la mémoire "Custom_dealloc" peut appeler
du code arbitraire lors de la suppression des attributs. Cela signifie
que le ramasse-miettes cyclique peut être déclenché à l'intérieur de
la fonction. Étant donné que le ramasse-miettes suppose que le nombre
de références n'est pas nul, nous devons annuler le suivi de l'objet
par le ramasse-miettes en appelant "PyObject_GC_UnTrack()" avant
d'effacer les membres. Voici notre fonction de libération de la
mémoire réimplémentée en utilisant "PyObject_GC_UnTrack()" et
"Custom_clear" :

   static void
   Custom_dealloc(CustomObject *self)
   {
       PyObject_GC_UnTrack(self);
       Custom_clear(self);
       Py_TYPE(self)->tp_free((PyObject *) self);
   }

Enfin, nous ajoutons le drapeau "Py_TPFLAGS_HAVE_GC" aux drapeaux de
la classe :

   .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,

C'est à peu près tout. Si nous avions écrit des gestionnaires
personnalisés "tp_alloc" ou "tp_free", nous aurions besoin de les
modifier pour le ramasse-miettes cyclique. La plupart des extensions
utilisent les versions fournies automatiquement.


2.5. Sous-classement d'autres types
===================================

Il est possible de créer de nouveaux types d'extension dérivés de
types existants. Il est plus facile d'hériter des types natifs, car
une extension peut facilement utiliser le "PyTypeObject" dont elle a
besoin. Il peut être difficile de partager ces structures
"PyTypeObject" entre modules d'extension.

In this example we will create a "SubList" type that inherits from the
built-in "list" type. The new type will be completely compatible with
regular lists, but will have an additional "increment()" method that
increases an internal counter:

   >>> import sublist
   >>> s = sublist.SubList(range(3))
   >>> s.extend(s)
   >>> print(len(s))
   6
   >>> print(s.increment())
   1
   >>> print(s.increment())
   2

   #define PY_SSIZE_T_CLEAN
   #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 PyMethodDef SubList_methods[] = {
       {"increment", (PyCFunction) SubList_increment, METH_NOARGS,
        PyDoc_STR("increment state counter")},
       {NULL},
   };

   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;
   }

   static PyTypeObject SubListType = {
       .ob_base = PyVarObject_HEAD_INIT(NULL, 0)
       .tp_name = "sublist.SubList",
       .tp_doc = PyDoc_STR("SubList objects"),
       .tp_basicsize = sizeof(SubListObject),
       .tp_itemsize = 0,
       .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
       .tp_init = (initproc) SubList_init,
       .tp_methods = SubList_methods,
   };

   static int
   sublist_module_exec(PyObject *m)
   {
       SubListType.tp_base = &PyList_Type;
       if (PyType_Ready(&SubListType) < 0) {
           return -1;
       }

       if (PyModule_AddObjectRef(m, "SubList", (PyObject *) &SubListType) < 0) {
           return -1;
       }

       return 0;
   }

   static PyModuleDef_Slot sublist_module_slots[] = {
       {Py_mod_exec, sublist_module_exec},
       {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
       {0, NULL}
   };

   static PyModuleDef sublist_module = {
       .m_base = PyModuleDef_HEAD_INIT,
       .m_name = "sublist",
       .m_doc = "Example module that creates an extension type.",
       .m_size = 0,
       .m_slots = sublist_module_slots,
   };

   PyMODINIT_FUNC
   PyInit_sublist(void)
   {
       return PyModuleDef_Init(&sublist_module);
   }

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;

La principale différence pour les objets d'un type dérivé est que la
structure d'objet du type père doit être la première valeur. Le type
père inclut déjà le "PyObject_HEAD()" au début de sa 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.

Ce modèle est important lors de l'écriture d'un type avec des membres
personnalisés "tp_new" et "tp_dealloc". Le gestionnaire "tp_new" ne
doit pas réellement allouer la mémoire pour l'objet avec son
"tp_alloc", mais laisser la classe mère gérer ça en appelant sa propre
"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 in the "Py_mod_exec" function:

   static int
   sublist_module_exec(PyObject *m)
   {
       SubListType.tp_base = &PyList_Type;
       if (PyType_Ready(&SubListType) < 0) {
           return -1;
       }

       if (PyModule_AddObjectRef(m, "SubList", (PyObject *) &SubListType) < 0) {
           return -1;
       }

       return 0;
   }

Avant d'appeler "PyType_Ready()", la structure de type doit avoir
l'emplacement "tp_base" rempli. Lorsque nous dérivons un type
existant, il n'est pas nécessaire de remplir l'emplacement "tp_alloc"
avec "PyType_GenericNew()" – la fonction d'allocation du type père
sera héritée.

After that, calling "PyType_Ready()" and adding the type object to the
module is the same as with the basic "Custom" examples.

-[ Notes ]-

[1] C'est vrai lorsque nous savons que l'objet est un type de base,
    comme une chaîne ou un flottant.

[2] Nous nous sommes appuyés sur le gestionnaire "tp_dealloc" dans cet
    exemple, car notre type ne prend pas en charge le ramasse-miettes.

[3] Nous savons maintenant que les premier et dernier membres sont des
    chaînes, nous pourrions donc peut-être être moins prudents quant à
    la décrémentation de leur nombre de références, cependant, nous
    acceptons les instances de sous-classes de chaînes. Même si la
    libération de la mémoire des chaînes normales ne rappellera pas
    nos objets, nous ne pouvons pas garantir que la libération de
    mémoire d'une instance d'une sous-classe de chaîne ne rappellera
    pas nos objets.

[4] De plus, même avec nos attributs limités aux instances de chaînes,
    l'utilisateur pourrait passer des sous-classes arbitraires "str"
    et donc encore créer des références cycliques.
