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