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.
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
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. */
28 static PyObject
*ErrorObject
;
32 PyObject
*x_attr
; /* Attributes dictionary */
35 staticforward PyTypeObject Xxo_Type
;
37 #define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
40 newXxoObject(PyObject
*arg
)
43 self
= PyObject_New(XxoObject
, &Xxo_Type
);
53 Xxo_dealloc(XxoObject
*self
)
55 Py_XDECREF(self
->x_attr
);
60 Xxo_demo(XxoObject
*self
, PyObject
*args
)
62 if (!PyArg_ParseTuple(args
, ":demo"))
68 static PyMethodDef Xxo_methods
[] = {
69 {"demo", (PyCFunction
)Xxo_demo
, METH_VARARGS
},
70 {NULL
, NULL
} /* sentinel */
74 Xxo_getattr(XxoObject
*self
, char *name
)
76 if (self
->x_attr
!= NULL
) {
77 PyObject
*v
= PyDict_GetItemString(self
->x_attr
, name
);
83 return Py_FindMethod(Xxo_methods
, (PyObject
*)self
, name
);
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
)
95 int rv
= PyDict_DelItemString(self
->x_attr
, name
);
97 PyErr_SetString(PyExc_AttributeError
,
98 "delete non-existing Xxo attribute");
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
)
111 sizeof(XxoObject
), /*tp_basicsize*/
114 (destructor
)Xxo_dealloc
, /*tp_dealloc*/
116 (getattrfunc
)Xxo_getattr
, /*tp_getattr*/
117 (setattrfunc
)Xxo_setattr
, /*tp_setattr*/
121 0, /*tp_as_sequence*/
125 /* --------------------------------------------------------------------- */
127 /* Function of two integers returning integer */
130 xx_foo(PyObject
*self
, PyObject
*args
)
134 if (!PyArg_ParseTuple(args
, "ll:foo", &i
, &j
))
136 res
= i
+j
; /* XXX Do something here */
137 return PyInt_FromLong(res
);
141 /* Function of no arguments returning new Xxo object */
144 xx_new(PyObject
*self
, PyObject
*args
)
148 if (!PyArg_ParseTuple(args
, ":new"))
150 rv
= newXxoObject(args
);
153 return (PyObject
*)rv
;
156 /* Example with subtle bug from extensions manual ("Thin Ice"). */
159 xx_bug(PyObject
*self
, PyObject
*args
)
161 PyObject
*list
, *item
;
163 if (!PyArg_ParseTuple(args
, "O:bug", &list
))
166 item
= PyList_GetItem(list
, 0);
167 /* Py_INCREF(item); */
168 PyList_SetItem(list
, 1, PyInt_FromLong(0L));
169 PyObject_Print(item
, stdout
, 0);
171 /* Py_DECREF(item); */
177 /* Test bad format character */
180 xx_roj(PyObject
*self
, PyObject
*args
)
184 if (!PyArg_ParseTuple(args
, "O#:roj", &a
, &b
))
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) */
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
);