@@ -145,7 +145,7 @@ only used for variable-sized objects and should otherwise be zero.
145
145
:c:member: `~PyTypeObject.tp_basicsize ` as its base type, you may have problems with multiple
146
146
inheritance. A Python subclass of your type will have to list your type first
147
147
in its :attr: `~class.__bases__ `, or else it will not be able to call your type's
148
- :meth: `__new__ ` method without getting an error. You can avoid this problem by
148
+ :meth: `~object. __new__ ` method without getting an error. You can avoid this problem by
149
149
ensuring that your type has a larger value for :c:member: `~PyTypeObject.tp_basicsize ` than its
150
150
base type does. Most of the time, this will be true anyway, because either your
151
151
base type will be :class: `object `, or else you will be adding data members to
@@ -164,14 +164,14 @@ We provide a doc string for the type in :c:member:`~PyTypeObject.tp_doc`. ::
164
164
.tp_doc = PyDoc_STR("Custom objects"),
165
165
166
166
To enable object creation, we have to provide a :c:member: `~PyTypeObject.tp_new `
167
- handler. This is the equivalent of the Python method :meth: `__new__ `, but
167
+ handler. This is the equivalent of the Python method :meth: `~object. __new__ `, but
168
168
has to be specified explicitly. In this case, we can just use the default
169
169
implementation provided by the API function :c:func: `PyType_GenericNew `. ::
170
170
171
171
.tp_new = PyType_GenericNew,
172
172
173
173
Everything else in the file should be familiar, except for some code in
174
- :c:func: `PyInit_custom `::
174
+ :c:func: `! PyInit_custom `::
175
175
176
176
if (PyType_Ready(&CustomType) < 0)
177
177
return;
@@ -218,7 +218,7 @@ Of course, the current Custom type is pretty uninteresting. It has no data and
218
218
doesn't do anything. It can't even be subclassed.
219
219
220
220
.. note ::
221
- While this documentation showcases the standard :mod: `distutils ` module
221
+ While this documentation showcases the standard :mod: `! distutils ` module
222
222
for building C extensions, it is recommended in real-world use cases to
223
223
use the newer and better-maintained ``setuptools `` library. Documentation
224
224
on how to do this is out of scope for this document and can be found in
@@ -270,7 +270,7 @@ This method first clears the reference counts of the two Python attributes.
270
270
``NULL `` (which might happen here if ``tp_new `` failed midway). It then
271
271
calls the :c:member: `~PyTypeObject.tp_free ` member of the object's type
272
272
(computed by ``Py_TYPE(self) ``) to free the object's memory. Note that
273
- the object's type might not be :class: `CustomType `, because the object may
273
+ the object's type might not be :class: `! CustomType `, because the object may
274
274
be an instance of a subclass.
275
275
276
276
.. note ::
@@ -309,7 +309,7 @@ and install it in the :c:member:`~PyTypeObject.tp_new` member::
309
309
.tp_new = Custom_new,
310
310
311
311
The ``tp_new `` handler is responsible for creating (as opposed to initializing)
312
- objects of the type. It is exposed in Python as the :meth: `__new__ ` method.
312
+ objects of the type. It is exposed in Python as the :meth: `~object. __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
315
version of the :class: `!Custom ` type above. In this case, we use the ``tp_new ``
@@ -343,7 +343,7 @@ result against ``NULL`` before proceeding.
343
343
344
344
.. note ::
345
345
If you are creating a co-operative :c:member: `~PyTypeObject.tp_new ` (one
346
- that calls a base type's :c:member: `~PyTypeObject.tp_new ` or :meth: `__new__ `),
346
+ that calls a base type's :c:member: `~PyTypeObject.tp_new ` or :meth: `~object. __new__ `),
347
347
you must *not * try to determine what method to call using method resolution
348
348
order at runtime. Always statically determine what type you are going to
349
349
call, and call its :c:member: `~PyTypeObject.tp_new ` directly, or via
@@ -386,14 +386,14 @@ by filling the :c:member:`~PyTypeObject.tp_init` slot. ::
386
386
.tp_init = (initproc) Custom_init,
387
387
388
388
The :c:member: `~PyTypeObject.tp_init ` slot is exposed in Python as the
389
- :meth: `__init__ ` method. It is used to initialize an object after it's
389
+ :meth: `~object. __init__ ` method. It is used to initialize an object after it's
390
390
created. Initializers always accept positional and keyword arguments,
391
391
and they should return either ``0 `` on success or ``-1 `` on error.
392
392
393
393
Unlike the ``tp_new `` handler, there is no guarantee that ``tp_init ``
394
394
is called at all (for example, the :mod: `pickle ` module by default
395
- doesn't call :meth: `__init__ ` on unpickled instances). It can also be
396
- called multiple times. Anyone can call the :meth: `__init__ ` method on
395
+ doesn't call :meth: `~object. __init__ ` on unpickled instances). It can also be
396
+ called multiple times. Anyone can call the :meth: `! __init__ ` method on
397
397
our objects. For this reason, we have to be extra careful when assigning
398
398
the new attribute values. We might be tempted, for example to assign the
399
399
``first `` member like this::
@@ -706,8 +706,8 @@ participate in cycles::
706
706
}
707
707
708
708
For each subobject that can participate in cycles, we need to call the
709
- :c:func: `visit ` function, which is passed to the traversal method. The
710
- :c:func: `visit ` function takes as arguments the subobject and the extra argument
709
+ :c:func: `! visit ` function, which is passed to the traversal method. The
710
+ :c:func: `! visit ` function takes as arguments the subobject and the extra argument
711
711
*arg * passed to the traversal method. It returns an integer value that must be
712
712
returned if it is non-zero.
713
713
@@ -789,9 +789,9 @@ types. It is easiest to inherit from the built in types, since an extension can
789
789
easily use the :c:type: `PyTypeObject ` it needs. It can be difficult to share
790
790
these :c:type: `PyTypeObject ` structures between extension modules.
791
791
792
- In this example we will create a :class: `SubList ` type that inherits from the
792
+ In this example we will create a :class: `! SubList ` type that inherits from the
793
793
built-in :class: `list ` type. The new type will be completely compatible with
794
- regular lists, but will have an additional :meth: `increment ` method that
794
+ regular lists, but will have an additional :meth: `! increment ` method that
795
795
increases an internal counter:
796
796
797
797
.. code-block :: pycon
@@ -821,7 +821,7 @@ The primary difference for derived type objects is that the base type's
821
821
object structure must be the first value. The base type will already include
822
822
the :c:func: `PyObject_HEAD ` at the beginning of its structure.
823
823
824
- When a Python object is a :class: `SubList ` instance, its ``PyObject * `` pointer
824
+ When a Python object is a :class: `! SubList ` instance, its ``PyObject * `` pointer
825
825
can be safely cast to both ``PyListObject * `` and ``SubListObject * ``::
826
826
827
827
static int
@@ -833,7 +833,7 @@ can be safely cast to both ``PyListObject *`` and ``SubListObject *``::
833
833
return 0;
834
834
}
835
835
836
- We see above how to call through to the :attr: ` __init__ ` method of the base
836
+ We see above how to call through to the :meth: ` ~object. __init__ ` method of the base
837
837
type.
838
838
839
839
This pattern is important when writing a type with custom
0 commit comments