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