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. |
Tim Peters | c9ca5c8 | 2002-05-23 15:49:38 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 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 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 26 | static 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[] = { |
Skip Montanaro | d9e7d24 | 2002-08-14 01:44:33 +0000 | [diff] [blame] | 60 | {"demo", (PyCFunction)Xxo_demo, METH_VARARGS, |
| 61 | PyDoc_STR("demo() -> None")}, |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 62 | {NULL, NULL} /* sentinel */ |
| 63 | }; |
| 64 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 65 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 66 | Xxo_getattr(XxoObject *self, char *name) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 67 | { |
| 68 | if (self->x_attr != NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 69 | PyObject *v = PyDict_GetItemString(self->x_attr, name); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 70 | if (v != NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 71 | Py_INCREF(v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 72 | return v; |
| 73 | } |
| 74 | } |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 75 | return Py_FindMethod(Xxo_methods, (PyObject *)self, name); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static int |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 79 | Xxo_setattr(XxoObject *self, char *name, PyObject *v) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 80 | { |
| 81 | if (self->x_attr == NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 82 | self->x_attr = PyDict_New(); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 83 | if (self->x_attr == NULL) |
| 84 | return -1; |
| 85 | } |
| 86 | if (v == NULL) { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 87 | int rv = PyDict_DelItemString(self->x_attr, name); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 88 | if (rv < 0) |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 89 | PyErr_SetString(PyExc_AttributeError, |
| 90 | "delete non-existing Xxo attribute"); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 91 | return rv; |
| 92 | } |
| 93 | else |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 94 | return PyDict_SetItemString(self->x_attr, name, v); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Tim Peters | 0c32279 | 2002-07-17 16:49:03 +0000 | [diff] [blame] | 97 | static PyTypeObject Xxo_Type = { |
Fred Drake | 6724835 | 1999-02-16 22:15:42 +0000 | [diff] [blame] | 98 | /* The ob_type field must be initialized in the module init function |
| 99 | * to be portable to Windows without using C++. */ |
| 100 | PyObject_HEAD_INIT(NULL) |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 101 | 0, /*ob_size*/ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 102 | "xxmodule.Xxo", /*tp_name*/ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 103 | sizeof(XxoObject), /*tp_basicsize*/ |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 104 | 0, /*tp_itemsize*/ |
| 105 | /* methods */ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 106 | (destructor)Xxo_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 107 | 0, /*tp_print*/ |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 108 | (getattrfunc)Xxo_getattr, /*tp_getattr*/ |
| 109 | (setattrfunc)Xxo_setattr, /*tp_setattr*/ |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 110 | 0, /*tp_compare*/ |
| 111 | 0, /*tp_repr*/ |
| 112 | 0, /*tp_as_number*/ |
| 113 | 0, /*tp_as_sequence*/ |
| 114 | 0, /*tp_as_mapping*/ |
| 115 | 0, /*tp_hash*/ |
Martin v. Löwis | ffa7aff | 2001-10-09 10:46:58 +0000 | [diff] [blame] | 116 | 0, /*tp_call*/ |
| 117 | 0, /*tp_str*/ |
| 118 | 0, /*tp_getattro*/ |
| 119 | 0, /*tp_setattro*/ |
| 120 | 0, /*tp_as_buffer*/ |
| 121 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 122 | 0, /*tp_doc*/ |
| 123 | 0, /*tp_traverse*/ |
| 124 | 0, /*tp_clear*/ |
| 125 | 0, /*tp_richcompare*/ |
| 126 | 0, /*tp_weaklistoffset*/ |
| 127 | 0, /*tp_iter*/ |
| 128 | 0, /*tp_iternext*/ |
| 129 | 0, /*tp_methods*/ |
| 130 | 0, /*tp_members*/ |
| 131 | 0, /*tp_getset*/ |
| 132 | 0, /*tp_base*/ |
| 133 | 0, /*tp_dict*/ |
| 134 | 0, /*tp_descr_get*/ |
| 135 | 0, /*tp_descr_set*/ |
| 136 | 0, /*tp_dictoffset*/ |
| 137 | 0, /*tp_init*/ |
| 138 | 0, /*tp_alloc*/ |
| 139 | 0, /*tp_new*/ |
| 140 | 0, /*tp_free*/ |
| 141 | 0, /*tp_is_gc*/ |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 142 | }; |
| 143 | /* --------------------------------------------------------------------- */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 144 | |
| 145 | /* Function of two integers returning integer */ |
| 146 | |
Skip Montanaro | d9e7d24 | 2002-08-14 01:44:33 +0000 | [diff] [blame] | 147 | PyDoc_STRVAR(xx_foo_doc, |
| 148 | "foo(i,j)\n\ |
| 149 | \n\ |
| 150 | Return the sum of i and j."); |
| 151 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 152 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 153 | xx_foo(PyObject *self, 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 | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 157 | if (!PyArg_ParseTuple(args, "ll:foo", &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 * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 167 | xx_new(PyObject *self, PyObject *args) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 168 | { |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 169 | XxoObject *rv; |
Tim Peters | c9ca5c8 | 2002-05-23 15:49:38 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 171 | if (!PyArg_ParseTuple(args, ":new")) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 172 | return NULL; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 173 | rv = newXxoObject(args); |
Tim Peters | c9ca5c8 | 2002-05-23 15:49:38 +0000 | [diff] [blame] | 174 | if (rv == NULL) |
| 175 | return NULL; |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 176 | return (PyObject *)rv; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Guido van Rossum | fbcfd52 | 1996-12-13 02:57:25 +0000 | [diff] [blame] | 179 | /* Example with subtle bug from extensions manual ("Thin Ice"). */ |
| 180 | |
| 181 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 182 | xx_bug(PyObject *self, PyObject *args) |
Guido van Rossum | fbcfd52 | 1996-12-13 02:57:25 +0000 | [diff] [blame] | 183 | { |
| 184 | PyObject *list, *item; |
Tim Peters | c9ca5c8 | 2002-05-23 15:49:38 +0000 | [diff] [blame] | 185 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 186 | if (!PyArg_ParseTuple(args, "O:bug", &list)) |
Guido van Rossum | fbcfd52 | 1996-12-13 02:57:25 +0000 | [diff] [blame] | 187 | return NULL; |
Tim Peters | c9ca5c8 | 2002-05-23 15:49:38 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | fbcfd52 | 1996-12-13 02:57:25 +0000 | [diff] [blame] | 189 | item = PyList_GetItem(list, 0); |
| 190 | /* Py_INCREF(item); */ |
| 191 | PyList_SetItem(list, 1, PyInt_FromLong(0L)); |
| 192 | PyObject_Print(item, stdout, 0); |
| 193 | printf("\n"); |
| 194 | /* Py_DECREF(item); */ |
Tim Peters | c9ca5c8 | 2002-05-23 15:49:38 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | fbcfd52 | 1996-12-13 02:57:25 +0000 | [diff] [blame] | 196 | Py_INCREF(Py_None); |
| 197 | return Py_None; |
| 198 | } |
| 199 | |
Guido van Rossum | c525e43 | 1997-12-09 20:37:25 +0000 | [diff] [blame] | 200 | /* Test bad format character */ |
| 201 | |
| 202 | static PyObject * |
Peter Schneider-Kamp | c4bc0e0 | 2000-07-10 11:56:03 +0000 | [diff] [blame] | 203 | xx_roj(PyObject *self, PyObject *args) |
Guido van Rossum | c525e43 | 1997-12-09 20:37:25 +0000 | [diff] [blame] | 204 | { |
| 205 | PyObject *a; |
| 206 | long b; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 207 | if (!PyArg_ParseTuple(args, "O#:roj", &a, &b)) |
Guido van Rossum | c525e43 | 1997-12-09 20:37:25 +0000 | [diff] [blame] | 208 | return NULL; |
| 209 | Py_INCREF(Py_None); |
| 210 | return Py_None; |
| 211 | } |
| 212 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | 9eb67ea | 2003-02-11 21:19:11 +0000 | [diff] [blame] | 214 | /* ---------- */ |
| 215 | |
| 216 | static PyTypeObject Str_Type = { |
| 217 | /* The ob_type field must be initialized in the module init function |
| 218 | * to be portable to Windows without using C++. */ |
| 219 | PyObject_HEAD_INIT(NULL) |
| 220 | 0, /*ob_size*/ |
| 221 | "xxmodule.Str", /*tp_name*/ |
| 222 | 0, /*tp_basicsize*/ |
| 223 | 0, /*tp_itemsize*/ |
| 224 | /* methods */ |
| 225 | 0, /*tp_dealloc*/ |
| 226 | 0, /*tp_print*/ |
| 227 | 0, /*tp_getattr*/ |
| 228 | 0, /*tp_setattr*/ |
| 229 | 0, /*tp_compare*/ |
| 230 | 0, /*tp_repr*/ |
| 231 | 0, /*tp_as_number*/ |
| 232 | 0, /*tp_as_sequence*/ |
| 233 | 0, /*tp_as_mapping*/ |
| 234 | 0, /*tp_hash*/ |
| 235 | 0, /*tp_call*/ |
| 236 | 0, /*tp_str*/ |
| 237 | 0, /*tp_getattro*/ |
| 238 | 0, /*tp_setattro*/ |
| 239 | 0, /*tp_as_buffer*/ |
| 240 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 241 | 0, /*tp_doc*/ |
| 242 | 0, /*tp_traverse*/ |
| 243 | 0, /*tp_clear*/ |
| 244 | 0, /*tp_richcompare*/ |
| 245 | 0, /*tp_weaklistoffset*/ |
| 246 | 0, /*tp_iter*/ |
| 247 | 0, /*tp_iternext*/ |
| 248 | 0, /*tp_methods*/ |
| 249 | 0, /*tp_members*/ |
| 250 | 0, /*tp_getset*/ |
| 251 | &PyString_Type, /*tp_base*/ |
| 252 | 0, /*tp_dict*/ |
| 253 | 0, /*tp_descr_get*/ |
| 254 | 0, /*tp_descr_set*/ |
| 255 | 0, /*tp_dictoffset*/ |
| 256 | 0, /*tp_init*/ |
| 257 | 0, /*tp_alloc*/ |
| 258 | 0, /*tp_new*/ |
| 259 | 0, /*tp_free*/ |
| 260 | 0, /*tp_is_gc*/ |
| 261 | }; |
| 262 | |
Guido van Rossum | 7297650 | 2003-02-13 18:44:57 +0000 | [diff] [blame] | 263 | /* ---------- */ |
| 264 | |
| 265 | static PyObject * |
| 266 | null_richcompare(PyObject *self, PyObject *other, int op) |
| 267 | { |
| 268 | Py_INCREF(Py_NotImplemented); |
| 269 | return Py_NotImplemented; |
| 270 | } |
| 271 | |
| 272 | static PyTypeObject Null_Type = { |
| 273 | /* The ob_type field must be initialized in the module init function |
| 274 | * to be portable to Windows without using C++. */ |
| 275 | PyObject_HEAD_INIT(NULL) |
| 276 | 0, /*ob_size*/ |
| 277 | "xxmodule.Null", /*tp_name*/ |
| 278 | 0, /*tp_basicsize*/ |
| 279 | 0, /*tp_itemsize*/ |
| 280 | /* methods */ |
| 281 | 0, /*tp_dealloc*/ |
| 282 | 0, /*tp_print*/ |
| 283 | 0, /*tp_getattr*/ |
| 284 | 0, /*tp_setattr*/ |
| 285 | 0, /*tp_compare*/ |
| 286 | 0, /*tp_repr*/ |
| 287 | 0, /*tp_as_number*/ |
| 288 | 0, /*tp_as_sequence*/ |
| 289 | 0, /*tp_as_mapping*/ |
| 290 | 0, /*tp_hash*/ |
| 291 | 0, /*tp_call*/ |
| 292 | 0, /*tp_str*/ |
| 293 | 0, /*tp_getattro*/ |
| 294 | 0, /*tp_setattro*/ |
| 295 | 0, /*tp_as_buffer*/ |
| 296 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 297 | 0, /*tp_doc*/ |
| 298 | 0, /*tp_traverse*/ |
| 299 | 0, /*tp_clear*/ |
| 300 | null_richcompare, /*tp_richcompare*/ |
| 301 | 0, /*tp_weaklistoffset*/ |
| 302 | 0, /*tp_iter*/ |
| 303 | 0, /*tp_iternext*/ |
| 304 | 0, /*tp_methods*/ |
| 305 | 0, /*tp_members*/ |
| 306 | 0, /*tp_getset*/ |
| 307 | &PyBaseObject_Type, /*tp_base*/ |
| 308 | 0, /*tp_dict*/ |
| 309 | 0, /*tp_descr_get*/ |
| 310 | 0, /*tp_descr_set*/ |
| 311 | 0, /*tp_dictoffset*/ |
| 312 | 0, /*tp_init*/ |
| 313 | 0, /*tp_alloc*/ |
| 314 | PyType_GenericNew, /*tp_new*/ |
| 315 | 0, /*tp_free*/ |
| 316 | 0, /*tp_is_gc*/ |
| 317 | }; |
| 318 | |
Guido van Rossum | 9eb67ea | 2003-02-11 21:19:11 +0000 | [diff] [blame] | 319 | |
| 320 | /* ---------- */ |
| 321 | |
| 322 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 323 | /* List of functions defined in the module */ |
| 324 | |
Guido van Rossum | 2b65444 | 1996-07-30 16:56:16 +0000 | [diff] [blame] | 325 | static PyMethodDef xx_methods[] = { |
Skip Montanaro | d9e7d24 | 2002-08-14 01:44:33 +0000 | [diff] [blame] | 326 | {"roj", xx_roj, METH_VARARGS, |
| 327 | PyDoc_STR("roj(a,b) -> None")}, |
| 328 | {"foo", xx_foo, METH_VARARGS, |
| 329 | xx_foo_doc}, |
| 330 | {"new", xx_new, METH_VARARGS, |
| 331 | PyDoc_STR("new() -> new Xx object")}, |
| 332 | {"bug", xx_bug, METH_VARARGS, |
| 333 | PyDoc_STR("bug(o) -> None")}, |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 334 | {NULL, NULL} /* sentinel */ |
| 335 | }; |
| 336 | |
Skip Montanaro | d9e7d24 | 2002-08-14 01:44:33 +0000 | [diff] [blame] | 337 | PyDoc_STRVAR(module_doc, |
| 338 | "This is a template module just for instruction."); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 339 | |
| 340 | /* Initialization function for the module (*must* be called initxx) */ |
| 341 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 342 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 343 | initxx(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 344 | { |
Thomas Heller | 1630520 | 2002-04-09 12:50:13 +0000 | [diff] [blame] | 345 | PyObject *m; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 346 | |
Raymond Hettinger | 3c736f1 | 2002-12-29 17:16:49 +0000 | [diff] [blame] | 347 | /* Finalize the type object including setting type of the new type |
| 348 | * object; doing it here is required for portability to Windows |
| 349 | * without requiring C++. */ |
| 350 | if (PyType_Ready(&Xxo_Type) < 0) |
| 351 | return; |
Fred Drake | 6724835 | 1999-02-16 22:15:42 +0000 | [diff] [blame] | 352 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 353 | /* Create the module and add the functions */ |
Skip Montanaro | d9e7d24 | 2002-08-14 01:44:33 +0000 | [diff] [blame] | 354 | m = Py_InitModule3("xx", xx_methods, module_doc); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 355 | |
| 356 | /* Add some symbolic constants to the module */ |
Fred Drake | 1de5a72 | 2002-03-12 21:49:44 +0000 | [diff] [blame] | 357 | if (ErrorObject == NULL) { |
| 358 | ErrorObject = PyErr_NewException("xx.error", NULL, NULL); |
| 359 | if (ErrorObject == NULL) |
| 360 | return; |
| 361 | } |
| 362 | Py_INCREF(ErrorObject); |
Thomas Heller | 1630520 | 2002-04-09 12:50:13 +0000 | [diff] [blame] | 363 | PyModule_AddObject(m, "error", ErrorObject); |
Guido van Rossum | 9eb67ea | 2003-02-11 21:19:11 +0000 | [diff] [blame] | 364 | |
| 365 | /* Add Str */ |
| 366 | if (PyType_Ready(&Str_Type) < 0) |
| 367 | return; |
| 368 | PyModule_AddObject(m, "Str", (PyObject *)&Str_Type); |
Guido van Rossum | 7297650 | 2003-02-13 18:44:57 +0000 | [diff] [blame] | 369 | |
| 370 | /* Add Null */ |
| 371 | if (PyType_Ready(&Null_Type) < 0) |
| 372 | return; |
| 373 | PyModule_AddObject(m, "Null", (PyObject *)&Null_Type); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 374 | } |