2 #include "structmember.h"
6 PyObject
*first
; /* first name */
7 PyObject
*last
; /* last name */
12 Noddy_dealloc(Noddy
* self
)
14 Py_XDECREF(self
->first
);
15 Py_XDECREF(self
->last
);
16 self
->ob_type
->tp_free((PyObject
*)self
);
20 Noddy_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
24 self
= (Noddy
*)type
->tp_alloc(type
, 0);
26 self
->first
= PyString_FromString("");
27 if (self
->first
== NULL
)
33 self
->last
= PyString_FromString("");
34 if (self
->last
== NULL
)
43 return (PyObject
*)self
;
47 Noddy_init(Noddy
*self
, PyObject
*args
, PyObject
*kwds
)
49 PyObject
*first
=NULL
, *last
=NULL
, *tmp
;
51 static char *kwlist
[] = {"first", "last", "number", NULL
};
53 if (! PyArg_ParseTupleAndKeywords(args
, kwds
, "|OOi", kwlist
,
76 static PyMemberDef Noddy_members
[] = {
77 {"first", T_OBJECT_EX
, offsetof(Noddy
, first
), 0,
79 {"last", T_OBJECT_EX
, offsetof(Noddy
, last
), 0,
81 {"number", T_INT
, offsetof(Noddy
, number
), 0,
87 Noddy_name(Noddy
* self
)
89 static PyObject
*format
= NULL
;
90 PyObject
*args
, *result
;
93 format
= PyString_FromString("%s %s");
98 if (self
->first
== NULL
) {
99 PyErr_SetString(PyExc_AttributeError
, "first");
103 if (self
->last
== NULL
) {
104 PyErr_SetString(PyExc_AttributeError
, "last");
108 args
= Py_BuildValue("OO", self
->first
, self
->last
);
112 result
= PyString_Format(format
, args
);
118 static PyMethodDef Noddy_methods
[] = {
119 {"name", (PyCFunction
)Noddy_name
, METH_NOARGS
,
120 "Return the name, combining the first and last name"
122 {NULL
} /* Sentinel */
125 static PyTypeObject NoddyType
= {
126 PyObject_HEAD_INIT(NULL
)
128 "noddy.Noddy", /*tp_name*/
129 sizeof(Noddy
), /*tp_basicsize*/
131 (destructor
)Noddy_dealloc
, /*tp_dealloc*/
138 0, /*tp_as_sequence*/
146 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
147 "Noddy objects", /* tp_doc */
150 0, /* tp_richcompare */
151 0, /* tp_weaklistoffset */
154 Noddy_methods
, /* tp_methods */
155 Noddy_members
, /* tp_members */
159 0, /* tp_descr_get */
160 0, /* tp_descr_set */
161 0, /* tp_dictoffset */
162 (initproc
)Noddy_init
, /* tp_init */
164 Noddy_new
, /* tp_new */
167 static PyMethodDef module_methods
[] = {
168 {NULL
} /* Sentinel */
171 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
172 #define PyMODINIT_FUNC void
179 if (PyType_Ready(&NoddyType
) < 0)
182 m
= Py_InitModule3("noddy2", module_methods
,
183 "Example module that creates an extension type.");
188 Py_INCREF(&NoddyType
);
189 PyModule_AddObject(m
, "Noddy", (PyObject
*)&NoddyType
);