Clarify portability and main program.
[python/dscho.git] / Modules / xxmodule.c
blob8de3c5d3cf97d8ee8e6310136a1b2dbefb4bfdd4
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Use this file as a template to start implementing a module that
33 also declares objects types. All occurrences of 'Xxo' should be changed
34 to something reasonable for your objects. After that, all other
35 occurrences of 'xx' should be changed to something reasonable for your
36 module. If your module is named foo your sourcefile should be named
37 foomodule.c.
39 You will probably want to delete all references to 'x_attr' and add
40 your own types of attributes instead. Maybe you want to name your
41 local variables other than 'self'. If your object type is needed in
42 other files, you'll have to create a file "foobarobject.h"; see
43 intobject.h for an example. */
45 /* Xxo objects */
47 #include "Python.h"
49 static PyObject *ErrorObject;
51 typedef struct {
52 PyObject_HEAD
53 PyObject *x_attr; /* Attributes dictionary */
54 } XxoObject;
56 staticforward PyTypeObject Xxo_Type;
58 #define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
60 static XxoObject *
61 newXxoObject(arg)
62 PyObject *arg;
64 XxoObject *self;
65 self = PyObject_NEW(XxoObject, &Xxo_Type);
66 if (self == NULL)
67 return NULL;
68 self->x_attr = NULL;
69 return self;
72 /* Xxo methods */
74 static void
75 Xxo_dealloc(self)
76 XxoObject *self;
78 Py_XDECREF(self->x_attr);
79 PyMem_DEL(self);
82 static PyObject *
83 Xxo_demo(self, args)
84 XxoObject *self;
85 PyObject *args;
87 if (!PyArg_ParseTuple(args, ""))
88 return NULL;
89 Py_INCREF(Py_None);
90 return Py_None;
93 static PyMethodDef Xxo_methods[] = {
94 {"demo", (PyCFunction)Xxo_demo, 1},
95 {NULL, NULL} /* sentinel */
98 static PyObject *
99 Xxo_getattr(self, name)
100 XxoObject *self;
101 char *name;
103 if (self->x_attr != NULL) {
104 PyObject *v = PyDict_GetItemString(self->x_attr, name);
105 if (v != NULL) {
106 Py_INCREF(v);
107 return v;
110 return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
113 static int
114 Xxo_setattr(self, name, v)
115 XxoObject *self;
116 char *name;
117 PyObject *v;
119 if (self->x_attr == NULL) {
120 self->x_attr = PyDict_New();
121 if (self->x_attr == NULL)
122 return -1;
124 if (v == NULL) {
125 int rv = PyDict_DelItemString(self->x_attr, name);
126 if (rv < 0)
127 PyErr_SetString(PyExc_AttributeError,
128 "delete non-existing Xxo attribute");
129 return rv;
131 else
132 return PyDict_SetItemString(self->x_attr, name, v);
135 staticforward PyTypeObject Xxo_Type = {
136 PyObject_HEAD_INIT(&PyType_Type)
137 0, /*ob_size*/
138 "Xxo", /*tp_name*/
139 sizeof(XxoObject), /*tp_basicsize*/
140 0, /*tp_itemsize*/
141 /* methods */
142 (destructor)Xxo_dealloc, /*tp_dealloc*/
143 0, /*tp_print*/
144 (getattrfunc)Xxo_getattr, /*tp_getattr*/
145 (setattrfunc)Xxo_setattr, /*tp_setattr*/
146 0, /*tp_compare*/
147 0, /*tp_repr*/
148 0, /*tp_as_number*/
149 0, /*tp_as_sequence*/
150 0, /*tp_as_mapping*/
151 0, /*tp_hash*/
153 /* --------------------------------------------------------------------- */
155 /* Function of two integers returning integer */
157 static PyObject *
158 xx_foo(self, args)
159 PyObject *self; /* Not used */
160 PyObject *args;
162 long i, j;
163 long res;
164 if (!PyArg_ParseTuple(args, "ll", &i, &j))
165 return NULL;
166 res = i+j; /* XXX Do something here */
167 return PyInt_FromLong(res);
171 /* Function of no arguments returning new Xxo object */
173 static PyObject *
174 xx_new(self, args)
175 PyObject *self; /* Not used */
176 PyObject *args;
178 XxoObject *rv;
180 if (!PyArg_ParseTuple(args, ""))
181 return NULL;
182 rv = newXxoObject(args);
183 if ( rv == NULL )
184 return NULL;
185 return (PyObject *)rv;
188 /* Example with subtle bug from extensions manual ("Thin Ice"). */
190 static PyObject *
191 xx_bug(self, args)
192 PyObject *self;
193 PyObject *args;
195 PyObject *list, *item;
197 if (!PyArg_ParseTuple(args, "O", &list))
198 return NULL;
200 item = PyList_GetItem(list, 0);
201 /* Py_INCREF(item); */
202 PyList_SetItem(list, 1, PyInt_FromLong(0L));
203 PyObject_Print(item, stdout, 0);
204 printf("\n");
205 /* Py_DECREF(item); */
207 Py_INCREF(Py_None);
208 return Py_None;
211 /* Test bad format character */
213 static PyObject *
214 xx_roj(self, args)
215 PyObject *self; /* Not used */
216 PyObject *args;
218 PyObject *a;
219 long b;
220 if (!PyArg_ParseTuple(args, "O#", &a, &b))
221 return NULL;
222 Py_INCREF(Py_None);
223 return Py_None;
227 /* List of functions defined in the module */
229 static PyMethodDef xx_methods[] = {
230 {"roj", xx_roj, 1},
231 {"foo", xx_foo, 1},
232 {"new", xx_new, 1},
233 {"bug", xx_bug, 1},
234 {NULL, NULL} /* sentinel */
238 /* Initialization function for the module (*must* be called initxx) */
240 void
241 initxx()
243 PyObject *m, *d;
245 /* Create the module and add the functions */
246 m = Py_InitModule("xx", xx_methods);
248 /* Add some symbolic constants to the module */
249 d = PyModule_GetDict(m);
250 ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
251 PyDict_SetItemString(d, "error", ErrorObject);