Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 32 | /* Use this file as a template to start implementing a new object type. |
| 33 | If your objects will be called foobar, start by copying this file to |
| 34 | foobarobject.c, changing all occurrences of xx to foobar and all |
| 35 | occurrences of Xx by Foobar. You will probably want to delete all |
| 36 | references to 'x_attr' and add your own types of attributes |
| 37 | instead. Maybe you want to name your local variables other than |
| 38 | 'xp'. If your object type is needed in other files, you'll have to |
| 39 | create a file "foobarobject.h"; see intobject.h for an example. */ |
| 40 | |
| 41 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | /* Xx objects */ |
| 43 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 44 | #include "Python.h" |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | typedef struct { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 47 | PyObject_HEAD |
| 48 | PyObject *x_attr; /* Attributes dictionary */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | } xxobject; |
| 50 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 51 | staticforward PyTypeObject Xxtype; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 53 | #define is_xxobject(v) ((v)->ob_type == &Xxtype) |
| 54 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 55 | static xxobject * |
| 56 | newxxobject(arg) |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 57 | PyObject *arg; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | { |
Guido van Rossum | 94726d5 | 1991-01-02 15:12:51 +0000 | [diff] [blame] | 59 | xxobject *xp; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 60 | xp = PyObject_NEW(xxobject, &Xxtype); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | if (xp == NULL) |
| 62 | return NULL; |
| 63 | xp->x_attr = NULL; |
| 64 | return xp; |
| 65 | } |
| 66 | |
| 67 | /* Xx methods */ |
| 68 | |
| 69 | static void |
| 70 | xx_dealloc(xp) |
| 71 | xxobject *xp; |
| 72 | { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 73 | Py_XDECREF(xp->x_attr); |
| 74 | PyMem_DEL(xp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 77 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 78 | xx_demo(self, args) |
| 79 | xxobject *self; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 80 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 81 | { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 82 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | return NULL; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 84 | Py_INCREF(Py_None); |
| 85 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 88 | static Py_MethodDef xx_methods[] = { |
| 89 | {"demo", (PyCFunction)xx_demo}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | {NULL, NULL} /* sentinel */ |
| 91 | }; |
| 92 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 93 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | xx_getattr(xp, name) |
| 95 | xxobject *xp; |
| 96 | char *name; |
| 97 | { |
| 98 | if (xp->x_attr != NULL) { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 99 | PyObject *v = PyDict_GetItemString(xp->x_attr, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | if (v != NULL) { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 101 | Py_INCREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | return v; |
| 103 | } |
| 104 | } |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 105 | return Py_FindMethod(xx_methods, (PyObject *)xp, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static int |
| 109 | xx_setattr(xp, name, v) |
| 110 | xxobject *xp; |
| 111 | char *name; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 112 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | { |
| 114 | if (xp->x_attr == NULL) { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 115 | xp->x_attr = PyDict_New(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | if (xp->x_attr == NULL) |
Guido van Rossum | 320a5cc | 1991-01-02 15:11:48 +0000 | [diff] [blame] | 117 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | } |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 119 | if (v == NULL) { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 120 | int rv = PyDict_DelItemString(xp->x_attr, name); |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 121 | if (rv < 0) |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 122 | PyErr_SetString(PyExc_AttributeError, |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 123 | "delete non-existing xx attribute"); |
| 124 | return rv; |
| 125 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | else |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 127 | return PyDict_SetItemString(xp->x_attr, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 130 | static PyTypeObject Xxtype = { |
| 131 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | 0, /*ob_size*/ |
| 133 | "xx", /*tp_name*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 134 | sizeof(xxobject), /*tp_basicsize*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | 0, /*tp_itemsize*/ |
| 136 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 137 | (destructor)xx_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 70d7a31 | 1992-08-14 12:04:19 +0000 | [diff] [blame] | 138 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 139 | (getattrfunc)xx_getattr, /*tp_getattr*/ |
| 140 | (setattrfunc)xx_setattr, /*tp_setattr*/ |
Guido van Rossum | 70d7a31 | 1992-08-14 12:04:19 +0000 | [diff] [blame] | 141 | 0, /*tp_compare*/ |
| 142 | 0, /*tp_repr*/ |
| 143 | 0, /*tp_as_number*/ |
| 144 | 0, /*tp_as_sequence*/ |
| 145 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 146 | 0, /*tp_hash*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | }; |