blob: c0d05f4fff4b5a1a10da266f2daea61f9de80ded [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum1984f1e1992-08-04 12:41:02 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum1984f1e1992-08-04 12:41:02 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum1984f1e1992-08-04 12:41:02 +000029
30******************************************************************/
31
Guido van Rossum14ed0b21994-09-29 09:50:09 +000032/* Use this file as a template to start implementing a module that
Guido van Rossum2b654441996-07-30 16:56:16 +000033 also declares objects types. All occurrences of 'Xxo' should be changed
Guido van Rossum14ed0b21994-09-29 09:50:09 +000034 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
37 foomodule.c.
38
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. */
44
45/* Xxo objects */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000046
Guido van Rossum2b654441996-07-30 16:56:16 +000047#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000048
Guido van Rossum2b654441996-07-30 16:56:16 +000049static PyObject *ErrorObject;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000050
51typedef struct {
Guido van Rossum2b654441996-07-30 16:56:16 +000052 PyObject_HEAD
53 PyObject *x_attr; /* Attributes dictionary */
54} XxoObject;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000055
Guido van Rossum2b654441996-07-30 16:56:16 +000056staticforward PyTypeObject Xxo_Type;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000057
Guido van Rossum2b654441996-07-30 16:56:16 +000058#define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
Guido van Rossum14ed0b21994-09-29 09:50:09 +000059
Guido van Rossum2b654441996-07-30 16:56:16 +000060static XxoObject *
61newXxoObject(arg)
62 PyObject *arg;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000063{
Guido van Rossum2b654441996-07-30 16:56:16 +000064 XxoObject *self;
65 self = PyObject_NEW(XxoObject, &Xxo_Type);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000066 if (self == NULL)
67 return NULL;
68 self->x_attr = NULL;
69 return self;
70}
71
72/* Xxo methods */
73
74static void
Guido van Rossum2b654441996-07-30 16:56:16 +000075Xxo_dealloc(self)
76 XxoObject *self;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000077{
Guido van Rossum2b654441996-07-30 16:56:16 +000078 Py_XDECREF(self->x_attr);
79 PyMem_DEL(self);
Guido van Rossum14ed0b21994-09-29 09:50:09 +000080}
81
Guido van Rossum2b654441996-07-30 16:56:16 +000082static PyObject *
83Xxo_demo(self, args)
84 XxoObject *self;
85 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000086{
Guido van Rossum43713e52000-02-29 13:59:29 +000087 if (!PyArg_ParseTuple(args, ":demo"))
Guido van Rossum14ed0b21994-09-29 09:50:09 +000088 return NULL;
Guido van Rossum2b654441996-07-30 16:56:16 +000089 Py_INCREF(Py_None);
90 return Py_None;
Guido van Rossum14ed0b21994-09-29 09:50:09 +000091}
92
Guido van Rossum2b654441996-07-30 16:56:16 +000093static PyMethodDef Xxo_methods[] = {
94 {"demo", (PyCFunction)Xxo_demo, 1},
Guido van Rossum14ed0b21994-09-29 09:50:09 +000095 {NULL, NULL} /* sentinel */
96};
97
Guido van Rossum2b654441996-07-30 16:56:16 +000098static PyObject *
99Xxo_getattr(self, name)
100 XxoObject *self;
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000101 char *name;
102{
103 if (self->x_attr != NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +0000104 PyObject *v = PyDict_GetItemString(self->x_attr, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000105 if (v != NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +0000106 Py_INCREF(v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000107 return v;
108 }
109 }
Guido van Rossum2b654441996-07-30 16:56:16 +0000110 return Py_FindMethod(Xxo_methods, (PyObject *)self, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000111}
112
113static int
Guido van Rossum2b654441996-07-30 16:56:16 +0000114Xxo_setattr(self, name, v)
115 XxoObject *self;
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000116 char *name;
Guido van Rossum2b654441996-07-30 16:56:16 +0000117 PyObject *v;
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000118{
119 if (self->x_attr == NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +0000120 self->x_attr = PyDict_New();
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000121 if (self->x_attr == NULL)
122 return -1;
123 }
124 if (v == NULL) {
Guido van Rossum2b654441996-07-30 16:56:16 +0000125 int rv = PyDict_DelItemString(self->x_attr, name);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000126 if (rv < 0)
Guido van Rossum2b654441996-07-30 16:56:16 +0000127 PyErr_SetString(PyExc_AttributeError,
128 "delete non-existing Xxo attribute");
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000129 return rv;
130 }
131 else
Guido van Rossum2b654441996-07-30 16:56:16 +0000132 return PyDict_SetItemString(self->x_attr, name, v);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000133}
134
Fred Drake67248351999-02-16 22:15:42 +0000135statichere 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)
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000139 0, /*ob_size*/
Guido van Rossum2b654441996-07-30 16:56:16 +0000140 "Xxo", /*tp_name*/
141 sizeof(XxoObject), /*tp_basicsize*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000142 0, /*tp_itemsize*/
143 /* methods */
Guido van Rossum2b654441996-07-30 16:56:16 +0000144 (destructor)Xxo_dealloc, /*tp_dealloc*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000145 0, /*tp_print*/
Guido van Rossum2b654441996-07-30 16:56:16 +0000146 (getattrfunc)Xxo_getattr, /*tp_getattr*/
147 (setattrfunc)Xxo_setattr, /*tp_setattr*/
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000148 0, /*tp_compare*/
149 0, /*tp_repr*/
150 0, /*tp_as_number*/
151 0, /*tp_as_sequence*/
152 0, /*tp_as_mapping*/
153 0, /*tp_hash*/
154};
155/* --------------------------------------------------------------------- */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000156
157/* Function of two integers returning integer */
158
Guido van Rossum2b654441996-07-30 16:56:16 +0000159static PyObject *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000160xx_foo(self, args)
Guido van Rossum2b654441996-07-30 16:56:16 +0000161 PyObject *self; /* Not used */
162 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000163{
164 long i, j;
165 long res;
Guido van Rossum43713e52000-02-29 13:59:29 +0000166 if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000167 return NULL;
168 res = i+j; /* XXX Do something here */
Guido van Rossum2b654441996-07-30 16:56:16 +0000169 return PyInt_FromLong(res);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000170}
171
172
Guido van Rossum2b654441996-07-30 16:56:16 +0000173/* Function of no arguments returning new Xxo object */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174
Guido van Rossum2b654441996-07-30 16:56:16 +0000175static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000176xx_new(self, args)
Guido van Rossum2b654441996-07-30 16:56:16 +0000177 PyObject *self; /* Not used */
178 PyObject *args;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000179{
Guido van Rossum2b654441996-07-30 16:56:16 +0000180 XxoObject *rv;
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000181
Guido van Rossum43713e52000-02-29 13:59:29 +0000182 if (!PyArg_ParseTuple(args, ":new"))
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000183 return NULL;
Guido van Rossum2b654441996-07-30 16:56:16 +0000184 rv = newXxoObject(args);
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000185 if ( rv == NULL )
186 return NULL;
Guido van Rossum2b654441996-07-30 16:56:16 +0000187 return (PyObject *)rv;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000188}
189
Guido van Rossumfbcfd521996-12-13 02:57:25 +0000190/* Example with subtle bug from extensions manual ("Thin Ice"). */
191
192static PyObject *
193xx_bug(self, args)
194 PyObject *self;
195 PyObject *args;
196{
197 PyObject *list, *item;
198
Guido van Rossum43713e52000-02-29 13:59:29 +0000199 if (!PyArg_ParseTuple(args, "O:bug", &list))
Guido van Rossumfbcfd521996-12-13 02:57:25 +0000200 return NULL;
201
202 item = PyList_GetItem(list, 0);
203 /* Py_INCREF(item); */
204 PyList_SetItem(list, 1, PyInt_FromLong(0L));
205 PyObject_Print(item, stdout, 0);
206 printf("\n");
207 /* Py_DECREF(item); */
208
209 Py_INCREF(Py_None);
210 return Py_None;
211}
212
Guido van Rossumc525e431997-12-09 20:37:25 +0000213/* Test bad format character */
214
215static PyObject *
216xx_roj(self, args)
217 PyObject *self; /* Not used */
218 PyObject *args;
219{
220 PyObject *a;
221 long b;
Guido van Rossum43713e52000-02-29 13:59:29 +0000222 if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
Guido van Rossumc525e431997-12-09 20:37:25 +0000223 return NULL;
224 Py_INCREF(Py_None);
225 return Py_None;
226}
227
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000228
229/* List of functions defined in the module */
230
Guido van Rossum2b654441996-07-30 16:56:16 +0000231static PyMethodDef xx_methods[] = {
Guido van Rossumc525e431997-12-09 20:37:25 +0000232 {"roj", xx_roj, 1},
Guido van Rossum2b654441996-07-30 16:56:16 +0000233 {"foo", xx_foo, 1},
234 {"new", xx_new, 1},
Guido van Rossumfbcfd521996-12-13 02:57:25 +0000235 {"bug", xx_bug, 1},
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000236 {NULL, NULL} /* sentinel */
237};
238
239
240/* Initialization function for the module (*must* be called initxx) */
241
Guido van Rossum3886bb61998-12-04 18:50:17 +0000242DL_EXPORT(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000243initxx()
244{
Guido van Rossum2b654441996-07-30 16:56:16 +0000245 PyObject *m, *d;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000246
Fred Drake67248351999-02-16 22:15:42 +0000247 /* 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;
250
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000251 /* Create the module and add the functions */
Guido van Rossum2b654441996-07-30 16:56:16 +0000252 m = Py_InitModule("xx", xx_methods);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000253
254 /* Add some symbolic constants to the module */
Guido van Rossum2b654441996-07-30 16:56:16 +0000255 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000256 ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
Guido van Rossum2b654441996-07-30 16:56:16 +0000257 PyDict_SetItemString(d, "error", ErrorObject);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000258}