- Got rid of newmodule.c
[python/dscho.git] / Modules / xxmodule.c
blobb170651abb720e85d364621c4ce5e5a940e07aa2
2 /* Use this file as a template to start implementing a module that
3 also declares object types. All occurrences of 'Xxo' should be changed
4 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.
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. */
15 /* Xxo objects */
17 #include "Python.h"
19 static PyObject *ErrorObject;
21 typedef struct {
22 PyObject_HEAD
23 PyObject *x_attr; /* Attributes dictionary */
24 } XxoObject;
26 staticforward PyTypeObject Xxo_Type;
28 #define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
30 static XxoObject *
31 newXxoObject(PyObject *arg)
33 XxoObject *self;
34 self = PyObject_New(XxoObject, &Xxo_Type);
35 if (self == NULL)
36 return NULL;
37 self->x_attr = NULL;
38 return self;
41 /* Xxo methods */
43 static void
44 Xxo_dealloc(XxoObject *self)
46 Py_XDECREF(self->x_attr);
47 PyObject_Del(self);
50 static PyObject *
51 Xxo_demo(XxoObject *self, PyObject *args)
53 if (!PyArg_ParseTuple(args, ":demo"))
54 return NULL;
55 Py_INCREF(Py_None);
56 return Py_None;
59 static PyMethodDef Xxo_methods[] = {
60 {"demo", (PyCFunction)Xxo_demo, METH_VARARGS},
61 {NULL, NULL} /* sentinel */
64 static PyObject *
65 Xxo_getattr(XxoObject *self, char *name)
67 if (self->x_attr != NULL) {
68 PyObject *v = PyDict_GetItemString(self->x_attr, name);
69 if (v != NULL) {
70 Py_INCREF(v);
71 return v;
74 return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
77 static int
78 Xxo_setattr(XxoObject *self, char *name, PyObject *v)
80 if (self->x_attr == NULL) {
81 self->x_attr = PyDict_New();
82 if (self->x_attr == NULL)
83 return -1;
85 if (v == NULL) {
86 int rv = PyDict_DelItemString(self->x_attr, name);
87 if (rv < 0)
88 PyErr_SetString(PyExc_AttributeError,
89 "delete non-existing Xxo attribute");
90 return rv;
92 else
93 return PyDict_SetItemString(self->x_attr, name, v);
96 statichere 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)
100 0, /*ob_size*/
101 "xxmodule.Xxo", /*tp_name*/
102 sizeof(XxoObject), /*tp_basicsize*/
103 0, /*tp_itemsize*/
104 /* methods */
105 (destructor)Xxo_dealloc, /*tp_dealloc*/
106 0, /*tp_print*/
107 (getattrfunc)Xxo_getattr, /*tp_getattr*/
108 (setattrfunc)Xxo_setattr, /*tp_setattr*/
109 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*/
115 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*/
142 /* --------------------------------------------------------------------- */
144 /* Function of two integers returning integer */
146 static PyObject *
147 xx_foo(PyObject *self, PyObject *args)
149 long i, j;
150 long res;
151 if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
152 return NULL;
153 res = i+j; /* XXX Do something here */
154 return PyInt_FromLong(res);
158 /* Function of no arguments returning new Xxo object */
160 static PyObject *
161 xx_new(PyObject *self, PyObject *args)
163 XxoObject *rv;
165 if (!PyArg_ParseTuple(args, ":new"))
166 return NULL;
167 rv = newXxoObject(args);
168 if (rv == NULL)
169 return NULL;
170 return (PyObject *)rv;
173 /* Example with subtle bug from extensions manual ("Thin Ice"). */
175 static PyObject *
176 xx_bug(PyObject *self, PyObject *args)
178 PyObject *list, *item;
180 if (!PyArg_ParseTuple(args, "O:bug", &list))
181 return NULL;
183 item = PyList_GetItem(list, 0);
184 /* Py_INCREF(item); */
185 PyList_SetItem(list, 1, PyInt_FromLong(0L));
186 PyObject_Print(item, stdout, 0);
187 printf("\n");
188 /* Py_DECREF(item); */
190 Py_INCREF(Py_None);
191 return Py_None;
194 /* Test bad format character */
196 static PyObject *
197 xx_roj(PyObject *self, PyObject *args)
199 PyObject *a;
200 long b;
201 if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
202 return NULL;
203 Py_INCREF(Py_None);
204 return Py_None;
208 /* List of functions defined in the module */
210 static PyMethodDef xx_methods[] = {
211 {"roj", xx_roj, METH_VARARGS},
212 {"foo", xx_foo, METH_VARARGS},
213 {"new", xx_new, METH_VARARGS},
214 {"bug", xx_bug, METH_VARARGS},
215 {NULL, NULL} /* sentinel */
219 /* Initialization function for the module (*must* be called initxx) */
221 DL_EXPORT(void)
222 initxx(void)
224 PyObject *m;
226 /* Initialize the type of the new type object here; doing it here
227 * is required for portability to Windows without requiring C++. */
228 Xxo_Type.ob_type = &PyType_Type;
230 /* Create the module and add the functions */
231 m = Py_InitModule("xx", xx_methods);
233 /* Add some symbolic constants to the module */
234 if (ErrorObject == NULL) {
235 ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
236 if (ErrorObject == NULL)
237 return;
239 Py_INCREF(ErrorObject);
240 PyModule_AddObject(m, "error", ErrorObject);