2 #include "structmember.h"
12 Noddy_traverse(Noddy
*self
, visitproc visit
, void *arg
)
17 vret
= visit(self
->first
, arg
);
22 vret
= visit(self
->last
, arg
);
31 Noddy_clear(Noddy
*self
)
47 Noddy_dealloc(Noddy
* self
)
50 self
->ob_type
->tp_free((PyObject
*)self
);
54 Noddy_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
58 self
= (Noddy
*)type
->tp_alloc(type
, 0);
60 self
->first
= PyString_FromString("");
61 if (self
->first
== NULL
)
67 self
->last
= PyString_FromString("");
68 if (self
->last
== NULL
)
77 return (PyObject
*)self
;
81 Noddy_init(Noddy
*self
, PyObject
*args
, PyObject
*kwds
)
83 PyObject
*first
=NULL
, *last
=NULL
, *tmp
;
85 static char *kwlist
[] = {"first", "last", "number", NULL
};
87 if (! PyArg_ParseTupleAndKeywords(args
, kwds
, "|OOi", kwlist
,
110 static PyMemberDef Noddy_members
[] = {
111 {"first", T_OBJECT_EX
, offsetof(Noddy
, first
), 0,
113 {"last", T_OBJECT_EX
, offsetof(Noddy
, last
), 0,
115 {"number", T_INT
, offsetof(Noddy
, number
), 0,
117 {NULL
} /* Sentinel */
121 Noddy_name(Noddy
* self
)
123 static PyObject
*format
= NULL
;
124 PyObject
*args
, *result
;
126 if (format
== NULL
) {
127 format
= PyString_FromString("%s %s");
132 if (self
->first
== NULL
) {
133 PyErr_SetString(PyExc_AttributeError
, "first");
137 if (self
->last
== NULL
) {
138 PyErr_SetString(PyExc_AttributeError
, "last");
142 args
= Py_BuildValue("OO", self
->first
, self
->last
);
146 result
= PyString_Format(format
, args
);
152 static PyMethodDef Noddy_methods
[] = {
153 {"name", (PyCFunction
)Noddy_name
, METH_NOARGS
,
154 "Return the name, combining the first and last name"
156 {NULL
} /* Sentinel */
159 static PyTypeObject NoddyType
= {
160 PyObject_HEAD_INIT(NULL
)
162 "noddy.Noddy", /*tp_name*/
163 sizeof(Noddy
), /*tp_basicsize*/
165 (destructor
)Noddy_dealloc
, /*tp_dealloc*/
172 0, /*tp_as_sequence*/
180 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_GC
, /*tp_flags*/
181 "Noddy objects", /* tp_doc */
182 (traverseproc
)Noddy_traverse
, /* tp_traverse */
183 (inquiry
)Noddy_clear
, /* tp_clear */
184 0, /* tp_richcompare */
185 0, /* tp_weaklistoffset */
188 Noddy_methods
, /* tp_methods */
189 Noddy_members
, /* tp_members */
193 0, /* tp_descr_get */
194 0, /* tp_descr_set */
195 0, /* tp_dictoffset */
196 (initproc
)Noddy_init
, /* tp_init */
198 Noddy_new
, /* tp_new */
201 static PyMethodDef module_methods
[] = {
202 {NULL
} /* Sentinel */
205 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
206 #define PyMODINIT_FUNC void
213 if (PyType_Ready(&NoddyType
) < 0)
216 m
= Py_InitModule3("noddy4", module_methods
,
217 "Example module that creates an extension type.");
222 Py_INCREF(&NoddyType
);
223 PyModule_AddObject(m
, "Noddy", (PyObject
*)&NoddyType
);