@@ -36,7 +36,7 @@ So, if you want to define a new extension type, you need to create a new type
3636object.
3737
3838This sort of thing can only be explained by example, so here's a minimal, but
39- complete, module that defines a new type named :class: `Custom ` inside a C
39+ complete, module that defines a new type named :class: `! Custom ` inside a C
4040extension module :mod: `!custom `:
4141
4242.. note ::
@@ -50,9 +50,9 @@ extension module :mod:`!custom`:
5050Now that's quite a bit to take in at once, but hopefully bits will seem familiar
5151from the previous chapter. This file defines three things:
5252
53- #. What a :class: `Custom ` **object ** contains: this is the ``CustomObject ``
54- struct, which is allocated once for each :class: `Custom ` instance.
55- #. How the :class: `Custom ` **type ** behaves: this is the ``CustomType `` struct,
53+ #. What a :class: `! Custom ` **object ** contains: this is the ``CustomObject ``
54+ struct, which is allocated once for each :class: `! Custom ` instance.
55+ #. How the :class: `! Custom ` **type ** behaves: this is the ``CustomType `` struct,
5656 which defines a set of flags and function pointers that the interpreter
5757 inspects when specific operations are requested.
5858#. How to initialize the :mod: `!custom ` module: this is the ``PyInit_custom ``
@@ -128,15 +128,15 @@ our objects and in some error messages, for example:
128128
129129 Note that the name is a dotted name that includes both the module name and the
130130name of the type within the module. The module in this case is :mod: `!custom ` and
131- the type is :class: `Custom `, so we set the type name to :class: `custom.Custom `.
131+ the type is :class: `! Custom `, so we set the type name to :class: `custom.Custom `.
132132Using the real dotted import path is important to make your type compatible
133133with the :mod: `pydoc ` and :mod: `pickle ` modules. ::
134134
135135 .tp_basicsize = sizeof(CustomObject),
136136 .tp_itemsize = 0,
137137
138138This is so that Python knows how much memory to allocate when creating
139- new :class: `Custom ` instances. :c:member: `~PyTypeObject.tp_itemsize ` is
139+ new :class: `! Custom ` instances. :c:member: `~PyTypeObject.tp_itemsize ` is
140140only used for variable-sized objects and should otherwise be zero.
141141
142142.. note ::
@@ -176,7 +176,7 @@ Everything else in the file should be familiar, except for some code in
176176 if (PyType_Ready(&CustomType) < 0)
177177 return;
178178
179- This initializes the :class: `Custom ` type, filling in a number of members
179+ This initializes the :class: `! Custom ` type, filling in a number of members
180180to the appropriate default values, including :attr: `ob_type ` that we initially
181181set to ``NULL ``. ::
182182
@@ -186,7 +186,7 @@ set to ``NULL``. ::
186186 }
187187
188188This adds the type to the module dictionary. This allows us to create
189- :class: `Custom ` instances by calling the :class: `Custom ` class:
189+ :class: `! Custom ` instances by calling the :class: `! Custom ` class:
190190
191191.. code-block :: pycon
192192
@@ -237,7 +237,7 @@ adds these capabilities:
237237
238238This version of the module has a number of changes.
239239
240- The :class: `Custom ` type now has three data attributes in its C struct,
240+ The :class: `! Custom ` type now has three data attributes in its C struct,
241241*first *, *last *, and *number *. The *first * and *last * variables are Python
242242strings containing first and last names. The *number * attribute is a C integer.
243243
@@ -312,7 +312,7 @@ The ``tp_new`` handler is responsible for creating (as opposed to initializing)
312312objects of the type. It is exposed in Python as the :meth: `__new__ ` method.
313313It is not required to define a ``tp_new `` member, and indeed many extension
314314types will simply reuse :c:func: `PyType_GenericNew ` as done in the first
315- version of the `` Custom ` ` type above. In this case, we use the ``tp_new ``
315+ version of the :class: `<
3797
/span>! Custom ` type above. In this case, we use the ``tp_new ``
316316handler to initialize the ``first `` and ``last `` attributes to non-``NULL ``
317317default values.
318318
@@ -468,8 +468,8 @@ concatenation of the first and last names. ::
468468 return PyUnicode_FromFormat("%S %S", self->first, self->last);
469469 }
470470
471- The method is implemented as a C function that takes a :class: `Custom ` (or
472- :class: `Custom ` subclass) instance as the first argument. Methods always take an
471+ The method is implemented as a C function that takes a :class: `! Custom ` (or
472+ :class: `! Custom ` subclass) instance as the first argument. Methods always take an
473473instance as the first argument. Methods often take positional and keyword
474474arguments as well, but in this case we don't take any and don't need to accept
475475a positional argument tuple or keyword argument dictionary. This method is
@@ -530,7 +530,7 @@ Providing finer control over data attributes
530530============================================
531531
532532In this section, we'll provide finer control over how the :attr: `first ` and
533- :attr: `last ` attributes are set in the :class: `Custom ` example. In the previous
533+ :attr: `last ` attributes are set in the :class: `! Custom ` example. In the previous
534534version of our module, the instance variables :attr: `first ` and :attr: `last `
535535could be set to non-string values or even deleted. We want to make sure that
536536these attributes always contain strings.
@@ -569,13 +569,13 @@ getting and setting the :attr:`first` attribute::
569569 return 0;
570570 }
571571
572- The getter function is passed a :class: `Custom ` object and a "closure", which is
572+ The getter function is passed a :class: `! Custom ` object and a "closure", which is
573573a void pointer. In this case, the closure is ignored. (The closure supports an
574574advanced usage in which definition data is passed to the getter and setter. This
575575could, for example, be used to allow a single set of getter and setter functions
576576that decide the attribute to get or set based on data in the closure.)
577577
578- The setter function is passed the :class: `Custom ` object, the new value, and the
578+ The setter function is passed the :class: `! Custom ` object, the new value, and the
579579closure. The new value may be ``NULL ``, in which case the attribute is being
580580deleted. In our setter, we raise an error if the attribute is deleted or if its
581581new value is not a string.
@@ -664,11 +664,11 @@ still has a reference from itself. Its reference count doesn't drop to zero.
664664Fortunately, Python's cyclic garbage collector will eventually figure out that
665665the list is garbage and free it.
666666
667- In the second version of the :class: `Custom ` example, we allowed any kind of
667+ In the second version of the :class: `! Custom ` example, we allowed any kind of
668668object to be stored in the :at
3797
tr: `first ` or :attr: `last ` attributes [# ]_.
669669Besides, in the second and third versions, we allowed subclassing
670- :class: `Custom `, and subclasses may add arbitrary attributes. For any of
671- those two reasons, :class: `Custom ` objects can participate in cycles:
670+ :class: `! Custom `, and subclasses may add arbitrary attributes. For any of
671+ those two reasons, :class: `! Custom ` objects can participate in cycles:
672672
673673.. code-block :: pycon
674674
@@ -678,8 +678,8 @@ those two reasons, :class:`Custom` objects can participate in cycles:
678678 >>> n = Derived()
679679 >>> n.some_attribute = n
680680
681- To allow a :class: `Custom ` instance participating in a reference cycle to
682- be properly detected and collected by the cyclic GC, our :class: `Custom ` type
681+ To allow a :class: `! Custom ` instance participating in a reference cycle to
682+ be properly detected and collected by the cyclic GC, our :class: `! Custom ` type
683683needs to fill two additional slots and to enable a flag that enables these slots:
684684
685685.. literalinclude :: ../includes/custom4.c
@@ -809,7 +809,7 @@ increases an internal counter:
809809 .. literalinclude :: ../includes/sublist.c
810810
811811
812- As you can see, the source code closely resembles the :class: `Custom ` examples in
812+ As you can see, the source code closely resembles the :class: `! Custom ` examples in
813813previous sections. We will break down the main differences between them. ::
814814
815815 typedef struct {
@@ -875,7 +875,7 @@ slot with :c:func:`PyType_GenericNew` -- the allocation function from the base
875875type will be inherited.
876876
877877After that, calling :c:func: `PyType_Ready ` and adding the type object to the
878- module is the same as with the basic :class: `Custom ` examples.
878+ module is the same as with the basic :class: `! Custom ` examples.
879879
880880
881881.. rubric :: Footnotes
0 commit comments