1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Use this file as a template to start implementing a module that
33 also declares objects types. All occurrences of 'Xxo' should be changed
34 to something reasonable for your objects. After that, all other
35 occurrences of 'xx' should be changed to something reasonable for your
36 module. If your module is named foo your sourcefile should be named
39 You will probably want to delete all references to 'x_attr' and add
40 your own types of attributes instead. Maybe you want to name your
41 local variables other than 'self'. If your object type is needed in
42 other files, you'll have to create a file "foobarobject.h"; see
43 intobject.h for an example. */
49 static PyObject
*ErrorObject
;
53 PyObject
*x_attr
; /* Attributes dictionary */
56 staticforward PyTypeObject Xxo_Type
;
58 #define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
65 self
= PyObject_NEW(XxoObject
, &Xxo_Type
);
78 Py_XDECREF(self
->x_attr
);
87 if (!PyArg_ParseTuple(args
, ""))
93 static PyMethodDef Xxo_methods
[] = {
94 {"demo", (PyCFunction
)Xxo_demo
, 1},
95 {NULL
, NULL
} /* sentinel */
99 Xxo_getattr(self
, name
)
103 if (self
->x_attr
!= NULL
) {
104 PyObject
*v
= PyDict_GetItemString(self
->x_attr
, name
);
110 return Py_FindMethod(Xxo_methods
, (PyObject
*)self
, name
);
114 Xxo_setattr(self
, name
, v
)
119 if (self
->x_attr
== NULL
) {
120 self
->x_attr
= PyDict_New();
121 if (self
->x_attr
== NULL
)
125 int rv
= PyDict_DelItemString(self
->x_attr
, name
);
127 PyErr_SetString(PyExc_AttributeError
,
128 "delete non-existing Xxo attribute");
132 return PyDict_SetItemString(self
->x_attr
, name
, v
);
135 staticforward PyTypeObject Xxo_Type
= {
136 PyObject_HEAD_INIT(&PyType_Type
)
139 sizeof(XxoObject
), /*tp_basicsize*/
142 (destructor
)Xxo_dealloc
, /*tp_dealloc*/
144 (getattrfunc
)Xxo_getattr
, /*tp_getattr*/
145 (setattrfunc
)Xxo_setattr
, /*tp_setattr*/
149 0, /*tp_as_sequence*/
153 /* --------------------------------------------------------------------- */
155 /* Function of two integers returning integer */
159 PyObject
*self
; /* Not used */
164 if (!PyArg_ParseTuple(args
, "ll", &i
, &j
))
166 res
= i
+j
; /* XXX Do something here */
167 return PyInt_FromLong(res
);
171 /* Function of no arguments returning new Xxo object */
175 PyObject
*self
; /* Not used */
180 if (!PyArg_ParseTuple(args
, ""))
182 rv
= newXxoObject(args
);
185 return (PyObject
*)rv
;
188 /* Example with subtle bug from extensions manual ("Thin Ice"). */
195 PyObject
*list
, *item
;
197 if (!PyArg_ParseTuple(args
, "O", &list
))
200 item
= PyList_GetItem(list
, 0);
201 /* Py_INCREF(item); */
202 PyList_SetItem(list
, 1, PyInt_FromLong(0L));
203 PyObject_Print(item
, stdout
, 0);
205 /* Py_DECREF(item); */
211 /* Test bad format character */
215 PyObject
*self
; /* Not used */
220 if (!PyArg_ParseTuple(args
, "O#", &a
, &b
))
227 /* List of functions defined in the module */
229 static PyMethodDef xx_methods
[] = {
234 {NULL
, NULL
} /* sentinel */
238 /* Initialization function for the module (*must* be called initxx) */
245 /* Create the module and add the functions */
246 m
= Py_InitModule("xx", xx_methods
);
248 /* Add some symbolic constants to the module */
249 d
= PyModule_GetDict(m
);
250 ErrorObject
= PyErr_NewException("xx.error", NULL
, NULL
);
251 PyDict_SetItemString(d
, "error", ErrorObject
);