blob: a5a12a086ec292362f36b4d50c79839c8ebb8357 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum1984f1e1992-08-04 12:41:02 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum14ed0b21994-09-29 09:50:09 +000025/* Use this file as a template to start implementing a module that
Guido van Rossum2b654441996-07-30 16:56:16 +000026 also declares objects types. All occurrences of 'Xxo' should be changed
Guido van Rossum14ed0b21994-09-29 09:50:09 +000027 to something reasonable for your objects. After that, all other
28 occurrences of 'xx' should be changed to something reasonable for your
29 module. If your module is named foo your sourcefile should be named
30 foomodule.c.
31
32 You will probably want to delete all references to 'x_attr' and add
33 your own types of attributes instead. Maybe you want to name your
34 local variables other than 'self'. If your object type is needed in
35 other files, you'll have to create a file "foobarobject.h"; see
36 intobject.h for an example. */
37
38/* Xxo objects */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000039
Guido van Rossum2b654441996-07-30 16:56:16 +000040#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000041
Guido van Rossum2b654441996-07-30 16:56:16 +000042static PyObject *ErrorObject;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000043
44typedef struct {
Guido van Rossum2b654441996-07-30 16:56:16 +000045 PyObject_HEAD
46 PyObject *x_attr; /* Attributes dictionary */
47} XxoObject;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000048
Guido van Rossum2b654441996-07-30 16:56:16 +000049staticforward PyTypeObject Xxo_Type;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000050
Guido van Rossum2b654441996-07-30 16:56:16 +000051#define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
Guido van Rossum14ed0b21994-09-29 09:50:09 +000052
Guido van Rossum2b654441996-07-30 16:56:16 +000053static XxoObject *
54newXxoObject(arg)
55 PyObject *arg;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000056{
Guido van Rossum2b654441996-07-30 16:56:16 +000057 XxoObject *self;
58 self = PyObject_NEW(XxoObject, &Xxo_Type);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000059 if (self == NULL)
60 return NULL;
61 self->x_attr = NULL;
62 return self;
63}
64
65/* Xxo methods */
66
67static void
Guido van Rossum2b654441996-07-30 16:56:16 +000068Xxo_dealloc(self)
69 XxoObject *self;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000070{
Guido van Rossum2b654441996-07-30 16:56:16 +000071 Py_XDECREF(self->x_attr);
72 PyMem_DEL(self);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000073}
74
Guido van Rossum2b654441996-07-30 16:56:16 +000075static PyObject *
76Xxo_demo(self, args)
77 XxoObject *self;
78 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000079{
Guido van Rossum2b654441996-07-30 16:56:16 +000080 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum14ed0b21994-09-29 09:50:09 +000081 return NULL;
Guido van Rossum2b654441996-07-30 16:56:16 +000082 Py_INCREF(Py_None);
83 return Py_None;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000084}
85
Guido van Rossum2b654441996-07-30 16:56:16 +000086static PyMethodDef Xxo_methods[] = {
87 {"demo", (PyCFunction)Xxo_demo, 1},
Guido van Rossum14ed0b21994-09-29 09:50:09 +000088 {NULL, NULL} /* sentinel */
89};
90
Guido van Rossum2b654441996-07-30 16:56:16 +000091static PyObject *
92Xxo_getattr(self, name)
93 XxoObject *self;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000094 char *name;
95{
96 if (self->x_attr != NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +000097 PyObject *v = PyDict_GetItemString(self->x_attr, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000098 if (v != NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +000099 Py_INCREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000100 return v;
101 }
102 }
Guido van Rossum2b654441996-07-30 16:56:16 +0000103 return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000104}
105
106static int
Guido van Rossum2b654441996-07-30 16:56:16 +0000107Xxo_setattr(self, name, v)
108 XxoObject *self;
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000109 char *name;
Guido van Rossum2b654441996-07-30 16:56:16 +0000110 PyObject *v;
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000111{
112 if (self->x_attr == NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +0000113 self->x_attr = PyDict_New();
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000114 if (self->x_attr == NULL)
115 return -1;
116 }
117 if (v == NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +0000118 int rv = PyDict_DelItemString(self->x_attr, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000119 if (rv < 0)
Guido van Rossum2b654441996-07-30 16:56:16 +0000120 PyErr_SetString(PyExc_AttributeError,
121 "delete non-existing Xxo attribute");
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000122 return rv;
123 }
124 else
Guido van Rossum2b654441996-07-30 16:56:16 +0000125 return PyDict_SetItemString(self->x_attr, name, v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000126}
127
Guido van Rossum2b654441996-07-30 16:56:16 +0000128staticforward PyTypeObject Xxo_Type = {
129 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000130 0, /*ob_size*/
Guido van Rossum2b654441996-07-30 16:56:16 +0000131 "Xxo", /*tp_name*/
132 sizeof(XxoObject), /*tp_basicsize*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000133 0, /*tp_itemsize*/
134 /* methods */
Guido van Rossum2b654441996-07-30 16:56:16 +0000135 (destructor)Xxo_dealloc, /*tp_dealloc*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000136 0, /*tp_print*/
Guido van Rossum2b654441996-07-30 16:56:16 +0000137 (getattrfunc)Xxo_getattr, /*tp_getattr*/
138 (setattrfunc)Xxo_setattr, /*tp_setattr*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000139 0, /*tp_compare*/
140 0, /*tp_repr*/
141 0, /*tp_as_number*/
142 0, /*tp_as_sequence*/
143 0, /*tp_as_mapping*/
144 0, /*tp_hash*/
145};
146/* --------------------------------------------------------------------- */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000147
148/* Function of two integers returning integer */
149
Guido van Rossum2b654441996-07-30 16:56:16 +0000150static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000151xx_foo(self, args)
Guido van Rossum2b654441996-07-30 16:56:16 +0000152 PyObject *self; /* Not used */
153 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000154{
155 long i, j;
156 long res;
Guido van Rossum2b654441996-07-30 16:56:16 +0000157 if (!PyArg_ParseTuple(args, "ll", &i, &j))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000158 return NULL;
159 res = i+j; /* XXX Do something here */
Guido van Rossum2b654441996-07-30 16:56:16 +0000160 return PyInt_FromLong(res);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000161}
162
163
Guido van Rossum2b654441996-07-30 16:56:16 +0000164/* Function of no arguments returning new Xxo object */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000165
Guido van Rossum2b654441996-07-30 16:56:16 +0000166static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000167xx_new(self, args)
Guido van Rossum2b654441996-07-30 16:56:16 +0000168 PyObject *self; /* Not used */
169 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000170{
171 int i, j;
Guido van Rossum2b654441996-07-30 16:56:16 +0000172 XxoObject *rv;
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000173
Guido van Rossum2b654441996-07-30 16:56:16 +0000174 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000175 return NULL;
Guido van Rossum2b654441996-07-30 16:56:16 +0000176 rv = newXxoObject(args);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000177 if ( rv == NULL )
178 return NULL;
Guido van Rossum2b654441996-07-30 16:56:16 +0000179 return (PyObject *)rv;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000180}
181
182
183/* List of functions defined in the module */
184
Guido van Rossum2b654441996-07-30 16:56:16 +0000185static PyMethodDef xx_methods[] = {
186 {"foo", xx_foo, 1},
187 {"new", xx_new, 1},
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000188 {NULL, NULL} /* sentinel */
189};
190
191
192/* Initialization function for the module (*must* be called initxx) */
193
194void
195initxx()
196{
Guido van Rossum2b654441996-07-30 16:56:16 +0000197 PyObject *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000198
199 /* Create the module and add the functions */
Guido van Rossum2b654441996-07-30 16:56:16 +0000200 m = Py_InitModule("xx", xx_methods);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000201
202 /* Add some symbolic constants to the module */
Guido van Rossum2b654441996-07-30 16:56:16 +0000203 d = PyModule_GetDict(m);
204 ErrorObject = PyString_FromString("xx.error");
205 PyDict_SetItemString(d, "error", ErrorObject);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000206
207 /* Check for errors */
Guido van Rossum2b654441996-07-30 16:56:16 +0000208 if (PyErr_Occurred())
209 Py_FatalError("can't initialize module xx");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000210}