Last set of CW Pro 5 projects (probably)
[python/dscho.git] / Modules / xxmodule.c
blobe80c2eee98d44bbb6c0c581342aaafb5e4340177
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Use this file as a template to start implementing a module that
12 also declares object types. All occurrences of 'Xxo' should be changed
13 to something reasonable for your objects. After that, all other
14 occurrences of 'xx' should be changed to something reasonable for your
15 module. If your module is named foo your sourcefile should be named
16 foomodule.c.
18 You will probably want to delete all references to 'x_attr' and add
19 your own types of attributes instead. Maybe you want to name your
20 local variables other than 'self'. If your object type is needed in
21 other files, you'll have to create a file "foobarobject.h"; see
22 intobject.h for an example. */
24 /* Xxo objects */
26 #include "Python.h"
28 static PyObject *ErrorObject;
30 typedef struct {
31 PyObject_HEAD
32 PyObject *x_attr; /* Attributes dictionary */
33 } XxoObject;
35 staticforward PyTypeObject Xxo_Type;
37 #define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
39 static XxoObject *
40 newXxoObject(PyObject *arg)
42 XxoObject *self;
43 self = PyObject_New(XxoObject, &Xxo_Type);
44 if (self == NULL)
45 return NULL;
46 self->x_attr = NULL;
47 return self;
50 /* Xxo methods */
52 static void
53 Xxo_dealloc(XxoObject *self)
55 Py_XDECREF(self->x_attr);
56 PyObject_Del(self);
59 static PyObject *
60 Xxo_demo(XxoObject *self, PyObject *args)
62 if (!PyArg_ParseTuple(args, ":demo"))
63 return NULL;
64 Py_INCREF(Py_None);
65 return Py_None;
68 static PyMethodDef Xxo_methods[] = {
69 {"demo", (PyCFunction)Xxo_demo, METH_VARARGS},
70 {NULL, NULL} /* sentinel */
73 static PyObject *
74 Xxo_getattr(XxoObject *self, char *name)
76 if (self->x_attr != NULL) {
77 PyObject *v = PyDict_GetItemString(self->x_attr, name);
78 if (v != NULL) {
79 Py_INCREF(v);
80 return v;
83 return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
86 static int
87 Xxo_setattr(XxoObject *self, char *name, PyObject *v)
89 if (self->x_attr == NULL) {
90 self->x_attr = PyDict_New();
91 if (self->x_attr == NULL)
92 return -1;
94 if (v == NULL) {
95 int rv = PyDict_DelItemString(self->x_attr, name);
96 if (rv < 0)
97 PyErr_SetString(PyExc_AttributeError,
98 "delete non-existing Xxo attribute");
99 return rv;
101 else
102 return PyDict_SetItemString(self->x_attr, name, v);
105 statichere PyTypeObject Xxo_Type = {
106 /* The ob_type field must be initialized in the module init function
107 * to be portable to Windows without using C++. */
108 PyObject_HEAD_INIT(NULL)
109 0, /*ob_size*/
110 "Xxo", /*tp_name*/
111 sizeof(XxoObject), /*tp_basicsize*/
112 0, /*tp_itemsize*/
113 /* methods */
114 (destructor)Xxo_dealloc, /*tp_dealloc*/
115 0, /*tp_print*/
116 (getattrfunc)Xxo_getattr, /*tp_getattr*/
117 (setattrfunc)Xxo_setattr, /*tp_setattr*/
118 0, /*tp_compare*/
119 0, /*tp_repr*/
120 0, /*tp_as_number*/
121 0, /*tp_as_sequence*/
122 0, /*tp_as_mapping*/
123 0, /*tp_hash*/
125 /* --------------------------------------------------------------------- */
127 /* Function of two integers returning integer */
129 static PyObject *
130 xx_foo(PyObject *self, PyObject *args)
132 long i, j;
133 long res;
134 if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
135 return NULL;
136 res = i+j; /* XXX Do something here */
137 return PyInt_FromLong(res);
141 /* Function of no arguments returning new Xxo object */
143 static PyObject *
144 xx_new(PyObject *self, PyObject *args)
146 XxoObject *rv;
148 if (!PyArg_ParseTuple(args, ":new"))
149 return NULL;
150 rv = newXxoObject(args);
151 if ( rv == NULL )
152 return NULL;
153 return (PyObject *)rv;
156 /* Example with subtle bug from extensions manual ("Thin Ice"). */
158 static PyObject *
159 xx_bug(PyObject *self, PyObject *args)
161 PyObject *list, *item;
163 if (!PyArg_ParseTuple(args, "O:bug", &list))
164 return NULL;
166 item = PyList_GetItem(list, 0);
167 /* Py_INCREF(item); */
168 PyList_SetItem(list, 1, PyInt_FromLong(0L));
169 PyObject_Print(item, stdout, 0);
170 printf("\n");
171 /* Py_DECREF(item); */
173 Py_INCREF(Py_None);
174 return Py_None;
177 /* Test bad format character */
179 static PyObject *
180 xx_roj(PyObject *self, PyObject *args)
182 PyObject *a;
183 long b;
184 if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
185 return NULL;
186 Py_INCREF(Py_None);
187 return Py_None;
191 /* List of functions defined in the module */
193 static PyMethodDef xx_methods[] = {
194 {"roj", xx_roj, METH_VARARGS},
195 {"foo", xx_foo, METH_VARARGS},
196 {"new", xx_new, METH_VARARGS},
197 {"bug", xx_bug, METH_VARARGS},
198 {NULL, NULL} /* sentinel */
202 /* Initialization function for the module (*must* be called initxx) */
204 DL_EXPORT(void)
205 initxx(void)
207 PyObject *m, *d;
209 /* Initialize the type of the new type object here; doing it here
210 * is required for portability to Windows without requiring C++. */
211 Xxo_Type.ob_type = &PyType_Type;
213 /* Create the module and add the functions */
214 m = Py_InitModule("xx", xx_methods);
216 /* Add some symbolic constants to the module */
217 d = PyModule_GetDict(m);
218 ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
219 PyDict_SetItemString(d, "error", ErrorObject);