1 | n/a | |
---|
2 | n/a | /* Use this file as a template to start implementing a module that |
---|
3 | n/a | also declares object types. All occurrences of 'Xxo' should be changed |
---|
4 | n/a | to something reasonable for your objects. After that, all other |
---|
5 | n/a | occurrences of 'xx' should be changed to something reasonable for your |
---|
6 | n/a | module. If your module is named foo your sourcefile should be named |
---|
7 | n/a | foomodule.c. |
---|
8 | n/a | |
---|
9 | n/a | You will probably want to delete all references to 'x_attr' and add |
---|
10 | n/a | your own types of attributes instead. Maybe you want to name your |
---|
11 | n/a | local variables other than 'self'. If your object type is needed in |
---|
12 | n/a | other files, you'll have to create a file "foobarobject.h"; see |
---|
13 | n/a | floatobject.h for an example. */ |
---|
14 | n/a | |
---|
15 | n/a | /* Xxo objects */ |
---|
16 | n/a | |
---|
17 | n/a | #include "Python.h" |
---|
18 | n/a | |
---|
19 | n/a | static PyObject *ErrorObject; |
---|
20 | n/a | |
---|
21 | n/a | typedef struct { |
---|
22 | n/a | PyObject_HEAD |
---|
23 | n/a | PyObject *x_attr; /* Attributes dictionary */ |
---|
24 | n/a | } XxoObject; |
---|
25 | n/a | |
---|
26 | n/a | static PyTypeObject Xxo_Type; |
---|
27 | n/a | |
---|
28 | n/a | #define XxoObject_Check(v) (Py_TYPE(v) == &Xxo_Type) |
---|
29 | n/a | |
---|
30 | n/a | static XxoObject * |
---|
31 | n/a | newXxoObject(PyObject *arg) |
---|
32 | n/a | { |
---|
33 | n/a | XxoObject *self; |
---|
34 | n/a | self = PyObject_New(XxoObject, &Xxo_Type); |
---|
35 | n/a | if (self == NULL) |
---|
36 | n/a | return NULL; |
---|
37 | n/a | self->x_attr = NULL; |
---|
38 | n/a | return self; |
---|
39 | n/a | } |
---|
40 | n/a | |
---|
41 | n/a | /* Xxo methods */ |
---|
42 | n/a | |
---|
43 | n/a | static void |
---|
44 | n/a | Xxo_dealloc(XxoObject *self) |
---|
45 | n/a | { |
---|
46 | n/a | Py_XDECREF(self->x_attr); |
---|
47 | n/a | PyObject_Del(self); |
---|
48 | n/a | } |
---|
49 | n/a | |
---|
50 | n/a | static PyObject * |
---|
51 | n/a | Xxo_demo(XxoObject *self, PyObject *args) |
---|
52 | n/a | { |
---|
53 | n/a | if (!PyArg_ParseTuple(args, ":demo")) |
---|
54 | n/a | return NULL; |
---|
55 | n/a | Py_INCREF(Py_None); |
---|
56 | n/a | return Py_None; |
---|
57 | n/a | } |
---|
58 | n/a | |
---|
59 | n/a | static PyMethodDef Xxo_methods[] = { |
---|
60 | n/a | {"demo", (PyCFunction)Xxo_demo, METH_VARARGS, |
---|
61 | n/a | PyDoc_STR("demo() -> None")}, |
---|
62 | n/a | {NULL, NULL} /* sentinel */ |
---|
63 | n/a | }; |
---|
64 | n/a | |
---|
65 | n/a | static PyObject * |
---|
66 | n/a | Xxo_getattro(XxoObject *self, PyObject *name) |
---|
67 | n/a | { |
---|
68 | n/a | if (self->x_attr != NULL) { |
---|
69 | n/a | PyObject *v = PyDict_GetItem(self->x_attr, name); |
---|
70 | n/a | if (v != NULL) { |
---|
71 | n/a | Py_INCREF(v); |
---|
72 | n/a | return v; |
---|
73 | n/a | } |
---|
74 | n/a | } |
---|
75 | n/a | return PyObject_GenericGetAttr((PyObject *)self, name); |
---|
76 | n/a | } |
---|
77 | n/a | |
---|
78 | n/a | static int |
---|
79 | n/a | Xxo_setattr(XxoObject *self, const char *name, PyObject *v) |
---|
80 | n/a | { |
---|
81 | n/a | if (self->x_attr == NULL) { |
---|
82 | n/a | self->x_attr = PyDict_New(); |
---|
83 | n/a | if (self->x_attr == NULL) |
---|
84 | n/a | return -1; |
---|
85 | n/a | } |
---|
86 | n/a | if (v == NULL) { |
---|
87 | n/a | int rv = PyDict_DelItemString(self->x_attr, name); |
---|
88 | n/a | if (rv < 0) |
---|
89 | n/a | PyErr_SetString(PyExc_AttributeError, |
---|
90 | n/a | "delete non-existing Xxo attribute"); |
---|
91 | n/a | return rv; |
---|
92 | n/a | } |
---|
93 | n/a | else |
---|
94 | n/a | return PyDict_SetItemString(self->x_attr, name, v); |
---|
95 | n/a | } |
---|
96 | n/a | |
---|
97 | n/a | static PyTypeObject Xxo_Type = { |
---|
98 | n/a | /* The ob_type field must be initialized in the module init function |
---|
99 | n/a | * to be portable to Windows without using C++. */ |
---|
100 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
101 | n/a | "xxmodule.Xxo", /*tp_name*/ |
---|
102 | n/a | sizeof(XxoObject), /*tp_basicsize*/ |
---|
103 | n/a | 0, /*tp_itemsize*/ |
---|
104 | n/a | /* methods */ |
---|
105 | n/a | (destructor)Xxo_dealloc, /*tp_dealloc*/ |
---|
106 | n/a | 0, /*tp_print*/ |
---|
107 | n/a | (getattrfunc)0, /*tp_getattr*/ |
---|
108 | n/a | (setattrfunc)Xxo_setattr, /*tp_setattr*/ |
---|
109 | n/a | 0, /*tp_reserved*/ |
---|
110 | n/a | 0, /*tp_repr*/ |
---|
111 | n/a | 0, /*tp_as_number*/ |
---|
112 | n/a | 0, /*tp_as_sequence*/ |
---|
113 | n/a | 0, /*tp_as_mapping*/ |
---|
114 | n/a | 0, /*tp_hash*/ |
---|
115 | n/a | 0, /*tp_call*/ |
---|
116 | n/a | 0, /*tp_str*/ |
---|
117 | n/a | (getattrofunc)Xxo_getattro, /*tp_getattro*/ |
---|
118 | n/a | 0, /*tp_setattro*/ |
---|
119 | n/a | 0, /*tp_as_buffer*/ |
---|
120 | n/a | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
---|
121 | n/a | 0, /*tp_doc*/ |
---|
122 | n/a | 0, /*tp_traverse*/ |
---|
123 | n/a | 0, /*tp_clear*/ |
---|
124 | n/a | 0, /*tp_richcompare*/ |
---|
125 | n/a | 0, /*tp_weaklistoffset*/ |
---|
126 | n/a | 0, /*tp_iter*/ |
---|
127 | n/a | 0, /*tp_iternext*/ |
---|
128 | n/a | Xxo_methods, /*tp_methods*/ |
---|
129 | n/a | 0, /*tp_members*/ |
---|
130 | n/a | 0, /*tp_getset*/ |
---|
131 | n/a | 0, /*tp_base*/ |
---|
132 | n/a | 0, /*tp_dict*/ |
---|
133 | n/a | 0, /*tp_descr_get*/ |
---|
134 | n/a | 0, /*tp_descr_set*/ |
---|
135 | n/a | 0, /*tp_dictoffset*/ |
---|
136 | n/a | 0, /*tp_init*/ |
---|
137 | n/a | 0, /*tp_alloc*/ |
---|
138 | n/a | 0, /*tp_new*/ |
---|
139 | n/a | 0, /*tp_free*/ |
---|
140 | n/a | 0, /*tp_is_gc*/ |
---|
141 | n/a | }; |
---|
142 | n/a | /* --------------------------------------------------------------------- */ |
---|
143 | n/a | |
---|
144 | n/a | /* Function of two integers returning integer */ |
---|
145 | n/a | |
---|
146 | n/a | PyDoc_STRVAR(xx_foo_doc, |
---|
147 | n/a | "foo(i,j)\n\ |
---|
148 | n/a | \n\ |
---|
149 | n/a | Return the sum of i and j."); |
---|
150 | n/a | |
---|
151 | n/a | static PyObject * |
---|
152 | n/a | xx_foo(PyObject *self, PyObject *args) |
---|
153 | n/a | { |
---|
154 | n/a | long i, j; |
---|
155 | n/a | long res; |
---|
156 | n/a | if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) |
---|
157 | n/a | return NULL; |
---|
158 | n/a | res = i+j; /* XXX Do something here */ |
---|
159 | n/a | return PyLong_FromLong(res); |
---|
160 | n/a | } |
---|
161 | n/a | |
---|
162 | n/a | |
---|
163 | n/a | /* Function of no arguments returning new Xxo object */ |
---|
164 | n/a | |
---|
165 | n/a | static PyObject * |
---|
166 | n/a | xx_new(PyObject *self, PyObject *args) |
---|
167 | n/a | { |
---|
168 | n/a | XxoObject *rv; |
---|
169 | n/a | |
---|
170 | n/a | if (!PyArg_ParseTuple(args, ":new")) |
---|
171 | n/a | return NULL; |
---|
172 | n/a | rv = newXxoObject(args); |
---|
173 | n/a | if (rv == NULL) |
---|
174 | n/a | return NULL; |
---|
175 | n/a | return (PyObject *)rv; |
---|
176 | n/a | } |
---|
177 | n/a | |
---|
178 | n/a | /* Example with subtle bug from extensions manual ("Thin Ice"). */ |
---|
179 | n/a | |
---|
180 | n/a | static PyObject * |
---|
181 | n/a | xx_bug(PyObject *self, PyObject *args) |
---|
182 | n/a | { |
---|
183 | n/a | PyObject *list, *item; |
---|
184 | n/a | |
---|
185 | n/a | if (!PyArg_ParseTuple(args, "O:bug", &list)) |
---|
186 | n/a | return NULL; |
---|
187 | n/a | |
---|
188 | n/a | item = PyList_GetItem(list, 0); |
---|
189 | n/a | /* Py_INCREF(item); */ |
---|
190 | n/a | PyList_SetItem(list, 1, PyLong_FromLong(0L)); |
---|
191 | n/a | PyObject_Print(item, stdout, 0); |
---|
192 | n/a | printf("\n"); |
---|
193 | n/a | /* Py_DECREF(item); */ |
---|
194 | n/a | |
---|
195 | n/a | Py_INCREF(Py_None); |
---|
196 | n/a | return Py_None; |
---|
197 | n/a | } |
---|
198 | n/a | |
---|
199 | n/a | /* Test bad format character */ |
---|
200 | n/a | |
---|
201 | n/a | static PyObject * |
---|
202 | n/a | xx_roj(PyObject *self, PyObject *args) |
---|
203 | n/a | { |
---|
204 | n/a | PyObject *a; |
---|
205 | n/a | long b; |
---|
206 | n/a | if (!PyArg_ParseTuple(args, "O#:roj", &a, &b)) |
---|
207 | n/a | return NULL; |
---|
208 | n/a | Py_INCREF(Py_None); |
---|
209 | n/a | return Py_None; |
---|
210 | n/a | } |
---|
211 | n/a | |
---|
212 | n/a | |
---|
213 | n/a | /* ---------- */ |
---|
214 | n/a | |
---|
215 | n/a | static PyTypeObject Str_Type = { |
---|
216 | n/a | /* The ob_type field must be initialized in the module init function |
---|
217 | n/a | * to be portable to Windows without using C++. */ |
---|
218 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
219 | n/a | "xxmodule.Str", /*tp_name*/ |
---|
220 | n/a | 0, /*tp_basicsize*/ |
---|
221 | n/a | 0, /*tp_itemsize*/ |
---|
222 | n/a | /* methods */ |
---|
223 | n/a | 0, /*tp_dealloc*/ |
---|
224 | n/a | 0, /*tp_print*/ |
---|
225 | n/a | 0, /*tp_getattr*/ |
---|
226 | n/a | 0, /*tp_setattr*/ |
---|
227 | n/a | 0, /*tp_reserved*/ |
---|
228 | n/a | 0, /*tp_repr*/ |
---|
229 | n/a | 0, /*tp_as_number*/ |
---|
230 | n/a | 0, /*tp_as_sequence*/ |
---|
231 | n/a | 0, /*tp_as_mapping*/ |
---|
232 | n/a | 0, /*tp_hash*/ |
---|
233 | n/a | 0, /*tp_call*/ |
---|
234 | n/a | 0, /*tp_str*/ |
---|
235 | n/a | 0, /*tp_getattro*/ |
---|
236 | n/a | 0, /*tp_setattro*/ |
---|
237 | n/a | 0, /*tp_as_buffer*/ |
---|
238 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
239 | n/a | 0, /*tp_doc*/ |
---|
240 | n/a | 0, /*tp_traverse*/ |
---|
241 | n/a | 0, /*tp_clear*/ |
---|
242 | n/a | 0, /*tp_richcompare*/ |
---|
243 | n/a | 0, /*tp_weaklistoffset*/ |
---|
244 | n/a | 0, /*tp_iter*/ |
---|
245 | n/a | 0, /*tp_iternext*/ |
---|
246 | n/a | 0, /*tp_methods*/ |
---|
247 | n/a | 0, /*tp_members*/ |
---|
248 | n/a | 0, /*tp_getset*/ |
---|
249 | n/a | 0, /* see PyInit_xx */ /*tp_base*/ |
---|
250 | n/a | 0, /*tp_dict*/ |
---|
251 | n/a | 0, /*tp_descr_get*/ |
---|
252 | n/a | 0, /*tp_descr_set*/ |
---|
253 | n/a | 0, /*tp_dictoffset*/ |
---|
254 | n/a | 0, /*tp_init*/ |
---|
255 | n/a | 0, /*tp_alloc*/ |
---|
256 | n/a | 0, /*tp_new*/ |
---|
257 | n/a | 0, /*tp_free*/ |
---|
258 | n/a | 0, /*tp_is_gc*/ |
---|
259 | n/a | }; |
---|
260 | n/a | |
---|
261 | n/a | /* ---------- */ |
---|
262 | n/a | |
---|
263 | n/a | static PyObject * |
---|
264 | n/a | null_richcompare(PyObject *self, PyObject *other, int op) |
---|
265 | n/a | { |
---|
266 | n/a | Py_INCREF(Py_NotImplemented); |
---|
267 | n/a | return Py_NotImplemented; |
---|
268 | n/a | } |
---|
269 | n/a | |
---|
270 | n/a | static PyTypeObject Null_Type = { |
---|
271 | n/a | /* The ob_type field must be initialized in the module init function |
---|
272 | n/a | * to be portable to Windows without using C++. */ |
---|
273 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
274 | n/a | "xxmodule.Null", /*tp_name*/ |
---|
275 | n/a | 0, /*tp_basicsize*/ |
---|
276 | n/a | 0, /*tp_itemsize*/ |
---|
277 | n/a | /* methods */ |
---|
278 | n/a | 0, /*tp_dealloc*/ |
---|
279 | n/a | 0, /*tp_print*/ |
---|
280 | n/a | 0, /*tp_getattr*/ |
---|
281 | n/a | 0, /*tp_setattr*/ |
---|
282 | n/a | 0, /*tp_reserved*/ |
---|
283 | n/a | 0, /*tp_repr*/ |
---|
284 | n/a | 0, /*tp_as_number*/ |
---|
285 | n/a | 0, /*tp_as_sequence*/ |
---|
286 | n/a | 0, /*tp_as_mapping*/ |
---|
287 | n/a | 0, /*tp_hash*/ |
---|
288 | n/a | 0, /*tp_call*/ |
---|
289 | n/a | 0, /*tp_str*/ |
---|
290 | n/a | 0, /*tp_getattro*/ |
---|
291 | n/a | 0, /*tp_setattro*/ |
---|
292 | n/a | 0, /*tp_as_buffer*/ |
---|
293 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
294 | n/a | 0, /*tp_doc*/ |
---|
295 | n/a | 0, /*tp_traverse*/ |
---|
296 | n/a | 0, /*tp_clear*/ |
---|
297 | n/a | null_richcompare, /*tp_richcompare*/ |
---|
298 | n/a | 0, /*tp_weaklistoffset*/ |
---|
299 | n/a | 0, /*tp_iter*/ |
---|
300 | n/a | 0, /*tp_iternext*/ |
---|
301 | n/a | 0, /*tp_methods*/ |
---|
302 | n/a | 0, /*tp_members*/ |
---|
303 | n/a | 0, /*tp_getset*/ |
---|
304 | n/a | 0, /* see PyInit_xx */ /*tp_base*/ |
---|
305 | n/a | 0, /*tp_dict*/ |
---|
306 | n/a | 0, /*tp_descr_get*/ |
---|
307 | n/a | 0, /*tp_descr_set*/ |
---|
308 | n/a | 0, /*tp_dictoffset*/ |
---|
309 | n/a | 0, /*tp_init*/ |
---|
310 | n/a | 0, /*tp_alloc*/ |
---|
311 | n/a | 0, /* see PyInit_xx */ /*tp_new*/ |
---|
312 | n/a | 0, /*tp_free*/ |
---|
313 | n/a | 0, /*tp_is_gc*/ |
---|
314 | n/a | }; |
---|
315 | n/a | |
---|
316 | n/a | |
---|
317 | n/a | /* ---------- */ |
---|
318 | n/a | |
---|
319 | n/a | |
---|
320 | n/a | /* List of functions defined in the module */ |
---|
321 | n/a | |
---|
322 | n/a | static PyMethodDef xx_methods[] = { |
---|
323 | n/a | {"roj", xx_roj, METH_VARARGS, |
---|
324 | n/a | PyDoc_STR("roj(a,b) -> None")}, |
---|
325 | n/a | {"foo", xx_foo, METH_VARARGS, |
---|
326 | n/a | xx_foo_doc}, |
---|
327 | n/a | {"new", xx_new, METH_VARARGS, |
---|
328 | n/a | PyDoc_STR("new() -> new Xx object")}, |
---|
329 | n/a | {"bug", xx_bug, METH_VARARGS, |
---|
330 | n/a | PyDoc_STR("bug(o) -> None")}, |
---|
331 | n/a | {NULL, NULL} /* sentinel */ |
---|
332 | n/a | }; |
---|
333 | n/a | |
---|
334 | n/a | PyDoc_STRVAR(module_doc, |
---|
335 | n/a | "This is a template module just for instruction."); |
---|
336 | n/a | |
---|
337 | n/a | |
---|
338 | n/a | static int |
---|
339 | n/a | xx_exec(PyObject *m) |
---|
340 | n/a | { |
---|
341 | n/a | /* Due to cross platform compiler issues the slots must be filled |
---|
342 | n/a | * here. It's required for portability to Windows without requiring |
---|
343 | n/a | * C++. */ |
---|
344 | n/a | Null_Type.tp_base = &PyBaseObject_Type; |
---|
345 | n/a | Null_Type.tp_new = PyType_GenericNew; |
---|
346 | n/a | Str_Type.tp_base = &PyUnicode_Type; |
---|
347 | n/a | |
---|
348 | n/a | /* Finalize the type object including setting type of the new type |
---|
349 | n/a | * object; doing it here is required for portability, too. */ |
---|
350 | n/a | if (PyType_Ready(&Xxo_Type) < 0) |
---|
351 | n/a | goto fail; |
---|
352 | n/a | |
---|
353 | n/a | /* Add some symbolic constants to the module */ |
---|
354 | n/a | if (ErrorObject == NULL) { |
---|
355 | n/a | ErrorObject = PyErr_NewException("xx.error", NULL, NULL); |
---|
356 | n/a | if (ErrorObject == NULL) |
---|
357 | n/a | goto fail; |
---|
358 | n/a | } |
---|
359 | n/a | Py_INCREF(ErrorObject); |
---|
360 | n/a | PyModule_AddObject(m, "error", ErrorObject); |
---|
361 | n/a | |
---|
362 | n/a | /* Add Str */ |
---|
363 | n/a | if (PyType_Ready(&Str_Type) < 0) |
---|
364 | n/a | goto fail; |
---|
365 | n/a | PyModule_AddObject(m, "Str", (PyObject *)&Str_Type); |
---|
366 | n/a | |
---|
367 | n/a | /* Add Null */ |
---|
368 | n/a | if (PyType_Ready(&Null_Type) < 0) |
---|
369 | n/a | goto fail; |
---|
370 | n/a | PyModule_AddObject(m, "Null", (PyObject *)&Null_Type); |
---|
371 | n/a | return 0; |
---|
372 | n/a | fail: |
---|
373 | n/a | Py_XDECREF(m); |
---|
374 | n/a | return -1; |
---|
375 | n/a | } |
---|
376 | n/a | |
---|
377 | n/a | static struct PyModuleDef_Slot xx_slots[] = { |
---|
378 | n/a | {Py_mod_exec, xx_exec}, |
---|
379 | n/a | {0, NULL}, |
---|
380 | n/a | }; |
---|
381 | n/a | |
---|
382 | n/a | static struct PyModuleDef xxmodule = { |
---|
383 | n/a | PyModuleDef_HEAD_INIT, |
---|
384 | n/a | "xx", |
---|
385 | n/a | module_doc, |
---|
386 | n/a | 0, |
---|
387 | n/a | xx_methods, |
---|
388 | n/a | xx_slots, |
---|
389 | n/a | NULL, |
---|
390 | n/a | NULL, |
---|
391 | n/a | NULL |
---|
392 | n/a | }; |
---|
393 | n/a | |
---|
394 | n/a | /* Export function for the module (*must* be called PyInit_xx) */ |
---|
395 | n/a | |
---|
396 | n/a | PyMODINIT_FUNC |
---|
397 | n/a | PyInit_xx(void) |
---|
398 | n/a | { |
---|
399 | n/a | return PyModuleDef_Init(&xxmodule); |
---|
400 | n/a | } |
---|