@@ -36,7 +36,7 @@ So, if you want to define a new extension type, you need to create a new type
36
36
object.
37
37
38
38
This 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
40
40
extension module :mod: `!custom `:
41
41
42
42
.. note ::
@@ -50,9 +50,9 @@ extension module :mod:`!custom`:
50
50
Now that's quite a bit to take in at once, but hopefully bits will seem familiar
51
51
from the previous chapter. This file defines three things:
52
52
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,
56
56
which defines a set of flags and function pointers that the interpreter
57
57
inspects when specific operations are requested.
58
58
#. 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:
128
128
129
129
Note that the name is a dotted name that includes both the module name and the
130
130
name 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 `.
132
132
Using the real dotted import path is important to make your type compatible
133
133
with the :mod: `pydoc ` and :mod: `pickle ` modules. ::
134
134
135
135
.tp_basicsize = sizeof(CustomObject),
136
136
.tp_itemsize = 0,
137
137
138
138
This 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
140
140
only used for variable-sized objects and should otherwise be zero.
141
141
142
142
.. note ::
@@ -176,7 +176,7 @@ Everything else in the file should be familiar, except for some code in
176
176
if (PyType_Ready(&CustomType) < 0)
177
177
return;
178
178
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
180
180
to the appropriate default values, including :attr: `ob_type ` that we initially
181
181
set to ``NULL ``. ::
182
182
@@ -186,7 +186,7 @@ set to ``NULL``. ::
186
186
}
187
187
188
188
This 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:
190
190
191
191
.. code-block :: pycon
192
192
@@ -237,7 +237,7 @@ adds these capabilities:
237
237
238
238
This version of the module has a number of changes.
239
239
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,
241
241
*first *, *last *, and *number *. The *first * and *last * variables are Python
242
242
strings containing first and last names. The *number * attribute is a C integer.
243
243
@@ -312,7 +312,7 @@ The ``tp_new`` handler is responsible for creating (as opposed to initializing)
312
312
objects of the type. It is exposed in Python as the :meth: `__new__ ` method.
313
313
It is not required to define a ``tp_new `` member, and indeed many extension
314
314
types 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: ` ! Custom ` type above. In this case, we use the ``tp_new ``
316
316
handler to initialize the ``first `` and ``last `` attributes to non-``NULL ``
317
317
default values.
318
318
@@ -468,8 +468,8 @@ concatenation of the first and last names. ::
468
468
return PyUnicode_FromFormat("%S %S", self->first, self->last);
469
469
}
470
470
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
473
473
instance as the first argument. Methods often take positional and keyword
474
474
arguments as well, but in this case we don't take any and don't need to accept
475
475
a positional argument tuple or keyword argument dictionary. This method is
@@ -530,7 +530,7 @@ Providing finer control over data attributes
530
530
============================================
531
531
532
532
In 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
534
534
version of our module, the instance variables :attr: `first ` and :attr: `last `
535
535
could be set to non-string values or even deleted. We want to make sure that
536
536
these attributes always contain strings.
@@ -569,13 +569,13 @@ getting and setting the :attr:`first` attribute::
569
569
return 0;
570
570
}
571
571
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
573
573
a void pointer. In this case, the closure is ignored. (The closure supports an
574
574
advanced usage in which definition data is passed to the getter and setter. This
575
575
could, for example, be used to allow a single set of getter and setter functions
576
576
that decide the attribute to get or set based on data in the closure.)
577
577
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
579
579
closure. The new value may be ``NULL ``, in which case the attribute is being
580
580
deleted. In our setter, we raise an error if the attribute is deleted or if its
581
581
new value is not a string.
@@ -664,11 +664,11 @@ still has a reference from itself. Its reference count doesn't drop to zero.
664
664
Fortunately, Python's cyclic garbage collector will eventually figure out that
665
665
the list is garbage and free it.
666
666
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
668
668
object to be stored in the :attr: `first ` or :attr: `last ` attributes [# ]_.
669
669
Besides, 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:
672
672
673
673
.. code-block :: pycon
674
674
@@ -678,8 +678,8 @@ those two reasons, :class:`Custom` objects can participate in cycles:
678
678
>>> n = Derived()
679
679
>>> n.some_attribute = n
680
680
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
683
683
needs to fill two additional slots and to enable a flag that enables these slots:
684
684
685
685
.. literalinclude :: ../includes/custom4.c
@@ -809,7 +809,7 @@ increases an internal counter:
809
809
.. literalinclude :: ../includes/sublist.c
810
810
811
811
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
813
813
previous sections. We will break down the main differences between them. ::
814
814
815
815
typedef struct {
@@ -875,7 +875,7 @@ slot with :c:func:`PyType_GenericNew` -- the allocation function from the base
875
875
type will be inherited.
876
876
877
877
After 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.
879
879
880
880
881
881
.. rubric :: Footnotes
0 commit comments