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
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. */
19 static PyObject
*ErrorObject
;
23 PyObject
*x_attr
; /* Attributes dictionary */
26 static PyTypeObject Xxo_Type
;
28 #define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
31 newXxoObject(PyObject
*arg
)
34 self
= PyObject_New(XxoObject
, &Xxo_Type
);
44 Xxo_dealloc(XxoObject
*self
)
46 Py_XDECREF(self
->x_attr
);
51 Xxo_demo(XxoObject
*self
, PyObject
*args
)
53 if (!PyArg_ParseTuple(args
, ":demo"))
59 static PyMethodDef Xxo_methods
[] = {
60 {"demo", (PyCFunction
)Xxo_demo
, METH_VARARGS
,
61 PyDoc_STR("demo() -> None")},
62 {NULL
, NULL
} /* sentinel */
66 Xxo_getattr(XxoObject
*self
, char *name
)
68 if (self
->x_attr
!= NULL
) {
69 PyObject
*v
= PyDict_GetItemString(self
->x_attr
, name
);
75 return Py_FindMethod(Xxo_methods
, (PyObject
*)self
, name
);
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
)
87 int rv
= PyDict_DelItemString(self
->x_attr
, name
);
89 PyErr_SetString(PyExc_AttributeError
,
90 "delete non-existing Xxo attribute");
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
)
102 "xxmodule.Xxo", /*tp_name*/
103 sizeof(XxoObject
), /*tp_basicsize*/
106 (destructor
)Xxo_dealloc
, /*tp_dealloc*/
108 (getattrfunc
)Xxo_getattr
, /*tp_getattr*/
109 (setattrfunc
)Xxo_setattr
, /*tp_setattr*/
113 0, /*tp_as_sequence*/
121 Py_TPFLAGS_DEFAULT
, /*tp_flags*/
125 0, /*tp_richcompare*/
126 0, /*tp_weaklistoffset*/
143 /* --------------------------------------------------------------------- */
145 /* Function of two integers returning integer */
147 PyDoc_STRVAR(xx_foo_doc
,
150 Return the sum of i and j.");
153 xx_foo(PyObject
*self
, PyObject
*args
)
157 if (!PyArg_ParseTuple(args
, "ll:foo", &i
, &j
))
159 res
= i
+j
; /* XXX Do something here */
160 return PyInt_FromLong(res
);
164 /* Function of no arguments returning new Xxo object */
167 xx_new(PyObject
*self
, PyObject
*args
)
171 if (!PyArg_ParseTuple(args
, ":new"))
173 rv
= newXxoObject(args
);
176 return (PyObject
*)rv
;
179 /* Example with subtle bug from extensions manual ("Thin Ice"). */
182 xx_bug(PyObject
*self
, PyObject
*args
)
184 PyObject
*list
, *item
;
186 if (!PyArg_ParseTuple(args
, "O:bug", &list
))
189 item
= PyList_GetItem(list
, 0);
190 /* Py_INCREF(item); */
191 PyList_SetItem(list
, 1, PyInt_FromLong(0L));
192 PyObject_Print(item
, stdout
, 0);
194 /* Py_DECREF(item); */
200 /* Test bad format character */
203 xx_roj(PyObject
*self
, PyObject
*args
)
207 if (!PyArg_ParseTuple(args
, "O#:roj", &a
, &b
))
216 static PyTypeObject Str_Type
= {
217 /* The ob_type field must be initialized in the module init function
218 * to be portable to Windows without using C++. */
219 PyObject_HEAD_INIT(NULL
)
221 "xxmodule.Str", /*tp_name*/
232 0, /*tp_as_sequence*/
240 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
244 0, /*tp_richcompare*/
245 0, /*tp_weaklistoffset*/
251 &PyString_Type
, /*tp_base*/
266 null_richcompare(PyObject
*self
, PyObject
*other
, int op
)
268 Py_INCREF(Py_NotImplemented
);
269 return Py_NotImplemented
;
272 static PyTypeObject Null_Type
= {
273 /* The ob_type field must be initialized in the module init function
274 * to be portable to Windows without using C++. */
275 PyObject_HEAD_INIT(NULL
)
277 "xxmodule.Null", /*tp_name*/
288 0, /*tp_as_sequence*/
296 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
300 null_richcompare
, /*tp_richcompare*/
301 0, /*tp_weaklistoffset*/
307 &PyBaseObject_Type
, /*tp_base*/
314 PyType_GenericNew
, /*tp_new*/
323 /* List of functions defined in the module */
325 static PyMethodDef xx_methods
[] = {
326 {"roj", xx_roj
, METH_VARARGS
,
327 PyDoc_STR("roj(a,b) -> None")},
328 {"foo", xx_foo
, METH_VARARGS
,
330 {"new", xx_new
, METH_VARARGS
,
331 PyDoc_STR("new() -> new Xx object")},
332 {"bug", xx_bug
, METH_VARARGS
,
333 PyDoc_STR("bug(o) -> None")},
334 {NULL
, NULL
} /* sentinel */
337 PyDoc_STRVAR(module_doc
,
338 "This is a template module just for instruction.");
340 /* Initialization function for the module (*must* be called initxx) */
347 /* Finalize the type object including setting type of the new type
348 * object; doing it here is required for portability to Windows
349 * without requiring C++. */
350 if (PyType_Ready(&Xxo_Type
) < 0)
353 /* Create the module and add the functions */
354 m
= Py_InitModule3("xx", xx_methods
, module_doc
);
358 /* Add some symbolic constants to the module */
359 if (ErrorObject
== NULL
) {
360 ErrorObject
= PyErr_NewException("xx.error", NULL
, NULL
);
361 if (ErrorObject
== NULL
)
364 Py_INCREF(ErrorObject
);
365 PyModule_AddObject(m
, "error", ErrorObject
);
368 if (PyType_Ready(&Str_Type
) < 0)
370 PyModule_AddObject(m
, "Str", (PyObject
*)&Str_Type
);
373 if (PyType_Ready(&Null_Type
) < 0)
375 PyModule_AddObject(m
, "Null", (PyObject
*)&Null_Type
);