blob: 03fe2888f9d0121a4b7ecede69306afdfa908142 [file] [log] [blame]
Jim Fultonaed0a4a2003-05-07 19:48:13 +00001#include <Python.h>
2#include "structmember.h"
3
4typedef struct {
5 PyObject_HEAD
6 PyObject *first;
7 PyObject *last;
8 int number;
9} Noddy;
10
11static void
12Noddy_dealloc(Noddy* self)
13{
14 Py_XDECREF(self->first);
15 Py_XDECREF(self->last);
Jim Fulton1f325562003-05-16 13:53:43 +000016 self->ob_type->tp_free((PyObject*)self);
Jim Fultonaed0a4a2003-05-07 19:48:13 +000017}
18
19static PyObject *
20Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
21{
22 Noddy *self;
23
24 self = (Noddy *)type->tp_alloc(type, 0);
25 if (self != NULL) {
26 self->first = PyString_FromString("");
27 if (self->first == NULL)
28 {
29 Py_DECREF(self);
30 return NULL;
31 }
32
33 self->last = PyString_FromString("");
34 if (self->last == NULL)
35 {
36 Py_DECREF(self);
37 return NULL;
38 }
39
40 self->number = 0;
41 }
42
43 return (PyObject *)self;
44}
45
Jim Fulton7050e922003-06-28 11:54:03 +000046static int
Jim Fultonaed0a4a2003-05-07 19:48:13 +000047Noddy_init(Noddy *self, PyObject *args, PyObject *kwds)
48{
49 PyObject *first=NULL, *last=NULL;
50
51 static char *kwlist[] = {"first", "last", "number", NULL};
52
53 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
54 &first, &last,
55 &self->number))
Jim Fulton7050e922003-06-28 11:54:03 +000056 return -1;
Jim Fultonaed0a4a2003-05-07 19:48:13 +000057
58 if (first) {
59 Py_XDECREF(self->first);
60 Py_INCREF(first);
61 self->first = first;
62 }
63
64 if (last) {
65 Py_XDECREF(self->last);
66 Py_INCREF(last);
67 self->last = last;
68 }
69
Jim Fulton7050e922003-06-28 11:54:03 +000070 return 0;
Jim Fultonaed0a4a2003-05-07 19:48:13 +000071}
72
73
74static PyMemberDef Noddy_members[] = {
75 {"first", T_OBJECT_EX, offsetof(Noddy, first), 0,
76 "first name"},
77 {"last", T_OBJECT_EX, offsetof(Noddy, last), 0,
78 "last name"},
79 {"number", T_INT, offsetof(Noddy, number), 0,
80 "noddy number"},
81 {NULL} /* Sentinel */
82};
83
84static PyObject *
85Noddy_name(Noddy* self)
86{
87 static PyObject *format = NULL;
88 PyObject *args, *result;
89
90 if (format == NULL) {
91 format = PyString_FromString("%s %s");
92 if (format == NULL)
93 return NULL;
94 }
95
96 if (self->first == NULL) {
97 PyErr_SetString(PyExc_AttributeError, "first");
98 return NULL;
99 }
100
101 if (self->last == NULL) {
102 PyErr_SetString(PyExc_AttributeError, "last");
103 return NULL;
104 }
105
106 args = Py_BuildValue("OO", self->first, self->last);
107 if (args == NULL)
108 return NULL;
109
110 result = PyString_Format(format, args);
111 Py_DECREF(args);
112
113 return result;
114}
115
116static PyMethodDef Noddy_methods[] = {
117 {"name", (PyCFunction)Noddy_name, METH_NOARGS,
118 "Return the name, combining the first and last name"
119 },
120 {NULL} /* Sentinel */
121};
122
123static PyTypeObject NoddyType = {
124 PyObject_HEAD_INIT(NULL)
125 0, /*ob_size*/
126 "noddy.Noddy", /*tp_name*/
127 sizeof(Noddy), /*tp_basicsize*/
128 0, /*tp_itemsize*/
129 (destructor)Noddy_dealloc, /*tp_dealloc*/
130 0, /*tp_print*/
131 0, /*tp_getattr*/
132 0, /*tp_setattr*/
133 0, /*tp_compare*/
134 0, /*tp_repr*/
135 0, /*tp_as_number*/
136 0, /*tp_as_sequence*/
137 0, /*tp_as_mapping*/
138 0, /*tp_hash */
139 0, /*tp_call*/
140 0, /*tp_str*/
141 0, /*tp_getattro*/
142 0, /*tp_setattro*/
143 0, /*tp_as_buffer*/
144 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
145 "Noddy objects", /* tp_doc */
146 0, /* tp_traverse */
147 0, /* tp_clear */
148 0, /* tp_richcompare */
149 0, /* tp_weaklistoffset */
150 0, /* tp_iter */
151 0, /* tp_iternext */
152 Noddy_methods, /* tp_methods */
153 Noddy_members, /* tp_members */
154 0, /* tp_getset */
155 0, /* tp_base */
156 0, /* tp_dict */
157 0, /* tp_descr_get */
158 0, /* tp_descr_set */
159 0, /* tp_dictoffset */
160 (initproc)Noddy_init, /* tp_init */
161 0, /* tp_alloc */
162 Noddy_new, /* tp_new */
163};
164
165static PyMethodDef module_methods[] = {
166 {NULL} /* Sentinel */
167};
168
Jim Fulton1f325562003-05-16 13:53:43 +0000169#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
170#define PyMODINIT_FUNC void
171#endif
Jim Fultonaed0a4a2003-05-07 19:48:13 +0000172PyMODINIT_FUNC
173initnoddy2(void)
174{
175 PyObject* m;
176
177 if (PyType_Ready(&NoddyType) < 0)
178 return;
179
180 m = Py_InitModule3("noddy2", module_methods,
181 "Example module that creates an extension type.");
182
183 if (m == NULL)
184 return;
185
Raymond Hettinger8fb665a2003-05-25 17:59:38 +0000186 Py_INCREF(&NoddyType);
Jim Fultonaed0a4a2003-05-07 19:48:13 +0000187 PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);
188}