blob: fb0c6fbcbc09131fda0a9f49326ff07aeaeffead [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.
8
9 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
Guido van Rossum2b654441996-07-30 16:56:16 +000026staticforward PyTypeObject Xxo_Type;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000027
Guido van Rossum2b654441996-07-30 16:56:16 +000028#define XxoObject_Check(v) ((v)->ob_type == &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[] = {
Andrew M. Kuchlingf580d272000-08-19 15:36:41 +000060 {"demo", (PyCFunction)Xxo_demo, METH_VARARGS},
Guido van Rossum14ed0b21994-09-29 09:50:09 +000061 {NULL, NULL} /* sentinel */
62};
63
Guido van Rossum2b654441996-07-30 16:56:16 +000064static PyObject *
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000065Xxo_getattr(XxoObject *self, char *name)
Guido van Rossum14ed0b21994-09-29 09:50:09 +000066{
67 if (self->x_attr != NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +000068 PyObject *v = PyDict_GetItemString(self->x_attr, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000069 if (v != NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +000070 Py_INCREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000071 return v;
72 }
73 }
Guido van Rossum2b654441996-07-30 16:56:16 +000074 return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000075}
76
77static int
Peter Schneider-Kampc4bc0e02000-07-10 11:56:03 +000078Xxo_setattr(XxoObject *self, char *name, PyObject *v)
Guido van Rossum14ed0b21994-09-29 09:50:09 +000079{
80 if (self->x_attr == NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +000081 self->x_attr = PyDict_New();
Guido van Rossum14ed0b21994-09-29 09:50:09 +000082 if (self->x_attr == NULL)
83 return -1;
84 }
85 if (v == NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +000086 int rv = PyDict_DelItemString(self->x_attr, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000087 if (rv < 0)
Guido van Rossum2b654441996-07-30 16:56:16 +000088 PyErr_SetString(PyExc_AttributeError,
89 "delete non-existing Xxo attribute");
Guido van Rossum14ed0b21994-09-29 09:50:09 +000090 return rv;
91 }
92 else
Guido van Rossum2b654441996-07-30 16:56:16 +000093 return PyDict_SetItemString(self->x_attr, name, v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000094}
95
Fred Drake67248351999-02-16 22:15:42 +000096statichere PyTypeObject Xxo_Type = {
97 /* The ob_type field must be initialized in the module init function
98 * to be portable to Windows without using C++. */
99 PyObject_HEAD_INIT(NULL)
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000100 0, /*ob_size*/
Guido van Rossum2b654441996-07-30 16:56:16 +0000101 "Xxo", /*tp_name*/
102 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*/
Guido van Rossum2b654441996-07-30 16:56:16 +0000107 (getattrfunc)Xxo_getattr, /*tp_getattr*/
108 (setattrfunc)Xxo_setattr, /*tp_setattr*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000109 0, /*tp_compare*/
110 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*/
117 0, /*tp_getattro*/
118 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*/
128 0, /*tp_methods*/
129 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*/
141 0, /*tp_bases*/
142 0, /*tp_mro*/
143 0, /*tp_defined*/
144 0, /*tp_subclasses*/
145 0, /*tp_weaklist*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000146};
147/* --------------------------------------------------------------------- */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000148
149/* Function of two integers returning integer */
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 */
Guido van Rossum2b654441996-07-30 16:56:16 +0000159 return PyInt_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;
Guido van Rossum14ed0b21994-09-29 09:50:09 +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);
Guido van Rossum14ed0b21994-09-29 09:50:09 +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;
184
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;
187
188 item = PyList_GetItem(list, 0);
189 /* Py_INCREF(item); */
190 PyList_SetItem(list, 1, PyInt_FromLong(0L));
191 PyObject_Print(item, stdout, 0);
192 printf("\n");
193 /* Py_DECREF(item); */
194
195 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
213/* List of functions defined in the module */
214
Guido van Rossum2b654441996-07-30 16:56:16 +0000215static PyMethodDef xx_methods[] = {
Andrew M. Kuchlingf580d272000-08-19 15:36:41 +0000216 {"roj", xx_roj, METH_VARARGS},
217 {"foo", xx_foo, METH_VARARGS},
218 {"new", xx_new, METH_VARARGS},
219 {"bug", xx_bug, METH_VARARGS},
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220 {NULL, NULL} /* sentinel */
221};
222
223
224/* Initialization function for the module (*must* be called initxx) */
225
Guido van Rossum3886bb61998-12-04 18:50:17 +0000226DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000227initxx(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000228{
Guido van Rossum2b654441996-07-30 16:56:16 +0000229 PyObject *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000230
Fred Drake67248351999-02-16 22:15:42 +0000231 /* Initialize the type of the new type object here; doing it here
232 * is required for portability to Windows without requiring C++. */
233 Xxo_Type.ob_type = &PyType_Type;
234
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000235 /* Create the module and add the functions */
Guido van Rossum2b654441996-07-30 16:56:16 +0000236 m = Py_InitModule("xx", xx_methods);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000237
238 /* Add some symbolic constants to the module */
Guido van Rossum2b654441996-07-30 16:56:16 +0000239 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000240 ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
Guido van Rossum2b654441996-07-30 16:56:16 +0000241 PyDict_SetItemString(d, "error", ErrorObject);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000242}