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 PyObject *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_GC_New(XxoObject, (PyTypeObject*)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 int |
---|
44 | n/a | Xxo_traverse(XxoObject *self, visitproc visit, void *arg) |
---|
45 | n/a | { |
---|
46 | n/a | Py_VISIT(self->x_attr); |
---|
47 | n/a | return 0; |
---|
48 | n/a | } |
---|
49 | n/a | |
---|
50 | n/a | static int |
---|
51 | n/a | Xxo_finalize(XxoObject *self) |
---|
52 | n/a | { |
---|
53 | n/a | Py_CLEAR(self->x_attr); |
---|
54 | n/a | return 0; |
---|
55 | n/a | } |
---|
56 | n/a | |
---|
57 | n/a | static PyObject * |
---|
58 | n/a | Xxo_demo(XxoObject *self, PyObject *args) |
---|
59 | n/a | { |
---|
60 | n/a | PyObject *o = NULL; |
---|
61 | n/a | if (!PyArg_ParseTuple(args, "|O:demo", &o)) |
---|
62 | n/a | return NULL; |
---|
63 | n/a | /* Test availability of fast type checks */ |
---|
64 | n/a | if (o != NULL && PyUnicode_Check(o)) { |
---|
65 | n/a | Py_INCREF(o); |
---|
66 | n/a | return o; |
---|
67 | n/a | } |
---|
68 | n/a | Py_INCREF(Py_None); |
---|
69 | n/a | return Py_None; |
---|
70 | n/a | } |
---|
71 | n/a | |
---|
72 | n/a | static PyMethodDef Xxo_methods[] = { |
---|
73 | n/a | {"demo", (PyCFunction)Xxo_demo, METH_VARARGS, |
---|
74 | n/a | PyDoc_STR("demo() -> None")}, |
---|
75 | n/a | {NULL, NULL} /* sentinel */ |
---|
76 | n/a | }; |
---|
77 | n/a | |
---|
78 | n/a | static PyObject * |
---|
79 | n/a | Xxo_getattro(XxoObject *self, PyObject *name) |
---|
80 | n/a | { |
---|
81 | n/a | if (self->x_attr != NULL) { |
---|
82 | n/a | PyObject *v = PyDict_GetItem(self->x_attr, name); |
---|
83 | n/a | if (v != NULL) { |
---|
84 | n/a | Py_INCREF(v); |
---|
85 | n/a | return v; |
---|
86 | n/a | } |
---|
87 | n/a | } |
---|
88 | n/a | return PyObject_GenericGetAttr((PyObject *)self, name); |
---|
89 | n/a | } |
---|
90 | n/a | |
---|
91 | n/a | static int |
---|
92 | n/a | Xxo_setattr(XxoObject *self, const char *name, PyObject *v) |
---|
93 | n/a | { |
---|
94 | n/a | if (self->x_attr == NULL) { |
---|
95 | n/a | self->x_attr = PyDict_New(); |
---|
96 | n/a | if (self->x_attr == NULL) |
---|
97 | n/a | return -1; |
---|
98 | n/a | } |
---|
99 | n/a | if (v == NULL) { |
---|
100 | n/a | int rv = PyDict_DelItemString(self->x_attr, name); |
---|
101 | n/a | if (rv < 0) |
---|
102 | n/a | PyErr_SetString(PyExc_AttributeError, |
---|
103 | n/a | "delete non-existing Xxo attribute"); |
---|
104 | n/a | return rv; |
---|
105 | n/a | } |
---|
106 | n/a | else |
---|
107 | n/a | return PyDict_SetItemString(self->x_attr, name, v); |
---|
108 | n/a | } |
---|
109 | n/a | |
---|
110 | n/a | static PyType_Slot Xxo_Type_slots[] = { |
---|
111 | n/a | {Py_tp_doc, "The Xxo type"}, |
---|
112 | n/a | {Py_tp_traverse, Xxo_traverse}, |
---|
113 | n/a | {Py_tp_finalize, Xxo_finalize}, |
---|
114 | n/a | {Py_tp_getattro, Xxo_getattro}, |
---|
115 | n/a | {Py_tp_setattr, Xxo_setattr}, |
---|
116 | n/a | {Py_tp_methods, Xxo_methods}, |
---|
117 | n/a | {0, 0}, |
---|
118 | n/a | }; |
---|
119 | n/a | |
---|
120 | n/a | static PyType_Spec Xxo_Type_spec = { |
---|
121 | n/a | "xxlimited.Xxo", |
---|
122 | n/a | sizeof(XxoObject), |
---|
123 | n/a | 0, |
---|
124 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, |
---|
125 | n/a | Xxo_Type_slots |
---|
126 | n/a | }; |
---|
127 | n/a | |
---|
128 | n/a | /* --------------------------------------------------------------------- */ |
---|
129 | n/a | |
---|
130 | n/a | /* Function of two integers returning integer */ |
---|
131 | n/a | |
---|
132 | n/a | PyDoc_STRVAR(xx_foo_doc, |
---|
133 | n/a | "foo(i,j)\n\ |
---|
134 | n/a | \n\ |
---|
135 | n/a | Return the sum of i and j."); |
---|
136 | n/a | |
---|
137 | n/a | static PyObject * |
---|
138 | n/a | xx_foo(PyObject *self, PyObject *args) |
---|
139 | n/a | { |
---|
140 | n/a | long i, j; |
---|
141 | n/a | long res; |
---|
142 | n/a | if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) |
---|
143 | n/a | return NULL; |
---|
144 | n/a | res = i+j; /* XXX Do something here */ |
---|
145 | n/a | return PyLong_FromLong(res); |
---|
146 | n/a | } |
---|
147 | n/a | |
---|
148 | n/a | |
---|
149 | n/a | /* Function of no arguments returning new Xxo object */ |
---|
150 | n/a | |
---|
151 | n/a | static PyObject * |
---|
152 | n/a | xx_new(PyObject *self, PyObject *args) |
---|
153 | n/a | { |
---|
154 | n/a | XxoObject *rv; |
---|
155 | n/a | |
---|
156 | n/a | if (!PyArg_ParseTuple(args, ":new")) |
---|
157 | n/a | return NULL; |
---|
158 | n/a | rv = newXxoObject(args); |
---|
159 | n/a | if (rv == NULL) |
---|
160 | n/a | return NULL; |
---|
161 | n/a | return (PyObject *)rv; |
---|
162 | n/a | } |
---|
163 | n/a | |
---|
164 | n/a | /* Test bad format character */ |
---|
165 | n/a | |
---|
166 | n/a | static PyObject * |
---|
167 | n/a | xx_roj(PyObject *self, PyObject *args) |
---|
168 | n/a | { |
---|
169 | n/a | PyObject *a; |
---|
170 | n/a | long b; |
---|
171 | n/a | if (!PyArg_ParseTuple(args, "O#:roj", &a, &b)) |
---|
172 | n/a | return NULL; |
---|
173 | n/a | Py_INCREF(Py_None); |
---|
174 | n/a | return Py_None; |
---|
175 | n/a | } |
---|
176 | n/a | |
---|
177 | n/a | |
---|
178 | n/a | /* ---------- */ |
---|
179 | n/a | |
---|
180 | n/a | static PyType_Slot Str_Type_slots[] = { |
---|
181 | n/a | {Py_tp_base, NULL}, /* filled out in module init function */ |
---|
182 | n/a | {0, 0}, |
---|
183 | n/a | }; |
---|
184 | n/a | |
---|
185 | n/a | static PyType_Spec Str_Type_spec = { |
---|
186 | n/a | "xxlimited.Str", |
---|
187 | n/a | 0, |
---|
188 | n/a | 0, |
---|
189 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
---|
190 | n/a | Str_Type_slots |
---|
191 | n/a | }; |
---|
192 | n/a | |
---|
193 | n/a | /* ---------- */ |
---|
194 | n/a | |
---|
195 | n/a | static PyObject * |
---|
196 | n/a | null_richcompare(PyObject *self, PyObject *other, int op) |
---|
197 | n/a | { |
---|
198 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
199 | n/a | } |
---|
200 | n/a | |
---|
201 | n/a | static PyType_Slot Null_Type_slots[] = { |
---|
202 | n/a | {Py_tp_base, NULL}, /* filled out in module init */ |
---|
203 | n/a | {Py_tp_new, NULL}, |
---|
204 | n/a | {Py_tp_richcompare, null_richcompare}, |
---|
205 | n/a | {0, 0} |
---|
206 | n/a | }; |
---|
207 | n/a | |
---|
208 | n/a | static PyType_Spec Null_Type_spec = { |
---|
209 | n/a | "xxlimited.Null", |
---|
210 | n/a | 0, /* basicsize */ |
---|
211 | n/a | 0, /* itemsize */ |
---|
212 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
---|
213 | n/a | Null_Type_slots |
---|
214 | n/a | }; |
---|
215 | n/a | |
---|
216 | n/a | /* ---------- */ |
---|
217 | n/a | |
---|
218 | n/a | /* List of functions defined in the module */ |
---|
219 | n/a | |
---|
220 | n/a | static PyMethodDef xx_methods[] = { |
---|
221 | n/a | {"roj", xx_roj, METH_VARARGS, |
---|
222 | n/a | PyDoc_STR("roj(a,b) -> None")}, |
---|
223 | n/a | {"foo", xx_foo, METH_VARARGS, |
---|
224 | n/a | xx_foo_doc}, |
---|
225 | n/a | {"new", xx_new, METH_VARARGS, |
---|
226 | n/a | PyDoc_STR("new() -> new Xx object")}, |
---|
227 | n/a | {NULL, NULL} /* sentinel */ |
---|
228 | n/a | }; |
---|
229 | n/a | |
---|
230 | n/a | PyDoc_STRVAR(module_doc, |
---|
231 | n/a | "This is a template module just for instruction."); |
---|
232 | n/a | |
---|
233 | n/a | static int |
---|
234 | n/a | xx_modexec(PyObject *m) |
---|
235 | n/a | { |
---|
236 | n/a | PyObject *o; |
---|
237 | n/a | |
---|
238 | n/a | /* Due to cross platform compiler issues the slots must be filled |
---|
239 | n/a | * here. It's required for portability to Windows without requiring |
---|
240 | n/a | * C++. */ |
---|
241 | n/a | Null_Type_slots[0].pfunc = &PyBaseObject_Type; |
---|
242 | n/a | Null_Type_slots[1].pfunc = PyType_GenericNew; |
---|
243 | n/a | Str_Type_slots[0].pfunc = &PyUnicode_Type; |
---|
244 | n/a | |
---|
245 | n/a | Xxo_Type = PyType_FromSpec(&Xxo_Type_spec); |
---|
246 | n/a | if (Xxo_Type == NULL) |
---|
247 | n/a | goto fail; |
---|
248 | n/a | |
---|
249 | n/a | /* Add some symbolic constants to the module */ |
---|
250 | n/a | if (ErrorObject == NULL) { |
---|
251 | n/a | ErrorObject = PyErr_NewException("xxlimited.error", NULL, NULL); |
---|
252 | n/a | if (ErrorObject == NULL) |
---|
253 | n/a | goto fail; |
---|
254 | n/a | } |
---|
255 | n/a | Py_INCREF(ErrorObject); |
---|
256 | n/a | PyModule_AddObject(m, "error", ErrorObject); |
---|
257 | n/a | |
---|
258 | n/a | /* Add Xxo */ |
---|
259 | n/a | o = PyType_FromSpec(&Xxo_Type_spec); |
---|
260 | n/a | if (o == NULL) |
---|
261 | n/a | goto fail; |
---|
262 | n/a | PyModule_AddObject(m, "Xxo", o); |
---|
263 | n/a | |
---|
264 | n/a | /* Add Str */ |
---|
265 | n/a | o = PyType_FromSpec(&Str_Type_spec); |
---|
266 | n/a | if (o == NULL) |
---|
267 | n/a | goto fail; |
---|
268 | n/a | PyModule_AddObject(m, "Str", o); |
---|
269 | n/a | |
---|
270 | n/a | /* Add Null */ |
---|
271 | n/a | o = PyType_FromSpec(&Null_Type_spec); |
---|
272 | n/a | if (o == NULL) |
---|
273 | n/a | goto fail; |
---|
274 | n/a | PyModule_AddObject(m, "Null", o); |
---|
275 | n/a | return 0; |
---|
276 | n/a | fail: |
---|
277 | n/a | Py_XDECREF(m); |
---|
278 | n/a | return -1; |
---|
279 | n/a | } |
---|
280 | n/a | |
---|
281 | n/a | |
---|
282 | n/a | static PyModuleDef_Slot xx_slots[] = { |
---|
283 | n/a | {Py_mod_exec, xx_modexec}, |
---|
284 | n/a | {0, NULL} |
---|
285 | n/a | }; |
---|
286 | n/a | |
---|
287 | n/a | static struct PyModuleDef xxmodule = { |
---|
288 | n/a | PyModuleDef_HEAD_INIT, |
---|
289 | n/a | "xxlimited", |
---|
290 | n/a | module_doc, |
---|
291 | n/a | 0, |
---|
292 | n/a | xx_methods, |
---|
293 | n/a | xx_slots, |
---|
294 | n/a | NULL, |
---|
295 | n/a | NULL, |
---|
296 | n/a | NULL |
---|
297 | n/a | }; |
---|
298 | n/a | |
---|
299 | n/a | /* Export function for the module (*must* be called PyInit_xx) */ |
---|
300 | n/a | |
---|
301 | n/a | PyMODINIT_FUNC |
---|
302 | n/a | PyInit_xxlimited(void) |
---|
303 | n/a | { |
---|
304 | n/a | return PyModuleDef_Init(&xxmodule); |
---|
305 | n/a | } |
---|