Added 'list_only' option (and modified 'run()' to respect it).
[python/dscho.git] / Modules / xxmodule.c
blob04a474ce49cc1b0861b6cbf872e410783b384985
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 statichere PyTypeObject Xxo_Type = {
136 /* The ob_type field must be initialized in the module init function
137 * to be portable to Windows without using C++. */
138 PyObject_HEAD_INIT(NULL)
139 0, /*ob_size*/
140 "Xxo", /*tp_name*/
141 sizeof(XxoObject), /*tp_basicsize*/
142 0, /*tp_itemsize*/
143 /* methods */
144 (destructor)Xxo_dealloc, /*tp_dealloc*/
145 0, /*tp_print*/
146 (getattrfunc)Xxo_getattr, /*tp_getattr*/
147 (setattrfunc)Xxo_setattr, /*tp_setattr*/
148 0, /*tp_compare*/
149 0, /*tp_repr*/
150 0, /*tp_as_number*/
151 0, /*tp_as_sequence*/
152 0, /*tp_as_mapping*/
153 0, /*tp_hash*/
155 /* --------------------------------------------------------------------- */
157 /* Function of two integers returning integer */
159 static PyObject *
160 xx_foo(self, args)
161 PyObject *self; /* Not used */
162 PyObject *args;
164 long i, j;
165 long res;
166 if (!PyArg_ParseTuple(args, "ll", &i, &j))
167 return NULL;
168 res = i+j; /* XXX Do something here */
169 return PyInt_FromLong(res);
173 /* Function of no arguments returning new Xxo object */
175 static PyObject *
176 xx_new(self, args)
177 PyObject *self; /* Not used */
178 PyObject *args;
180 XxoObject *rv;
182 if (!PyArg_ParseTuple(args, ""))
183 return NULL;
184 rv = newXxoObject(args);
185 if ( rv == NULL )
186 return NULL;
187 return (PyObject *)rv;
190 /* Example with subtle bug from extensions manual ("Thin Ice"). */
192 static PyObject *
193 xx_bug(self, args)
194 PyObject *self;
195 PyObject *args;
197 PyObject *list, *item;
199 if (!PyArg_ParseTuple(args, "O", &list))
200 return NULL;
202 item = PyList_GetItem(list, 0);
203 /* Py_INCREF(item); */
204 PyList_SetItem(list, 1, PyInt_FromLong(0L));
205 PyObject_Print(item, stdout, 0);
206 printf("\n");
207 /* Py_DECREF(item); */
209 Py_INCREF(Py_None);
210 return Py_None;
213 /* Test bad format character */
215 static PyObject *
216 xx_roj(self, args)
217 PyObject *self; /* Not used */
218 PyObject *args;
220 PyObject *a;
221 long b;
222 if (!PyArg_ParseTuple(args, "O#", &a, &b))
223 return NULL;
224 Py_INCREF(Py_None);
225 return Py_None;
229 /* List of functions defined in the module */
231 static PyMethodDef xx_methods[] = {
232 {"roj", xx_roj, 1},
233 {"foo", xx_foo, 1},
234 {"new", xx_new, 1},
235 {"bug", xx_bug, 1},
236 {NULL, NULL} /* sentinel */
240 /* Initialization function for the module (*must* be called initxx) */
242 DL_EXPORT(void)
243 initxx()
245 PyObject *m, *d;
247 /* Initialize the type of the new type object here; doing it here
248 * is required for portability to Windows without requiring C++. */
249 Xxo_Type.ob_type = &PyType_Type;
251 /* Create the module and add the functions */
252 m = Py_InitModule("xx", xx_methods);
254 /* Add some symbolic constants to the module */
255 d = PyModule_GetDict(m);
256 ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
257 PyDict_SetItemString(d, "error", ErrorObject);