@@ -30,66 +30,7 @@ \section{The Basics
3030This sort of thing can only be explained by example, so here's a
3131minimal, but complete, module that defines a new type:
3232
33- \begin {verbatim }
34- #include <Python.h>
35-
36- staticforward PyTypeObject noddy_NoddyType;
37-
38- typedef struct {
39- PyObject_HEAD
40- } noddy_NoddyObject;
41-
42- static PyObject*
43- noddy_new_noddy(PyObject* self, PyObject* args)
44- {
45- noddy_NoddyObject* noddy;
46-
47- if (!PyArg_ParseTuple(args,":new_noddy"))
48- return NULL;
49-
50- noddy = PyObject_New(noddy_NoddyObject, &noddy_NoddyType);
51-
52- return (PyObject*)noddy;
53- }
54-
55- static void
56- noddy_noddy_dealloc(PyObject* self)
57- {
58- PyObject_Del(self);
59- }
60-
61- static PyTypeObject noddy_NoddyType = {
62- PyObject_HEAD_INIT(NULL)
63- 0,
64- "Noddy",
65- sizeof(noddy_NoddyObject),
66- 0,
67- noddy_noddy_dealloc, /*tp_dealloc*/
68- 0, /*tp_print*/
69- 0, /*tp_getattr*/
70- 0, /*tp_setattr*/
71- 0, /*tp_compare*/
72- 0, /*tp_repr*/
73- 0, /*tp_as_number*/
74- 0, /*tp_as_sequence*/
75- 0, /*tp_as_mapping*/
76- 0, /*tp_hash */
77- };
78-
79- static PyMethodDef noddy_methods[] = {
80- {"new_noddy", noddy_new_noddy, METH_VARARGS,
81- "Create a new Noddy object."},
82- {NULL, NULL, 0, NULL}
83- };
84-
85- DL_EXPORT(void)
86- initnoddy(void)
87- {
88- noddy_NoddyType.ob_type = &PyType_Type;
89-
90- Py_InitModule("noddy", noddy_methods);
91- }
92- \end {verbatim }
33+ \verbatiminput {noddy.c}
9334
9435Now that's quite a bit to take in at once, but hopefully bits will
9536seem familiar from the last chapter.
@@ -150,9 +91,10 @@ \section{The Basics
15091
15192This is in fact just a regular module function, as described in the
15293last chapter. The reason it gets special mention is that this is
153- where we create our Noddy object. Defining PyTypeObject structures is
154- all very well, but if there's no way to actually \emph {create } one
155- of the wretched things it is not going to do anyone much good.
94+ where we create our Noddy object. Defining \ctype {PyTypeObject}
95+ structures is all very well, but if there's no way to actually
96+ \emph {create } one of the wretched things it is not going to do anyone
97+ much good.
15698
15799Almost always, you create objects with a call of the form:
158100
@@ -161,11 +103,23 @@ \section{The Basics
161103\end {verbatim }
162104
163105This allocates the memory and then initializes the object (sets
164- the reference count to one, makes the \cdata {ob_type} pointer point at
106+ the reference count to one, makes the \member {ob_type} pointer point at
165107the right place and maybe some other stuff, depending on build options).
166108You \emph {can } do these steps separately if you have some reason to
167109--- but at this level we don't bother.
168110
111+ Note that \cfunction {PyObject_New()} is a polymorphic macro rather
112+ than a real function. The first parameter is the name of the C
113+ structure that represents an object of our new type, and the return
114+ value is a pointer to that type. This would be
115+ \ctype {noddy_NoddyObject} in our example:
116+
117+ \begin {verbatim }
118+ noddy_NoddyObject *my_noddy;
119+
120+ my_noddy = PyObject_New(noddy_NoddyObject, &noddy_NoddyType);
121+ \end {verbatim }
122+
169123We cast the return value to a \ctype {PyObject*} because that's what
170124the Python runtime expects. This is safe because of guarantees about
171125the layout of structures in the C standard, and is a fairly common C
@@ -236,14 +190,17 @@ \section{The Basics
236190
237191as the type of a type object is `` type'' , but this isn't strictly
238192conforming C and some compilers complain. So instead we fill in the
239- \cdata {ob_type} field of \cdata {noddy_NoddyType} at the earliest
193+ \member {ob_type} field of \cdata {noddy_NoddyType} at the earliest
240194oppourtunity --- in \cfunction {initnoddy()}.
241195
242196\begin {verbatim }
243197 0,
244198\end {verbatim }
245199
246- XXX why does the type info struct start PyObject_*VAR*_HEAD??
200+ The \member {ob_size} field of the header is not used; it's presence in
201+ the type structure is a historical artifact that is maintained for
202+ binary compatibility with extension modules compiled for older
203+ versions of Python. Always set this field to zero.
247204
248205\begin {verbatim }
249206 "Noddy",
@@ -282,8 +239,8 @@ \section{The Basics
282239 noddy_noddy_dealloc, /*tp_dealloc*/
283240\end {verbatim }
284241
285- From here, all the type methods are nil so I won't go over them yet -
286- that's for the next section!
242+ From here, all the type methods are \NULL , so I won't go over them yet
243+ --- that's for the next section!
287244
288245Everything else in the file should be familiar, except for this line
289246in \cfunction {initnoddy}:
@@ -302,15 +259,15 @@ \section{The Basics
302259
303260\begin {verbatim }
304261from distutils.core import setup, Extension
305- setup(name = "noddy", version = "1.0",
306- ext_modules = [Extension("noddy", ["noddymodule.c"])])
262+ setup(name= "noddy", version= "1.0",
263+ ext_modules= [Extension("noddy", ["noddymodule.c"])])
307264\end {verbatim }
308265
309266in a file called \file {setup.py}; then typing
310267
311268\begin {verbatim }
312- $ python setup.py build%$
313- \end {verbatim }
269+ $ python setup.py build
270+ \end {verbatim } % $ <-- bow to font-lock ;-(
314271
315272at a shell should produce a file \file {noddy.so} in a subdirectory;
316273move to that directory and fire up Python --- you should be able to
@@ -328,81 +285,7 @@ \section{Type Methods
328285Here is the definition of \ctype {PyTypeObject}, with some fields only
329286used in debug builds omitted:
330287
331- \begin {verbatim }
332- typedef struct _typeobject {
333- PyObject_VAR_HEAD
334- char *tp_name; /* For printing */
335- int tp_basicsize, tp_itemsize; /* For allocation */
336-
337- /* Methods to implement standard operations */
338-
339- destructor tp_dealloc;
340- printfunc tp_print;
341- getattrfunc tp_getattr;
342- setattrfunc tp_setattr;
343- cmpfunc tp_compare;
344- reprfunc tp_repr;
345-
346- /* Method suites for standard classes */
347-
348- PyNumberMethods *tp_as_number;
349- PySequenceMethods *tp_as_sequence;
350- PyMappingMethods *tp_as_mapping;
351-
352- /* More standard operations (here for binary compatibility) */
353-
354- hashfunc tp_hash;
355- ternaryfunc tp_call;
356- reprfunc tp_str;
357- getattrofunc tp_getattro;
358- setattrofunc tp_setattro;
359-
360- /* Functions to access object as input/output buffer */
361- PyBufferProcs *tp_as_buffer;
362-
363- /* Flags to define presence of optional/expanded features */
364- long tp_flags;
365-
366- char *tp_doc; /* Documentation string */
367-
368- /* Assigned meaning in release 2.0 */
369- /* call function for all accessible objects */
370- traverseproc tp_traverse;
371-
372- /* delete references to contained objects */
373- inquiry tp_clear;
374-
375- /* Assigned meaning in release 2.1 */
376- /* rich comparisons */
377- richcmpfunc tp_richcompare;
378-
379- /* weak reference enabler */
380- long tp_weaklistoffset;
381-
382- /* Added in release 2.2 */
383- /* Iterators */
384- getiterfunc tp_iter;
385- iternextfunc tp_iternext;
386-
387- /* Attribute descriptor and subclassing stuff */
388- struct PyMethodDef *tp_methods;
389- struct memberlist *tp_members;
390- struct getsetlist *tp_getset;
391- struct _typeobject *tp_base;
392- PyObject *tp_dict;
393- descrgetfunc tp_descr_get;
394- descrsetfunc tp_descr_set;
395- long tp_dictoffset;
396- initproc tp_init;
397- allocfunc tp_alloc;
398- newfunc tp_new;
399- destructor tp_free; /* Low-level free-memory routine */
400- PyObject *tp_bases;
401- PyObject *tp_mro; /* method resolution order */
402- PyObject *tp_defined;
403-
404- } PyTypeObject;
405- \end {verbatim }
288+ \verbatiminput {typestruct.h}
406289
407290Now that's a \emph {lot } of methods. Don't worry too much though - if
408291you have a type you want to define, the chances are very good that you
@@ -432,7 +315,7 @@ \section{Type Methods
432315These fields tell the runtime how much memory to allocate when new
433316objects of this typed are created. Python has some builtin support
434317for variable length structures (think: strings, lists) which is where
435- the \cdata {tp_itemsize} field comes in. This will be dealt with
318+ the \member {tp_itemsize} field comes in. This will be dealt with
436319later.
437320
438321\begin {verbatim }
0 commit comments