Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 11 | /* 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] | 12 | also declares objects types. All occurrences of 'Xxo' should be changed |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 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 |
| 16 | foomodule.c. |
| 17 | |
| 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. */ |
| 23 | |
| 24 | /* Xxo objects */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 26 | #include "Python.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 28 | static PyObject *ErrorObject; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 29 | |
| 30 | typedef struct { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 31 | PyObject_HEAD |
| 32 | PyObject *x_attr; /* Attributes dictionary */ |
| 33 | } XxoObject; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 35 | staticforward PyTypeObject Xxo_Type; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 37 | #define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 39 | static XxoObject * |
| 40 | newXxoObject(arg) |
| 41 | PyObject *arg; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 42 | { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 43 | XxoObject *self; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 44 | self = PyObject_New(XxoObject, &Xxo_Type); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 45 | if (self == NULL) |
| 46 | return NULL; |
| 47 | self->x_attr = NULL; |
| 48 | return self; |
| 49 | } |
| 50 | |
| 51 | /* Xxo methods */ |
| 52 | |
| 53 | static void |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 54 | Xxo_dealloc(self) |
| 55 | XxoObject *self; |
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 | Py_XDECREF(self->x_attr); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 58 | PyObject_Del(self); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 61 | static PyObject * |
| 62 | Xxo_demo(self, args) |
| 63 | XxoObject *self; |
| 64 | PyObject *args; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 65 | { |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 66 | if (!PyArg_ParseTuple(args, ":demo")) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 67 | return NULL; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 68 | Py_INCREF(Py_None); |
| 69 | return Py_None; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 72 | static PyMethodDef Xxo_methods[] = { |
| 73 | {"demo", (PyCFunction)Xxo_demo, 1}, |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 74 | {NULL, NULL} /* sentinel */ |
| 75 | }; |
| 76 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 77 | static PyObject * |
| 78 | Xxo_getattr(self, name) |
| 79 | XxoObject *self; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 80 | char *name; |
| 81 | { |
| 82 | if (self->x_attr != NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 83 | PyObject *v = PyDict_GetItemString(self->x_attr, name); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 84 | if (v != NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 85 | Py_INCREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 86 | return v; |
| 87 | } |
| 88 | } |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 89 | return Py_FindMethod(Xxo_methods, (PyObject *)self, name); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static int |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 93 | Xxo_setattr(self, name, v) |
| 94 | XxoObject *self; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 95 | char *name; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 96 | PyObject *v; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 97 | { |
| 98 | if (self->x_attr == NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 99 | self->x_attr = PyDict_New(); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 100 | if (self->x_attr == NULL) |
| 101 | return -1; |
| 102 | } |
| 103 | if (v == NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 104 | int rv = PyDict_DelItemString(self->x_attr, name); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 105 | if (rv < 0) |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 106 | PyErr_SetString(PyExc_AttributeError, |
| 107 | "delete non-existing Xxo attribute"); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 108 | return rv; |
| 109 | } |
| 110 | else |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 111 | return PyDict_SetItemString(self->x_attr, name, v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Fred Drake | 6724835 | 1999-02-16 22:15:42 +0000 | [diff] [blame] | 114 | statichere PyTypeObject Xxo_Type = { |
| 115 | /* The ob_type field must be initialized in the module init function |
| 116 | * to be portable to Windows without using C++. */ |
| 117 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 118 | 0, /*ob_size*/ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 119 | "Xxo", /*tp_name*/ |
| 120 | sizeof(XxoObject), /*tp_basicsize*/ |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 121 | 0, /*tp_itemsize*/ |
| 122 | /* methods */ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 123 | (destructor)Xxo_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 124 | 0, /*tp_print*/ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 125 | (getattrfunc)Xxo_getattr, /*tp_getattr*/ |
| 126 | (setattrfunc)Xxo_setattr, /*tp_setattr*/ |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 127 | 0, /*tp_compare*/ |
| 128 | 0, /*tp_repr*/ |
| 129 | 0, /*tp_as_number*/ |
| 130 | 0, /*tp_as_sequence*/ |
| 131 | 0, /*tp_as_mapping*/ |
| 132 | 0, /*tp_hash*/ |
| 133 | }; |
| 134 | /* --------------------------------------------------------------------- */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 135 | |
| 136 | /* Function of two integers returning integer */ |
| 137 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 138 | static PyObject * |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 139 | xx_foo(self, args) |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 140 | PyObject *self; /* Not used */ |
| 141 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 142 | { |
| 143 | long i, j; |
| 144 | long res; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 145 | if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 146 | return NULL; |
| 147 | res = i+j; /* XXX Do something here */ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 148 | return PyInt_FromLong(res); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 152 | /* Function of no arguments returning new Xxo object */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 154 | static PyObject * |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 155 | xx_new(self, args) |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 156 | PyObject *self; /* Not used */ |
| 157 | PyObject *args; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 158 | { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 159 | XxoObject *rv; |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 161 | if (!PyArg_ParseTuple(args, ":new")) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 162 | return NULL; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 163 | rv = newXxoObject(args); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 164 | if ( rv == NULL ) |
| 165 | return NULL; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 166 | return (PyObject *)rv; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Guido van Rossum | fbcfd52 | 1996-12-13 02:57:25 +0000 | [diff] [blame] | 169 | /* Example with subtle bug from extensions manual ("Thin Ice"). */ |
| 170 | |
| 171 | static PyObject * |
| 172 | xx_bug(self, args) |
| 173 | PyObject *self; |
| 174 | PyObject *args; |
| 175 | { |
| 176 | PyObject *list, *item; |
| 177 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 178 | if (!PyArg_ParseTuple(args, "O:bug", &list)) |
Guido van Rossum | fbcfd52 | 1996-12-13 02:57:25 +0000 | [diff] [blame] | 179 | return NULL; |
| 180 | |
| 181 | item = PyList_GetItem(list, 0); |
| 182 | /* Py_INCREF(item); */ |
| 183 | PyList_SetItem(list, 1, PyInt_FromLong(0L)); |
| 184 | PyObject_Print(item, stdout, 0); |
| 185 | printf("\n"); |
| 186 | /* Py_DECREF(item); */ |
| 187 | |
| 188 | Py_INCREF(Py_None); |
| 189 | return Py_None; |
| 190 | } |
| 191 | |
Guido van Rossum | c525e43 | 1997-12-09 20:37:25 +0000 | [diff] [blame] | 192 | /* Test bad format character */ |
| 193 | |
| 194 | static PyObject * |
| 195 | xx_roj(self, args) |
| 196 | PyObject *self; /* Not used */ |
| 197 | PyObject *args; |
| 198 | { |
| 199 | PyObject *a; |
| 200 | long b; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 201 | if (!PyArg_ParseTuple(args, "O#:roj", &a, &b)) |
Guido van Rossum | c525e43 | 1997-12-09 20:37:25 +0000 | [diff] [blame] | 202 | return NULL; |
| 203 | Py_INCREF(Py_None); |
| 204 | return Py_None; |
| 205 | } |
| 206 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 207 | |
| 208 | /* List of functions defined in the module */ |
| 209 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 210 | static PyMethodDef xx_methods[] = { |
Guido van Rossum | c525e43 | 1997-12-09 20:37:25 +0000 | [diff] [blame] | 211 | {"roj", xx_roj, 1}, |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 212 | {"foo", xx_foo, 1}, |
| 213 | {"new", xx_new, 1}, |
Guido van Rossum | fbcfd52 | 1996-12-13 02:57:25 +0000 | [diff] [blame] | 214 | {"bug", xx_bug, 1}, |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 215 | {NULL, NULL} /* sentinel */ |
| 216 | }; |
| 217 | |
| 218 | |
| 219 | /* Initialization function for the module (*must* be called initxx) */ |
| 220 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 221 | DL_EXPORT(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 222 | initxx() |
| 223 | { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 224 | PyObject *m, *d; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 225 | |
Fred Drake | 6724835 | 1999-02-16 22:15:42 +0000 | [diff] [blame] | 226 | /* Initialize the type of the new type object here; doing it here |
| 227 | * is required for portability to Windows without requiring C++. */ |
| 228 | Xxo_Type.ob_type = &PyType_Type; |
| 229 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 230 | /* Create the module and add the functions */ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 231 | m = Py_InitModule("xx", xx_methods); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 232 | |
| 233 | /* Add some symbolic constants to the module */ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 234 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 235 | ErrorObject = PyErr_NewException("xx.error", NULL, NULL); |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 236 | PyDict_SetItemString(d, "error", ErrorObject); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 237 | } |