@@ -79,27 +79,26 @@ Python integers::
79
79
Moving on, we come to the crunch --- the type object. ::
80
80
81
81
static PyTypeObject noddy_NoddyType = {
82
- PyObject_HEAD_INIT(NULL)
83
- 0, /*ob_size*/
84
- "noddy.Noddy", /*tp_name*/
85
- sizeof(noddy_NoddyObject), /*tp_basicsize*/
86
- 0, /*tp_itemsize*/
87
- 0, /*tp_dealloc*/
88
- 0, /*tp_print*/
89
- 0, /*tp_getattr*/
90
- 0, /*tp_setattr*/
91
- 0, /*tp_compare*/
92
- 0, /*tp_repr*/
93
- 0, /*tp_as_number*/
94
- 0, /*tp_as_sequence*/
95
- 0, /*tp_as_mapping*/
96
- 0, /*tp_hash */
97
- 0, /*tp_call*/
98
- 0, /*tp_str*/
99
- 0, /*tp_getattro*/
100
- 0, /*tp_setattro*/
101
- 0, /*tp_as_buffer*/
102
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
82
+ PyVarObject_HEAD_INIT(NULL, 0)
83
+ "noddy.Noddy", /* tp_name */
84
+ sizeof(noddy_NoddyObject), /* tp_basicsize */
85
+ 0, /* tp_itemsize */
86
+ 0, /* tp_dealloc */
87
+ 0, /* tp_print */
88
+ 0, /* tp_getattr */
89
+ 0, /* tp_setattr */
90
+ 0, /* tp_compare */
91
+ 0, /* tp_repr */
92
+ 0, /* tp_as_number */
93
+ 0, /* tp_as_sequence */
94
+ 0, /* tp_as_mapping */
95
+ 0, /* tp_hash */
96
+ 0, /* tp_call */
97
+ 0, /* tp_str */
98
+ 0, /* tp_getattro */
99
+ 0, /* tp_setattro */
100
+ 0, /* tp_as_buffer */
101
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
103
102
"Noddy objects", /* tp_doc */
104
103
};
105
104
@@ -111,23 +110,16 @@ it's common practice to not specify them explicitly unless you need them.
111
110
This is so important that we're going to pick the top of it apart still
112
111
further::
113
112
114
- PyObject_HEAD_INIT (NULL)
113
+ PyVarObject_HEAD_INIT (NULL, 0 )
115
114
116
115
This line is a bit of a wart; what we'd like to write is::
117
116
118
- PyObject_HEAD_INIT (&PyType_Type)
117
+ PyVarObject_HEAD_INIT (&PyType_Type, 0 )
119
118
120
119
as the type of a type object is "type", but this isn't strictly conforming C and
121
120
some compilers complain. Fortunately, this member will be filled in for us by
122
121
:c:func: `PyType_Ready `. ::
123
122
124
- 0, /* ob_size */
125
-
126
- The :attr: `ob_size ` field of the header is not used; its presence in the type
127
- structure is a historical artifact that is maintained for binary compatibility
128
- with extension modules compiled for older versions of Python. Always set this
129
- field to zero. ::
130
-
131
123
"noddy.Noddy", /* tp_name */
132
124
133
125
The name of our type. This will appear in the default textual representation of
@@ -171,7 +163,7 @@ for now.
171
163
Skipping a number of type methods that we don't provide, we set the class flags
172
164
to :const: `Py_TPFLAGS_DEFAULT `. ::
173
165
174
- Py_TPFLAGS_DEFAULT, /*tp_flags*/
166
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
175
167
176
168
All types should include this constant in their flags. It enables all of the
177
169
members defined by the current version of Python.
@@ -244,7 +236,7 @@ doesn't do anything. It can't even be subclassed.
244
236
Adding data and methods to the Basic example
245
237
--------------------------------------------
246
238
247
- Let's expend the basic example to add some data and methods. Let's also make
239
+ Let's extend the basic example to add some data and methods. Let's also make
248
240
the type usable as a base class. We'll create a new module, :mod: `noddy2 ` that
249
241
adds these capabilities:
250
242
@@ -284,7 +276,7 @@ allocation and deallocation. At a minimum, we need a deallocation method::
284
276
{
285
277
Py_XDECREF(self->first);
286
278
Py_XDECREF(self->last);
287
- self->ob_type ->tp_free((PyObject*)self);
279
+ Py_TYPE( self) ->tp_free((PyObject*)self);
288
280
}
289
281
290
282
which is assigned to the :c:member: `~PyTypeObject.tp_dealloc ` member::
@@ -497,7 +489,7 @@ concatenation of the first and last names. ::
497
489
The method is implemented as a C function that takes a :class: `Noddy ` (or
498
490
:class: `Noddy ` subclass) instance as the first argument. Methods always take an
499
491
instance as the first argument. Methods often take positional and keyword
500
- arguments as well, but in this cased we don't take any and don't need to accept
492
+ arguments as well, but in this case we don't take any and don't need to accept
501
493
a positional argument tuple or keyword argument dictionary. This method is
502
494
equivalent to the Python method::
503
495
@@ -806,7 +798,7 @@ decrementing of reference counts. With :c:func:`Py_CLEAR`, the
806
798
807
799
Finally, we add the :const: `Py_TPFLAGS_HAVE_GC ` flag to the class flags::
808
800
809
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
801
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
810
802
811
803
That's pretty much it. If we had written custom :c:member: `~PyTypeObject.tp_alloc ` or
812
804
:c:member: `~PyTypeObject.tp_free ` slots, we'd need to modify them for cyclic-garbage collection.
@@ -965,14 +957,15 @@ Finalization and De-allocation
965
957
966
958
This function is called when the reference count of the instance of your type is
967
959
reduced to zero and the Python interpreter wants to reclaim it. If your type
968
- has memory to free or other clean-up to perform, put it here. The object itself
969
- needs to be freed here as well. Here is an example of this function::
960
+ has memory to free or other clean-up to perform, you can put it here. The
961
+ object itself needs to be freed here as well. Here is an example of this
962
+ function::
970
963
971
964
static void
972
965
newdatatype_dealloc(newdatatypeobject * obj)
973
966
{
974
967
free(obj->obj_UnderlyingDatatypePtr);
975
- obj->ob_type ->tp_free(obj);
968
+ Py_TYPE( obj) ->tp_free(obj);
976
969
}
977
970
978
971
.. index ::
@@ -1015,7 +1008,7 @@ done. This can be done using the :c:func:`PyErr_Fetch` and
1015
1008
1016
1009
Py_DECREF(self->my_callback);
1017
1010
}
1018
- obj->ob_type ->tp_free((PyObject*)self);
1011
+ Py_TYPE( obj) ->tp_free((PyObject*)self);
1019
1012
}
1020
1013
1021
1014
0 commit comments