1 | n/a | #include <Python.h> |
---|
2 | n/a | #include "structmember.h" |
---|
3 | n/a | |
---|
4 | n/a | typedef struct { |
---|
5 | n/a | PyObject_HEAD |
---|
6 | n/a | PyObject *first; |
---|
7 | n/a | PyObject *last; |
---|
8 | n/a | int number; |
---|
9 | n/a | } Noddy; |
---|
10 | n/a | |
---|
11 | n/a | static void |
---|
12 | n/a | Noddy_dealloc(Noddy* self) |
---|
13 | n/a | { |
---|
14 | n/a | Py_XDECREF(self->first); |
---|
15 | n/a | Py_XDECREF(self->last); |
---|
16 | n/a | Py_TYPE(self)->tp_free((PyObject*)self); |
---|
17 | n/a | } |
---|
18 | n/a | |
---|
19 | n/a | static PyObject * |
---|
20 | n/a | Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
21 | n/a | { |
---|
22 | n/a | Noddy *self; |
---|
23 | n/a | |
---|
24 | n/a | self = (Noddy *)type->tp_alloc(type, 0); |
---|
25 | n/a | if (self != NULL) { |
---|
26 | n/a | self->first = PyUnicode_FromString(""); |
---|
27 | n/a | if (self->first == NULL) { |
---|
28 | n/a | Py_DECREF(self); |
---|
29 | n/a | return NULL; |
---|
30 | n/a | } |
---|
31 | n/a | |
---|
32 | n/a | self->last = PyUnicode_FromString(""); |
---|
33 | n/a | if (self->last == NULL) { |
---|
34 | n/a | Py_DECREF(self); |
---|
35 | n/a | return NULL; |
---|
36 | n/a | } |
---|
37 | n/a | |
---|
38 | n/a | self->number = 0; |
---|
39 | n/a | } |
---|
40 | n/a | |
---|
41 | n/a | return (PyObject *)self; |
---|
42 | n/a | } |
---|
43 | n/a | |
---|
44 | n/a | static int |
---|
45 | n/a | Noddy_init(Noddy *self, PyObject *args, PyObject *kwds) |
---|
46 | n/a | { |
---|
47 | n/a | PyObject *first=NULL, *last=NULL, *tmp; |
---|
48 | n/a | |
---|
49 | n/a | static char *kwlist[] = {"first", "last", "number", NULL}; |
---|
50 | n/a | |
---|
51 | n/a | if (! PyArg_ParseTupleAndKeywords(args, kwds, "|SSi", kwlist, |
---|
52 | n/a | &first, &last, |
---|
53 | n/a | &self->number)) |
---|
54 | n/a | return -1; |
---|
55 | n/a | |
---|
56 | n/a | if (first) { |
---|
57 | n/a | tmp = self->first; |
---|
58 | n/a | Py_INCREF(first); |
---|
59 | n/a | self->first = first; |
---|
60 | n/a | Py_DECREF(tmp); |
---|
61 | n/a | } |
---|
62 | n/a | |
---|
63 | n/a | if (last) { |
---|
64 | n/a | tmp = self->last; |
---|
65 | n/a | Py_INCREF(last); |
---|
66 | n/a | self->last = last; |
---|
67 | n/a | Py_DECREF(tmp); |
---|
68 | n/a | } |
---|
69 | n/a | |
---|
70 | n/a | return 0; |
---|
71 | n/a | } |
---|
72 | n/a | |
---|
73 | n/a | static PyMemberDef Noddy_members[] = { |
---|
74 | n/a | {"number", T_INT, offsetof(Noddy, number), 0, |
---|
75 | n/a | "noddy number"}, |
---|
76 | n/a | {NULL} /* Sentinel */ |
---|
77 | n/a | }; |
---|
78 | n/a | |
---|
79 | n/a | static PyObject * |
---|
80 | n/a | Noddy_getfirst(Noddy *self, void *closure) |
---|
81 | n/a | { |
---|
82 | n/a | Py_INCREF(self->first); |
---|
83 | n/a | return self->first; |
---|
84 | n/a | } |
---|
85 | n/a | |
---|
86 | n/a | static int |
---|
87 | n/a | Noddy_setfirst(Noddy *self, PyObject *value, void *closure) |
---|
88 | n/a | { |
---|
89 | n/a | if (value == NULL) { |
---|
90 | n/a | PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute"); |
---|
91 | n/a | return -1; |
---|
92 | n/a | } |
---|
93 | n/a | |
---|
94 | n/a | if (! PyUnicode_Check(value)) { |
---|
95 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
96 | n/a | "The first attribute value must be a string"); |
---|
97 | n/a | return -1; |
---|
98 | n/a | } |
---|
99 | n/a | |
---|
100 | n/a | Py_DECREF(self->first); |
---|
101 | n/a | Py_INCREF(value); |
---|
102 | n/a | self->first = value; |
---|
103 | n/a | |
---|
104 | n/a | return 0; |
---|
105 | n/a | } |
---|
106 | n/a | |
---|
107 | n/a | static PyObject * |
---|
108 | n/a | Noddy_getlast(Noddy *self, void *closure) |
---|
109 | n/a | { |
---|
110 | n/a | Py_INCREF(self->last); |
---|
111 | n/a | return self->last; |
---|
112 | n/a | } |
---|
113 | n/a | |
---|
114 | n/a | static int |
---|
115 | n/a | Noddy_setlast(Noddy *self, PyObject *value, void *closure) |
---|
116 | n/a | { |
---|
117 | n/a | if (value == NULL) { |
---|
118 | n/a | PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute"); |
---|
119 | n/a | return -1; |
---|
120 | n/a | } |
---|
121 | n/a | |
---|
122 | n/a | if (! PyUnicode_Check(value)) { |
---|
123 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
124 | n/a | "The last attribute value must be a string"); |
---|
125 | n/a | return -1; |
---|
126 | n/a | } |
---|
127 | n/a | |
---|
128 | n/a | Py_DECREF(self->last); |
---|
129 | n/a | Py_INCREF(value); |
---|
130 | n/a | self->last = value; |
---|
131 | n/a | |
---|
132 | n/a | return 0; |
---|
133 | n/a | } |
---|
134 | n/a | |
---|
135 | n/a | static PyGetSetDef Noddy_getseters[] = { |
---|
136 | n/a | {"first", |
---|
137 | n/a | (getter)Noddy_getfirst, (setter)Noddy_setfirst, |
---|
138 | n/a | "first name", |
---|
139 | n/a | NULL}, |
---|
140 | n/a | {"last", |
---|
141 | n/a | (getter)Noddy_getlast, (setter)Noddy_setlast, |
---|
142 | n/a | "last name", |
---|
143 | n/a | NULL}, |
---|
144 | n/a | {NULL} /* Sentinel */ |
---|
145 | n/a | }; |
---|
146 | n/a | |
---|
147 | n/a | static PyObject * |
---|
148 | n/a | Noddy_name(Noddy* self) |
---|
149 | n/a | { |
---|
150 | n/a | return PyUnicode_FromFormat("%S %S", self->first, self->last); |
---|
151 | n/a | } |
---|
152 | n/a | |
---|
153 | n/a | static PyMethodDef Noddy_methods[] = { |
---|
154 | n/a | {"name", (PyCFunction)Noddy_name, METH_NOARGS, |
---|
155 | n/a | "Return the name, combining the first and last name" |
---|
156 | n/a | }, |
---|
157 | n/a | {NULL} /* Sentinel */ |
---|
158 | n/a | }; |
---|
159 | n/a | |
---|
160 | n/a | static PyTypeObject NoddyType = { |
---|
161 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
162 | n/a | "noddy.Noddy", /* tp_name */ |
---|
163 | n/a | sizeof(Noddy), /* tp_basicsize */ |
---|
164 | n/a | 0, /* tp_itemsize */ |
---|
165 | n/a | (destructor)Noddy_dealloc, /* tp_dealloc */ |
---|
166 | n/a | 0, /* tp_print */ |
---|
167 | n/a | 0, /* tp_getattr */ |
---|
168 | n/a | 0, /* tp_setattr */ |
---|
169 | n/a | 0, /* tp_reserved */ |
---|
170 | n/a | 0, /* tp_repr */ |
---|
171 | n/a | 0, /* tp_as_number */ |
---|
172 | n/a | 0, /* tp_as_sequence */ |
---|
173 | n/a | 0, /* tp_as_mapping */ |
---|
174 | n/a | 0, /* tp_hash */ |
---|
175 | n/a | 0, /* tp_call */ |
---|
176 | n/a | 0, /* tp_str */ |
---|
177 | n/a | 0, /* tp_getattro */ |
---|
178 | n/a | 0, /* tp_setattro */ |
---|
179 | n/a | 0, /* tp_as_buffer */ |
---|
180 | n/a | Py_TPFLAGS_DEFAULT | |
---|
181 | n/a | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
---|
182 | n/a | "Noddy objects", /* tp_doc */ |
---|
183 | n/a | 0, /* tp_traverse */ |
---|
184 | n/a | 0, /* tp_clear */ |
---|
185 | n/a | 0, /* tp_richcompare */ |
---|
186 | n/a | 0, /* tp_weaklistoffset */ |
---|
187 | n/a | 0, /* tp_iter */ |
---|
188 | n/a | 0, /* tp_iternext */ |
---|
189 | n/a | Noddy_methods, /* tp_methods */ |
---|
190 | n/a | Noddy_members, /* tp_members */ |
---|
191 | n/a | Noddy_getseters, /* tp_getset */ |
---|
192 | n/a | 0, /* tp_base */ |
---|
193 | n/a | 0, /* tp_dict */ |
---|
194 | n/a | 0, /* tp_descr_get */ |
---|
195 | n/a | 0, /* tp_descr_set */ |
---|
196 | n/a | 0, /* tp_dictoffset */ |
---|
197 | n/a | (initproc)Noddy_init, /* tp_init */ |
---|
198 | n/a | 0, /* tp_alloc */ |
---|
199 | n/a | Noddy_new, /* tp_new */ |
---|
200 | n/a | }; |
---|
201 | n/a | |
---|
202 | n/a | static PyModuleDef noddy3module = { |
---|
203 | n/a | PyModuleDef_HEAD_INIT, |
---|
204 | n/a | "noddy3", |
---|
205 | n/a | "Example module that creates an extension type.", |
---|
206 | n/a | -1, |
---|
207 | n/a | NULL, NULL, NULL, NULL, NULL |
---|
208 | n/a | }; |
---|
209 | n/a | |
---|
210 | n/a | PyMODINIT_FUNC |
---|
211 | n/a | PyInit_noddy3(void) |
---|
212 | n/a | { |
---|
213 | n/a | PyObject* m; |
---|
214 | n/a | |
---|
215 | n/a | if (PyType_Ready(&NoddyType) < 0) |
---|
216 | n/a | return NULL; |
---|
217 | n/a | |
---|
218 | n/a | m = PyModule_Create(&noddy3module); |
---|
219 | n/a | if (m == NULL) |
---|
220 | n/a | return NULL; |
---|
221 | n/a | |
---|
222 | n/a | Py_INCREF(&NoddyType); |
---|
223 | n/a | PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType); |
---|
224 | n/a | return m; |
---|
225 | n/a | } |
---|