openfile(): Go back to opening the files in text mode. This undoes
[python/dscho.git] / Modules / xxmodule.c
blob4b8822cfc5b20b6f6e3927949661ab524d8f8016
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 static 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 PyDoc_STR("demo() -> None")},
62 {NULL, NULL} /* sentinel */
65 static PyObject *
66 Xxo_getattr(XxoObject *self, char *name)
68 if (self->x_attr != NULL) {
69 PyObject *v = PyDict_GetItemString(self->x_attr, name);
70 if (v != NULL) {
71 Py_INCREF(v);
72 return v;
75 return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
78 static int
79 Xxo_setattr(XxoObject *self, char *name, PyObject *v)
81 if (self->x_attr == NULL) {
82 self->x_attr = PyDict_New();
83 if (self->x_attr == NULL)
84 return -1;
86 if (v == NULL) {
87 int rv = PyDict_DelItemString(self->x_attr, name);
88 if (rv < 0)
89 PyErr_SetString(PyExc_AttributeError,
90 "delete non-existing Xxo attribute");
91 return rv;
93 else
94 return PyDict_SetItemString(self->x_attr, name, v);
97 static PyTypeObject Xxo_Type = {
98 /* The ob_type field must be initialized in the module init function
99 * to be portable to Windows without using C++. */
100 PyObject_HEAD_INIT(NULL)
101 0, /*ob_size*/
102 "xxmodule.Xxo", /*tp_name*/
103 sizeof(XxoObject), /*tp_basicsize*/
104 0, /*tp_itemsize*/
105 /* methods */
106 (destructor)Xxo_dealloc, /*tp_dealloc*/
107 0, /*tp_print*/
108 (getattrfunc)Xxo_getattr, /*tp_getattr*/
109 (setattrfunc)Xxo_setattr, /*tp_setattr*/
110 0, /*tp_compare*/
111 0, /*tp_repr*/
112 0, /*tp_as_number*/
113 0, /*tp_as_sequence*/
114 0, /*tp_as_mapping*/
115 0, /*tp_hash*/
116 0, /*tp_call*/
117 0, /*tp_str*/
118 0, /*tp_getattro*/
119 0, /*tp_setattro*/
120 0, /*tp_as_buffer*/
121 Py_TPFLAGS_DEFAULT, /*tp_flags*/
122 0, /*tp_doc*/
123 0, /*tp_traverse*/
124 0, /*tp_clear*/
125 0, /*tp_richcompare*/
126 0, /*tp_weaklistoffset*/
127 0, /*tp_iter*/
128 0, /*tp_iternext*/
129 0, /*tp_methods*/
130 0, /*tp_members*/
131 0, /*tp_getset*/
132 0, /*tp_base*/
133 0, /*tp_dict*/
134 0, /*tp_descr_get*/
135 0, /*tp_descr_set*/
136 0, /*tp_dictoffset*/
137 0, /*tp_init*/
138 0, /*tp_alloc*/
139 0, /*tp_new*/
140 0, /*tp_free*/
141 0, /*tp_is_gc*/
143 /* --------------------------------------------------------------------- */
145 /* Function of two integers returning integer */
147 PyDoc_STRVAR(xx_foo_doc,
148 "foo(i,j)\n\
150 Return the sum of i and j.");
152 static PyObject *
153 xx_foo(PyObject *self, PyObject *args)
155 long i, j;
156 long res;
157 if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
158 return NULL;
159 res = i+j; /* XXX Do something here */
160 return PyInt_FromLong(res);
164 /* Function of no arguments returning new Xxo object */
166 static PyObject *
167 xx_new(PyObject *self, PyObject *args)
169 XxoObject *rv;
171 if (!PyArg_ParseTuple(args, ":new"))
172 return NULL;
173 rv = newXxoObject(args);
174 if (rv == NULL)
175 return NULL;
176 return (PyObject *)rv;
179 /* Example with subtle bug from extensions manual ("Thin Ice"). */
181 static PyObject *
182 xx_bug(PyObject *self, PyObject *args)
184 PyObject *list, *item;
186 if (!PyArg_ParseTuple(args, "O:bug", &list))
187 return NULL;
189 item = PyList_GetItem(list, 0);
190 /* Py_INCREF(item); */
191 PyList_SetItem(list, 1, PyInt_FromLong(0L));
192 PyObject_Print(item, stdout, 0);
193 printf("\n");
194 /* Py_DECREF(item); */
196 Py_INCREF(Py_None);
197 return Py_None;
200 /* Test bad format character */
202 static PyObject *
203 xx_roj(PyObject *self, PyObject *args)
205 PyObject *a;
206 long b;
207 if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
208 return NULL;
209 Py_INCREF(Py_None);
210 return Py_None;
214 /* List of functions defined in the module */
216 static PyMethodDef xx_methods[] = {
217 {"roj", xx_roj, METH_VARARGS,
218 PyDoc_STR("roj(a,b) -> None")},
219 {"foo", xx_foo, METH_VARARGS,
220 xx_foo_doc},
221 {"new", xx_new, METH_VARARGS,
222 PyDoc_STR("new() -> new Xx object")},
223 {"bug", xx_bug, METH_VARARGS,
224 PyDoc_STR("bug(o) -> None")},
225 {NULL, NULL} /* sentinel */
228 PyDoc_STRVAR(module_doc,
229 "This is a template module just for instruction.");
231 /* Initialization function for the module (*must* be called initxx) */
233 PyMODINIT_FUNC
234 initxx(void)
236 PyObject *m;
238 /* Initialize the type of the new type object here; doing it here
239 * is required for portability to Windows without requiring C++. */
240 Xxo_Type.ob_type = &PyType_Type;
242 /* Create the module and add the functions */
243 m = Py_InitModule3("xx", xx_methods, module_doc);
245 /* Add some symbolic constants to the module */
246 if (ErrorObject == NULL) {
247 ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
248 if (ErrorObject == NULL)
249 return;
251 Py_INCREF(ErrorObject);
252 PyModule_AddObject(m, "error", ErrorObject);