blob: a31341c146f75e5fe045bdea2b0e59d7a453616a [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002/* Use this file as a template to start implementing a module that
Andrew M. Kuchlingf580d272000-08-19 15:36:41 +00003 also declares object types. All occurrences of 'Xxo' should be changed
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004 to something reasonable for your objects. After that, all other
5 occurrences of 'xx' should be changed to something reasonable for your
6 module. If your module is named foo your sourcefile should be named
7 foomodule.c.
Tim Petersc9ca5c82002-05-23 15:49:38 +00008
Guido van Rossum14ed0b21994-09-29 09:50:09 +00009 You will probably want to delete all references to 'x_attr' and add
10 your own types of attributes instead. Maybe you want to name your
11 local variables other than 'self'. If your object type is needed in
12 other files, you'll have to create a file "foobarobject.h"; see
13 intobject.h for an example. */
14
15/* Xxo objects */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000016
Guido van Rossum2b654441996-07-30 16:56:16 +000017#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000018
Guido van Rossum2b654441996-07-30 16:56:16 +000019static PyObject *ErrorObject;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000020
21typedef struct {
Guido van Rossum2b654441996-07-30 16:56:16 +000022 PyObject_HEAD
23 PyObject *x_attr; /* Attributes dictionary */
24} XxoObject;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000025
Jeremy Hylton938ace62002-07-17 16:30:39 +000026static PyTypeObject Xxo_Type;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000027
Christian Heimes90aa7642007-12-19 02:45:37 +000028#define XxoObject_Check(v) (Py_TYPE(v) == &Xxo_Type)
Guido van Rossum14ed0b21994-09-29 09:50:09 +000029
Guido van Rossum2b654441996-07-30 16:56:16 +000030static XxoObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000031newXxoObject(PyObject *arg)
Guido van Rossum14ed0b21994-09-29 09:50:09 +000032{
Guido van Rossum2b654441996-07-30 16:56:16 +000033 XxoObject *self;
Guido van Rossumb18618d2000-05-03 23:44:39 +000034 self = PyObject_New(XxoObject, &Xxo_Type);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000035 if (self == NULL)
36 return NULL;
37 self->x_attr = NULL;
38 return self;
39}
40
41/* Xxo methods */
42
43static void
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000044Xxo_dealloc(XxoObject *self)
Guido van Rossum14ed0b21994-09-29 09:50:09 +000045{
Guido van Rossum2b654441996-07-30 16:56:16 +000046 Py_XDECREF(self->x_attr);
Guido van Rossumb18618d2000-05-03 23:44:39 +000047 PyObject_Del(self);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000048}
49
Guido van Rossum2b654441996-07-30 16:56:16 +000050static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000051Xxo_demo(XxoObject *self, PyObject *args)
Guido van Rossum14ed0b21994-09-29 09:50:09 +000052{
Guido van Rossum43713e52000-02-29 13:59:29 +000053 if (!PyArg_ParseTuple(args, ":demo"))
Guido van Rossum14ed0b21994-09-29 09:50:09 +000054 return NULL;
Guido van Rossum2b654441996-07-30 16:56:16 +000055 Py_INCREF(Py_None);
56 return Py_None;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000057}
58
Guido van Rossum2b654441996-07-30 16:56:16 +000059static PyMethodDef Xxo_methods[] = {
Skip Montanarod9e7d242002-08-14 01:44:33 +000060 {"demo", (PyCFunction)Xxo_demo, METH_VARARGS,
61 PyDoc_STR("demo() -> None")},
Guido van Rossum14ed0b21994-09-29 09:50:09 +000062 {NULL, NULL} /* sentinel */
63};
64
Guido van Rossum2b654441996-07-30 16:56:16 +000065static PyObject *
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +000066Xxo_getattro(XxoObject *self, PyObject *name)
Guido van Rossum14ed0b21994-09-29 09:50:09 +000067{
68 if (self->x_attr != NULL) {
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +000069 PyObject *v = PyDict_GetItem(self->x_attr, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000070 if (v != NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +000071 Py_INCREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000072 return v;
73 }
74 }
Amaury Forgeot d'Arc4b02c5a2008-07-02 23:22:30 +000075 return PyObject_GenericGetAttr((PyObject *)self, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000076}
77
78static int
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000079Xxo_setattr(XxoObject *self, char *name, PyObject *v)
Guido van Rossum14ed0b21994-09-29 09:50:09 +000080{
81 if (self->x_attr == NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +000082 self->x_attr = PyDict_New();
Guido van Rossum14ed0b21994-09-29 09:50:09 +000083 if (self->x_attr == NULL)
84 return -1;
85 }
86 if (v == NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +000087 int rv = PyDict_DelItemString(self->x_attr, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000088 if (rv < 0)
Guido van Rossum2b654441996-07-30 16:56:16 +000089 PyErr_SetString(PyExc_AttributeError,
90 "delete non-existing Xxo attribute");
Guido van Rossum14ed0b21994-09-29 09:50:09 +000091 return rv;
92 }
93 else
Guido van Rossum2b654441996-07-30 16:56:16 +000094 return PyDict_SetItemString(self->x_attr, name, v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000095}
96
Tim Peters0c322792002-07-17 16:49:03 +000097static PyTypeObject Xxo_Type = {
Fred Drake67248351999-02-16 22:15:42 +000098 /* The ob_type field must be initialized in the module init function
99 * to be portable to Windows without using C++. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000100 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000101 "xxmodule.Xxo", /*tp_name*/
Guido van Rossum2b654441996-07-30 16:56:16 +0000102 sizeof(XxoObject), /*tp_basicsize*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000103 0, /*tp_itemsize*/
104 /* methods */
Guido van Rossum2b654441996-07-30 16:56:16 +0000105 (destructor)Xxo_dealloc, /*tp_dealloc*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000106 0, /*tp_print*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000107 (getattrfunc)0, /*tp_getattr*/
Guido van Rossum2b654441996-07-30 16:56:16 +0000108 (setattrfunc)Xxo_setattr, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000109 0, /*tp_reserved*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000110 0, /*tp_repr*/
111 0, /*tp_as_number*/
112 0, /*tp_as_sequence*/
113 0, /*tp_as_mapping*/
114 0, /*tp_hash*/
Martin v. Löwisffa7aff2001-10-09 10:46:58 +0000115 0, /*tp_call*/
116 0, /*tp_str*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000117 (getattrofunc)Xxo_getattro, /*tp_getattro*/
Martin v. Löwisffa7aff2001-10-09 10:46:58 +0000118 0, /*tp_setattro*/
119 0, /*tp_as_buffer*/
120 Py_TPFLAGS_DEFAULT, /*tp_flags*/
121 0, /*tp_doc*/
122 0, /*tp_traverse*/
123 0, /*tp_clear*/
124 0, /*tp_richcompare*/
125 0, /*tp_weaklistoffset*/
126 0, /*tp_iter*/
127 0, /*tp_iternext*/
Amaury Forgeot d'Arc1f900f12008-07-02 22:38:47 +0000128 Xxo_methods, /*tp_methods*/
Martin v. Löwisffa7aff2001-10-09 10:46:58 +0000129 0, /*tp_members*/
130 0, /*tp_getset*/
131 0, /*tp_base*/
132 0, /*tp_dict*/
133 0, /*tp_descr_get*/
134 0, /*tp_descr_set*/
135 0, /*tp_dictoffset*/
136 0, /*tp_init*/
137 0, /*tp_alloc*/
138 0, /*tp_new*/
139 0, /*tp_free*/
140 0, /*tp_is_gc*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000141};
142/* --------------------------------------------------------------------- */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000143
144/* Function of two integers returning integer */
145
Skip Montanarod9e7d242002-08-14 01:44:33 +0000146PyDoc_STRVAR(xx_foo_doc,
147"foo(i,j)\n\
148\n\
149Return the sum of i and j.");
150
Guido van Rossum2b654441996-07-30 16:56:16 +0000151static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000152xx_foo(PyObject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000153{
154 long i, j;
155 long res;
Guido van Rossum43713e52000-02-29 13:59:29 +0000156 if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000157 return NULL;
158 res = i+j; /* XXX Do something here */
Christian Heimes217cfd12007-12-02 14:31:20 +0000159 return PyLong_FromLong(res);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000160}
161
162
Guido van Rossum2b654441996-07-30 16:56:16 +0000163/* Function of no arguments returning new Xxo object */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000164
Guido van Rossum2b654441996-07-30 16:56:16 +0000165static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000166xx_new(PyObject *self, PyObject *args)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000167{
Guido van Rossum2b654441996-07-30 16:56:16 +0000168 XxoObject *rv;
Tim Petersc9ca5c82002-05-23 15:49:38 +0000169
Guido van Rossum43713e52000-02-29 13:59:29 +0000170 if (!PyArg_ParseTuple(args, ":new"))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000171 return NULL;
Guido van Rossum2b654441996-07-30 16:56:16 +0000172 rv = newXxoObject(args);
Tim Petersc9ca5c82002-05-23 15:49:38 +0000173 if (rv == NULL)
174 return NULL;
Guido van Rossum2b654441996-07-30 16:56:16 +0000175 return (PyObject *)rv;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000176}
177
Guido van Rossumfbcfd521996-12-13 02:57:25 +0000178/* Example with subtle bug from extensions manual ("Thin Ice"). */
179
180static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000181xx_bug(PyObject *self, PyObject *args)
Guido van Rossumfbcfd521996-12-13 02:57:25 +0000182{
183 PyObject *list, *item;
Tim Petersc9ca5c82002-05-23 15:49:38 +0000184
Guido van Rossum43713e52000-02-29 13:59:29 +0000185 if (!PyArg_ParseTuple(args, "O:bug", &list))
Guido van Rossumfbcfd521996-12-13 02:57:25 +0000186 return NULL;
Tim Petersc9ca5c82002-05-23 15:49:38 +0000187
Guido van Rossumfbcfd521996-12-13 02:57:25 +0000188 item = PyList_GetItem(list, 0);
189 /* Py_INCREF(item); */
Christian Heimes217cfd12007-12-02 14:31:20 +0000190 PyList_SetItem(list, 1, PyLong_FromLong(0L));
Guido van Rossumfbcfd521996-12-13 02:57:25 +0000191 PyObject_Print(item, stdout, 0);
192 printf("\n");
193 /* Py_DECREF(item); */
Tim Petersc9ca5c82002-05-23 15:49:38 +0000194
Guido van Rossumfbcfd521996-12-13 02:57:25 +0000195 Py_INCREF(Py_None);
196 return Py_None;
197}
198
Guido van Rossumc525e431997-12-09 20:37:25 +0000199/* Test bad format character */
200
201static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +0000202xx_roj(PyObject *self, PyObject *args)
Guido van Rossumc525e431997-12-09 20:37:25 +0000203{
204 PyObject *a;
205 long b;
Guido van Rossum43713e52000-02-29 13:59:29 +0000206 if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
Guido van Rossumc525e431997-12-09 20:37:25 +0000207 return NULL;
208 Py_INCREF(Py_None);
209 return Py_None;
210}
211
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000212
Guido van Rossum9eb67ea2003-02-11 21:19:11 +0000213/* ---------- */
214
215static PyTypeObject Str_Type = {
216 /* The ob_type field must be initialized in the module init function
217 * to be portable to Windows without using C++. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000218 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum9eb67ea2003-02-11 21:19:11 +0000219 "xxmodule.Str", /*tp_name*/
220 0, /*tp_basicsize*/
221 0, /*tp_itemsize*/
222 /* methods */
223 0, /*tp_dealloc*/
224 0, /*tp_print*/
225 0, /*tp_getattr*/
226 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000227 0, /*tp_reserved*/
Guido van Rossum9eb67ea2003-02-11 21:19:11 +0000228 0, /*tp_repr*/
229 0, /*tp_as_number*/
230 0, /*tp_as_sequence*/
231 0, /*tp_as_mapping*/
232 0, /*tp_hash*/
233 0, /*tp_call*/
234 0, /*tp_str*/
235 0, /*tp_getattro*/
236 0, /*tp_setattro*/
237 0, /*tp_as_buffer*/
238 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
239 0, /*tp_doc*/
240 0, /*tp_traverse*/
241 0, /*tp_clear*/
242 0, /*tp_richcompare*/
243 0, /*tp_weaklistoffset*/
244 0, /*tp_iter*/
245 0, /*tp_iternext*/
246 0, /*tp_methods*/
247 0, /*tp_members*/
248 0, /*tp_getset*/
Martin v. Löwis1a214512008-06-11 05:26:20 +0000249 0, /* see PyInit_xx */ /*tp_base*/
Guido van Rossum9eb67ea2003-02-11 21:19:11 +0000250 0, /*tp_dict*/
251 0, /*tp_descr_get*/
252 0, /*tp_descr_set*/
253 0, /*tp_dictoffset*/
254 0, /*tp_init*/
255 0, /*tp_alloc*/
256 0, /*tp_new*/
257 0, /*tp_free*/
258 0, /*tp_is_gc*/
259};
260
Guido van Rossum72976502003-02-13 18:44:57 +0000261/* ---------- */
262
263static PyObject *
264null_richcompare(PyObject *self, PyObject *other, int op)
265{
266 Py_INCREF(Py_NotImplemented);
267 return Py_NotImplemented;
268}
269
270static PyTypeObject Null_Type = {
271 /* The ob_type field must be initialized in the module init function
272 * to be portable to Windows without using C++. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000273 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum72976502003-02-13 18:44:57 +0000274 "xxmodule.Null", /*tp_name*/
275 0, /*tp_basicsize*/
276 0, /*tp_itemsize*/
277 /* methods */
278 0, /*tp_dealloc*/
279 0, /*tp_print*/
280 0, /*tp_getattr*/
281 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000282 0, /*tp_reserved*/
Guido van Rossum72976502003-02-13 18:44:57 +0000283 0, /*tp_repr*/
284 0, /*tp_as_number*/
285 0, /*tp_as_sequence*/
286 0, /*tp_as_mapping*/
287 0, /*tp_hash*/
288 0, /*tp_call*/
289 0, /*tp_str*/
290 0, /*tp_getattro*/
291 0, /*tp_setattro*/
292 0, /*tp_as_buffer*/
293 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
294 0, /*tp_doc*/
295 0, /*tp_traverse*/
296 0, /*tp_clear*/
297 null_richcompare, /*tp_richcompare*/
298 0, /*tp_weaklistoffset*/
299 0, /*tp_iter*/
300 0, /*tp_iternext*/
301 0, /*tp_methods*/
302 0, /*tp_members*/
303 0, /*tp_getset*/
Martin v. Löwis1a214512008-06-11 05:26:20 +0000304 0, /* see PyInit_xx */ /*tp_base*/
Guido van Rossum72976502003-02-13 18:44:57 +0000305 0, /*tp_dict*/
306 0, /*tp_descr_get*/
307 0, /*tp_descr_set*/
308 0, /*tp_dictoffset*/
309 0, /*tp_init*/
310 0, /*tp_alloc*/
Martin v. Löwis1a214512008-06-11 05:26:20 +0000311 0, /* see PyInit_xx */ /*tp_new*/
Guido van Rossum72976502003-02-13 18:44:57 +0000312 0, /*tp_free*/
313 0, /*tp_is_gc*/
314};
315
Guido van Rossum9eb67ea2003-02-11 21:19:11 +0000316
317/* ---------- */
318
319
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000320/* List of functions defined in the module */
321
Guido van Rossum2b654441996-07-30 16:56:16 +0000322static PyMethodDef xx_methods[] = {
Skip Montanarod9e7d242002-08-14 01:44:33 +0000323 {"roj", xx_roj, METH_VARARGS,
324 PyDoc_STR("roj(a,b) -> None")},
325 {"foo", xx_foo, METH_VARARGS,
326 xx_foo_doc},
327 {"new", xx_new, METH_VARARGS,
328 PyDoc_STR("new() -> new Xx object")},
329 {"bug", xx_bug, METH_VARARGS,
330 PyDoc_STR("bug(o) -> None")},
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000331 {NULL, NULL} /* sentinel */
332};
333
Skip Montanarod9e7d242002-08-14 01:44:33 +0000334PyDoc_STRVAR(module_doc,
335"This is a template module just for instruction.");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000336
Martin v. Löwis1a214512008-06-11 05:26:20 +0000337/* Initialization function for the module (*must* be called PyInit_xx) */
338
339
340static struct PyModuleDef xxmodule = {
341 PyModuleDef_HEAD_INIT,
342 "xx",
343 module_doc,
344 -1,
345 xx_methods,
346 NULL,
347 NULL,
348 NULL,
349 NULL
350};
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000351
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000352PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000353PyInit_xx(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000354{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000355 PyObject *m = NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000356
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000357 /* Due to cross platform compiler issues the slots must be filled
358 * here. It's required for portability to Windows without requiring
359 * C++. */
Christian Heimes80101a82007-11-18 21:30:36 +0000360 Null_Type.tp_base = &PyBaseObject_Type;
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000361 Null_Type.tp_new = PyType_GenericNew;
Christian Heimes80101a82007-11-18 21:30:36 +0000362 Str_Type.tp_base = &PyUnicode_Type;
363
Raymond Hettinger3c736f12002-12-29 17:16:49 +0000364 /* Finalize the type object including setting type of the new type
Christian Heimes38053212007-12-14 01:24:44 +0000365 * object; doing it here is required for portability, too. */
Raymond Hettinger3c736f12002-12-29 17:16:49 +0000366 if (PyType_Ready(&Xxo_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000367 goto fail;
Fred Drake67248351999-02-16 22:15:42 +0000368
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000369 /* Create the module and add the functions */
Martin v. Löwis1a214512008-06-11 05:26:20 +0000370 m = PyModule_Create(&xxmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000371 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000372 goto fail;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000373
374 /* Add some symbolic constants to the module */
Fred Drake1de5a722002-03-12 21:49:44 +0000375 if (ErrorObject == NULL) {
376 ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
377 if (ErrorObject == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000378 goto fail;
Fred Drake1de5a722002-03-12 21:49:44 +0000379 }
380 Py_INCREF(ErrorObject);
Thomas Heller16305202002-04-09 12:50:13 +0000381 PyModule_AddObject(m, "error", ErrorObject);
Guido van Rossum9eb67ea2003-02-11 21:19:11 +0000382
383 /* Add Str */
384 if (PyType_Ready(&Str_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000385 goto fail;
Guido van Rossum9eb67ea2003-02-11 21:19:11 +0000386 PyModule_AddObject(m, "Str", (PyObject *)&Str_Type);
Guido van Rossum72976502003-02-13 18:44:57 +0000387
388 /* Add Null */
389 if (PyType_Ready(&Null_Type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000390 goto fail;
Guido van Rossum72976502003-02-13 18:44:57 +0000391 PyModule_AddObject(m, "Null", (PyObject *)&Null_Type);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000392 return m;
393 fail:
394 Py_XDECREF(m);
395 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000396}