Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 2 | /* Use this file as a template to start implementing a new object type. |
| 3 | If your objects will be called foobar, start by copying this file to |
| 4 | foobarobject.c, changing all occurrences of xx to foobar and all |
| 5 | occurrences of Xx by Foobar. You will probably want to delete all |
| 6 | references to 'x_attr' and add your own types of attributes |
| 7 | instead. Maybe you want to name your local variables other than |
| 8 | 'xp'. If your object type is needed in other files, you'll have to |
| 9 | create a file "foobarobject.h"; see intobject.h for an example. */ |
| 10 | |
| 11 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12 | /* Xx objects */ |
| 13 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 14 | #include "Python.h" |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 16 | typedef struct { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 17 | PyObject_HEAD |
| 18 | PyObject *x_attr; /* Attributes dictionary */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 19 | } xxobject; |
| 20 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 21 | staticforward PyTypeObject Xxtype; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 23 | #define is_xxobject(v) ((v)->ob_type == &Xxtype) |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | static xxobject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 26 | newxxobject(PyObject *arg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 27 | { |
Guido van Rossum | 94726d5 | 1991-01-02 15:12:51 +0000 | [diff] [blame] | 28 | xxobject *xp; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 29 | xp = PyObject_NEW(xxobject, &Xxtype); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 30 | if (xp == NULL) |
| 31 | return NULL; |
| 32 | xp->x_attr = NULL; |
| 33 | return xp; |
| 34 | } |
| 35 | |
| 36 | /* Xx methods */ |
| 37 | |
| 38 | static void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 39 | xx_dealloc(xxobject *xp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 41 | Py_XDECREF(xp->x_attr); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 42 | PyObject_DEL(xp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 45 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 46 | xx_demo(xxobject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 47 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 48 | if (!PyArg_ParseTuple(args, ":demo")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | return NULL; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 50 | Py_INCREF(Py_None); |
| 51 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Guido van Rossum | 00d225e | 1999-01-29 14:39:12 +0000 | [diff] [blame] | 54 | static PyMethodDef xx_methods[] = { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 55 | {"demo", (PyCFunction)xx_demo, METH_VARARGS}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | {NULL, NULL} /* sentinel */ |
| 57 | }; |
| 58 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 59 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 60 | xx_getattr(xxobject *xp, char *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | { |
| 62 | if (xp->x_attr != NULL) { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 63 | PyObject *v = PyDict_GetItemString(xp->x_attr, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | if (v != NULL) { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 65 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | return v; |
| 67 | } |
| 68 | } |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 69 | return Py_FindMethod(xx_methods, (PyObject *)xp, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 73 | xx_setattr(xxobject *xp, char *name, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | { |
| 75 | if (xp->x_attr == NULL) { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 76 | xp->x_attr = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | if (xp->x_attr == NULL) |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 78 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | } |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 80 | if (v == NULL) { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 81 | int rv = PyDict_DelItemString(xp->x_attr, name); |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 82 | if (rv < 0) |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 83 | PyErr_SetString(PyExc_AttributeError, |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 84 | "delete non-existing xx attribute"); |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 85 | return rv; |
| 86 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | else |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 88 | return PyDict_SetItemString(xp->x_attr, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 91 | static PyTypeObject Xxtype = { |
| 92 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | 0, /*ob_size*/ |
| 94 | "xx", /*tp_name*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 95 | sizeof(xxobject), /*tp_basicsize*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | 0, /*tp_itemsize*/ |
| 97 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 98 | (destructor)xx_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 70d7a31 | 1992-08-14 12:04:19 +0000 | [diff] [blame] | 99 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 100 | (getattrfunc)xx_getattr, /*tp_getattr*/ |
| 101 | (setattrfunc)xx_setattr, /*tp_setattr*/ |
Guido van Rossum | 70d7a31 | 1992-08-14 12:04:19 +0000 | [diff] [blame] | 102 | 0, /*tp_compare*/ |
| 103 | 0, /*tp_repr*/ |
| 104 | 0, /*tp_as_number*/ |
| 105 | 0, /*tp_as_sequence*/ |
| 106 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 107 | 0, /*tp_hash*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | }; |