Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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 not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 25 | /* Use this file as a template to start implementing a module that |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 26 | also declares objects types. All occurrences of 'Xxo' should be changed |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 27 | to something reasonable for your objects. After that, all other |
| 28 | occurrences of 'xx' should be changed to something reasonable for your |
| 29 | module. If your module is named foo your sourcefile should be named |
| 30 | foomodule.c. |
| 31 | |
| 32 | You will probably want to delete all references to 'x_attr' and add |
| 33 | your own types of attributes instead. Maybe you want to name your |
| 34 | local variables other than 'self'. If your object type is needed in |
| 35 | other files, you'll have to create a file "foobarobject.h"; see |
| 36 | intobject.h for an example. */ |
| 37 | |
| 38 | /* Xxo objects */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 40 | #include "Python.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 42 | static PyObject *ErrorObject; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 43 | |
| 44 | typedef struct { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 45 | PyObject_HEAD |
| 46 | PyObject *x_attr; /* Attributes dictionary */ |
| 47 | } XxoObject; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 49 | staticforward PyTypeObject Xxo_Type; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 51 | #define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 53 | static XxoObject * |
| 54 | newXxoObject(arg) |
| 55 | PyObject *arg; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 56 | { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 57 | XxoObject *self; |
| 58 | self = PyObject_NEW(XxoObject, &Xxo_Type); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 59 | if (self == NULL) |
| 60 | return NULL; |
| 61 | self->x_attr = NULL; |
| 62 | return self; |
| 63 | } |
| 64 | |
| 65 | /* Xxo methods */ |
| 66 | |
| 67 | static void |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 68 | Xxo_dealloc(self) |
| 69 | XxoObject *self; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 70 | { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 71 | Py_XDECREF(self->x_attr); |
| 72 | PyMem_DEL(self); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 75 | static PyObject * |
| 76 | Xxo_demo(self, args) |
| 77 | XxoObject *self; |
| 78 | PyObject *args; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 79 | { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 80 | if (!PyArg_ParseTuple(args, "")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 81 | return NULL; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 82 | Py_INCREF(Py_None); |
| 83 | return Py_None; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 86 | static PyMethodDef Xxo_methods[] = { |
| 87 | {"demo", (PyCFunction)Xxo_demo, 1}, |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 88 | {NULL, NULL} /* sentinel */ |
| 89 | }; |
| 90 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 91 | static PyObject * |
| 92 | Xxo_getattr(self, name) |
| 93 | XxoObject *self; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 94 | char *name; |
| 95 | { |
| 96 | if (self->x_attr != NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 97 | PyObject *v = PyDict_GetItemString(self->x_attr, name); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 98 | if (v != NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 99 | Py_INCREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 100 | return v; |
| 101 | } |
| 102 | } |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 103 | return Py_FindMethod(Xxo_methods, (PyObject *)self, name); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static int |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 107 | Xxo_setattr(self, name, v) |
| 108 | XxoObject *self; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 109 | char *name; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 110 | PyObject *v; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 111 | { |
| 112 | if (self->x_attr == NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 113 | self->x_attr = PyDict_New(); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 114 | if (self->x_attr == NULL) |
| 115 | return -1; |
| 116 | } |
| 117 | if (v == NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 118 | int rv = PyDict_DelItemString(self->x_attr, name); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 119 | if (rv < 0) |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 120 | PyErr_SetString(PyExc_AttributeError, |
| 121 | "delete non-existing Xxo attribute"); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 122 | return rv; |
| 123 | } |
| 124 | else |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 125 | return PyDict_SetItemString(self->x_attr, name, v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 128 | staticforward PyTypeObject Xxo_Type = { |
| 129 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 130 | 0, /*ob_size*/ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 131 | "Xxo", /*tp_name*/ |
| 132 | sizeof(XxoObject), /*tp_basicsize*/ |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 133 | 0, /*tp_itemsize*/ |
| 134 | /* methods */ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 135 | (destructor)Xxo_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 136 | 0, /*tp_print*/ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 137 | (getattrfunc)Xxo_getattr, /*tp_getattr*/ |
| 138 | (setattrfunc)Xxo_setattr, /*tp_setattr*/ |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 139 | 0, /*tp_compare*/ |
| 140 | 0, /*tp_repr*/ |
| 141 | 0, /*tp_as_number*/ |
| 142 | 0, /*tp_as_sequence*/ |
| 143 | 0, /*tp_as_mapping*/ |
| 144 | 0, /*tp_hash*/ |
| 145 | }; |
| 146 | /* --------------------------------------------------------------------- */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 147 | |
| 148 | /* Function of two integers returning integer */ |
| 149 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 150 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 151 | xx_foo(self, args) |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 152 | PyObject *self; /* Not used */ |
| 153 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 154 | { |
| 155 | long i, j; |
| 156 | long res; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 157 | if (!PyArg_ParseTuple(args, "ll", &i, &j)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 158 | return NULL; |
| 159 | res = i+j; /* XXX Do something here */ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 160 | return PyInt_FromLong(res); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 164 | /* Function of no arguments returning new Xxo object */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 166 | static PyObject * |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 167 | xx_new(self, args) |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 168 | PyObject *self; /* Not used */ |
| 169 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 170 | { |
| 171 | int i, j; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 172 | XxoObject *rv; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 173 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 174 | if (!PyArg_ParseTuple(args, "")) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 175 | return NULL; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 176 | rv = newXxoObject(args); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 177 | if ( rv == NULL ) |
| 178 | return NULL; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 179 | return (PyObject *)rv; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
| 183 | /* List of functions defined in the module */ |
| 184 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 185 | static PyMethodDef xx_methods[] = { |
| 186 | {"foo", xx_foo, 1}, |
| 187 | {"new", xx_new, 1}, |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 188 | {NULL, NULL} /* sentinel */ |
| 189 | }; |
| 190 | |
| 191 | |
| 192 | /* Initialization function for the module (*must* be called initxx) */ |
| 193 | |
| 194 | void |
| 195 | initxx() |
| 196 | { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 197 | PyObject *m, *d; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 198 | |
| 199 | /* Create the module and add the functions */ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 200 | m = Py_InitModule("xx", xx_methods); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 201 | |
| 202 | /* Add some symbolic constants to the module */ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 203 | d = PyModule_GetDict(m); |
| 204 | ErrorObject = PyString_FromString("xx.error"); |
| 205 | PyDict_SetItemString(d, "error", ErrorObject); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 206 | |
| 207 | /* Check for errors */ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame^] | 208 | if (PyErr_Occurred()) |
| 209 | Py_FatalError("can't initialize module xx"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 210 | } |