blob: 1b3704ceff8dfde9bc1b905727c7d3524e88899d [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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 int
12Noddy_traverse(Noddy *self, visitproc visit, void *arg)
13{
14 int vret;
15
16 if (self->first) {
17 vret = visit(self->first, arg);
18 if (vret != 0)
19 return vret;
20 }
21 if (self->last) {
22 vret = visit(self->last, arg);
23 if (vret != 0)
24 return vret;
25 }
26
27 return 0;
28}
29
30static int
31Noddy_clear(Noddy *self)
32{
33 PyObject *tmp;
34
35 tmp = self->first;
36 self->first = NULL;
37 Py_XDECREF(tmp);
38
39 tmp = self->last;
40 self->last = NULL;
41 Py_XDECREF(tmp);
42
43 return 0;
44}
45
46static void
47Noddy_dealloc(Noddy* self)
48{
49 Noddy_clear(self);
Georg Brandl913b2a32008-12-05 15:12:15 +000050 Py_TYPE(self)->tp_free((PyObject*)self);
Georg Brandl116aa622007-08-15 14:28:22 +000051}
52
53static PyObject *
54Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
55{
56 Noddy *self;
57
58 self = (Noddy *)type->tp_alloc(type, 0);
59 if (self != NULL) {
Neal Norwitzbed67842007-10-27 04:00:45 +000060 self->first = PyUnicode_FromString("");
Georg Brandl116aa622007-08-15 14:28:22 +000061 if (self->first == NULL)
62 {
63 Py_DECREF(self);
64 return NULL;
65 }
66
Neal Norwitzbed67842007-10-27 04:00:45 +000067 self->last = PyUnicode_FromString("");
Georg Brandl116aa622007-08-15 14:28:22 +000068 if (self->last == NULL)
69 {
70 Py_DECREF(self);
71 return NULL;
72 }
73
74 self->number = 0;
75 }
76
77 return (PyObject *)self;
78}
79
80static int
81Noddy_init(Noddy *self, PyObject *args, PyObject *kwds)
82{
83 PyObject *first=NULL, *last=NULL, *tmp;
84
85 static char *kwlist[] = {"first", "last", "number", NULL};
86
87 if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
88 &first, &last,
89 &self->number))
90 return -1;
91
92 if (first) {
93 tmp = self->first;
94 Py_INCREF(first);
95 self->first = first;
96 Py_XDECREF(tmp);
97 }
98
99 if (last) {
100 tmp = self->last;
101 Py_INCREF(last);
102 self->last = last;
103 Py_XDECREF(tmp);
104 }
105
106 return 0;
107}
108
109
110static PyMemberDef Noddy_members[] = {
111 {"first", T_OBJECT_EX, offsetof(Noddy, first), 0,
112 "first name"},
113 {"last", T_OBJECT_EX, offsetof(Noddy, last), 0,
114 "last name"},
115 {"number", T_INT, offsetof(Noddy, number), 0,
116 "noddy number"},
117 {NULL} /* Sentinel */
118};
119
120static PyObject *
121Noddy_name(Noddy* self)
122{
123 static PyObject *format = NULL;
124 PyObject *args, *result;
125
126 if (format == NULL) {
Neal Norwitzbed67842007-10-27 04:00:45 +0000127 format = PyUnicode_FromString("%s %s");
Georg Brandl116aa622007-08-15 14:28:22 +0000128 if (format == NULL)
129 return NULL;
130 }
131
132 if (self->first == NULL) {
133 PyErr_SetString(PyExc_AttributeError, "first");
134 return NULL;
135 }
136
137 if (self->last == NULL) {
138 PyErr_SetString(PyExc_AttributeError, "last");
139 return NULL;
140 }
141
142 args = Py_BuildValue("OO", self->first, self->last);
143 if (args == NULL)
144 return NULL;
145
Neal Norwitzbed67842007-10-27 04:00:45 +0000146 result = PyUnicode_Format(format, args);
Georg Brandl116aa622007-08-15 14:28:22 +0000147 Py_DECREF(args);
148
149 return result;
150}
151
152static PyMethodDef Noddy_methods[] = {
153 {"name", (PyCFunction)Noddy_name, METH_NOARGS,
154 "Return the name, combining the first and last name"
155 },
156 {NULL} /* Sentinel */
157};
158
159static PyTypeObject NoddyType = {
160 PyObject_HEAD_INIT(NULL)
Georg Brandl913b2a32008-12-05 15:12:15 +0000161 "noddy.Noddy", /* tp_name */
162 sizeof(Noddy), /* tp_basicsize */
163 0, /* tp_itemsize */
164 (destructor)Noddy_dealloc, /* tp_dealloc */
165 0, /* tp_print */
166 0, /* tp_getattr */
167 0, /* tp_setattr */
Mark Dickinson9f989262009-02-02 21:29:40 +0000168 0, /* tp_reserved */
Georg Brandl913b2a32008-12-05 15:12:15 +0000169 0, /* tp_repr */
170 0, /* tp_as_number */
171 0, /* tp_as_sequence */
172 0, /* tp_as_mapping */
173 0, /* tp_hash */
174 0, /* tp_call */
175 0, /* tp_str */
176 0, /* tp_getattro */
177 0, /* tp_setattro */
178 0, /* tp_as_buffer */
179 Py_TPFLAGS_DEFAULT |
180 Py_TPFLAGS_BASETYPE |
181 Py_TPFLAGS_HAVE_GC, /* tp_flags */
Georg Brandl116aa622007-08-15 14:28:22 +0000182 "Noddy objects", /* tp_doc */
183 (traverseproc)Noddy_traverse, /* tp_traverse */
184 (inquiry)Noddy_clear, /* tp_clear */
185 0, /* tp_richcompare */
186 0, /* tp_weaklistoffset */
187 0, /* tp_iter */
188 0, /* tp_iternext */
189 Noddy_methods, /* tp_methods */
190 Noddy_members, /* tp_members */
191 0, /* tp_getset */
192 0, /* tp_base */
193 0, /* tp_dict */
194 0, /* tp_descr_get */
195 0, /* tp_descr_set */
196 0, /* tp_dictoffset */
197 (initproc)Noddy_init, /* tp_init */
198 0, /* tp_alloc */
199 Noddy_new, /* tp_new */
200};
201
Georg Brandl913b2a32008-12-05 15:12:15 +0000202static PyModuleDef noddy4module = {
203 PyModuleDef_HEAD_INIT,
204 "noddy4",
205 "Example module that creates an extension type.",
206 -1,
207 NULL, NULL, NULL, NULL, NULL
Georg Brandl116aa622007-08-15 14:28:22 +0000208};
209
Georg Brandl116aa622007-08-15 14:28:22 +0000210PyMODINIT_FUNC
Georg Brandl913b2a32008-12-05 15:12:15 +0000211PyInit_noddy4(void)
Georg Brandl116aa622007-08-15 14:28:22 +0000212{
213 PyObject* m;
214
215 if (PyType_Ready(&NoddyType) < 0)
Georg Brandl913b2a32008-12-05 15:12:15 +0000216 return NULL;
Georg Brandl116aa622007-08-15 14:28:22 +0000217
Georg Brandl913b2a32008-12-05 15:12:15 +0000218 m = PyModule_Create(&noddy4module);
Georg Brandl116aa622007-08-15 14:28:22 +0000219 if (m == NULL)
Georg Brandl913b2a32008-12-05 15:12:15 +0000220 return NULL;
Georg Brandl116aa622007-08-15 14:28:22 +0000221
222 Py_INCREF(&NoddyType);
223 PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);
Benjamin Peterson71e30a02008-12-24 16:27:25 +0000224 return m;
Georg Brandl116aa622007-08-15 14:28:22 +0000225}