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 statichere PyTypeObject Xxo_Type
= {
136 /* The ob_type field must be initialized in the module init function
137 * to be portable to Windows without using C++. */
138 PyObject_HEAD_INIT(NULL
)
141 sizeof(XxoObject
), /*tp_basicsize*/
144 (destructor
)Xxo_dealloc
, /*tp_dealloc*/
146 (getattrfunc
)Xxo_getattr
, /*tp_getattr*/
147 (setattrfunc
)Xxo_setattr
, /*tp_setattr*/
151 0, /*tp_as_sequence*/
155 /* --------------------------------------------------------------------- */
157 /* Function of two integers returning integer */
161 PyObject
*self
; /* Not used */
166 if (!PyArg_ParseTuple(args
, "ll", &i
, &j
))
168 res
= i
+j
; /* XXX Do something here */
169 return PyInt_FromLong(res
);
173 /* Function of no arguments returning new Xxo object */
177 PyObject
*self
; /* Not used */
182 if (!PyArg_ParseTuple(args
, ""))
184 rv
= newXxoObject(args
);
187 return (PyObject
*)rv
;
190 /* Example with subtle bug from extensions manual ("Thin Ice"). */
197 PyObject
*list
, *item
;
199 if (!PyArg_ParseTuple(args
, "O", &list
))
202 item
= PyList_GetItem(list
, 0);
203 /* Py_INCREF(item); */
204 PyList_SetItem(list
, 1, PyInt_FromLong(0L));
205 PyObject_Print(item
, stdout
, 0);
207 /* Py_DECREF(item); */
213 /* Test bad format character */
217 PyObject
*self
; /* Not used */
222 if (!PyArg_ParseTuple(args
, "O#", &a
, &b
))
229 /* List of functions defined in the module */
231 static PyMethodDef xx_methods
[] = {
236 {NULL
, NULL
} /* sentinel */
240 /* Initialization function for the module (*must* be called initxx) */
247 /* Initialize the type of the new type object here; doing it here
248 * is required for portability to Windows without requiring C++. */
249 Xxo_Type
.ob_type
= &PyType_Type
;
251 /* Create the module and add the functions */
252 m
= Py_InitModule("xx", xx_methods
);
254 /* Add some symbolic constants to the module */
255 d
= PyModule_GetDict(m
);
256 ErrorObject
= PyErr_NewException("xx.error", NULL
, NULL
);
257 PyDict_SetItemString(d
, "error", ErrorObject
);