blob: bb8a5f677f900f5178fb3492fed7bf5cb98b1d13 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum320a5cc1991-01-02 15:11:48 +000025/* Use this file as a template to start implementing a new object type.
26 If your objects will be called foobar, start by copying this file to
27 foobarobject.c, changing all occurrences of xx to foobar and all
28 occurrences of Xx by Foobar. You will probably want to delete all
29 references to 'x_attr' and add your own types of attributes
30 instead. Maybe you want to name your local variables other than
31 'xp'. If your object type is needed in other files, you'll have to
32 create a file "foobarobject.h"; see intobject.h for an example. */
33
34
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035/* Xx objects */
36
Guido van Rossum03093a21994-09-28 15:51:32 +000037#include "Python.h"
Guido van Rossum320a5cc1991-01-02 15:11:48 +000038
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039typedef struct {
Guido van Rossum03093a21994-09-28 15:51:32 +000040 PyObject_HEAD
41 PyObject *x_attr; /* Attributes dictionary */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042} xxobject;
43
Guido van Rossum03093a21994-09-28 15:51:32 +000044staticforward PyTypeObject Xxtype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Guido van Rossum320a5cc1991-01-02 15:11:48 +000046#define is_xxobject(v) ((v)->ob_type == &Xxtype)
47
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048static xxobject *
49newxxobject(arg)
Guido van Rossum03093a21994-09-28 15:51:32 +000050 PyObject *arg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051{
Guido van Rossum94726d51991-01-02 15:12:51 +000052 xxobject *xp;
Guido van Rossum03093a21994-09-28 15:51:32 +000053 xp = PyObject_NEW(xxobject, &Xxtype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054 if (xp == NULL)
55 return NULL;
56 xp->x_attr = NULL;
57 return xp;
58}
59
60/* Xx methods */
61
62static void
63xx_dealloc(xp)
64 xxobject *xp;
65{
Guido van Rossum03093a21994-09-28 15:51:32 +000066 Py_XDECREF(xp->x_attr);
67 PyMem_DEL(xp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
Guido van Rossum03093a21994-09-28 15:51:32 +000070static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071xx_demo(self, args)
72 xxobject *self;
Guido van Rossum03093a21994-09-28 15:51:32 +000073 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074{
Guido van Rossum03093a21994-09-28 15:51:32 +000075 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 return NULL;
Guido van Rossum03093a21994-09-28 15:51:32 +000077 Py_INCREF(Py_None);
78 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079}
80
Guido van Rossum03093a21994-09-28 15:51:32 +000081static Py_MethodDef xx_methods[] = {
82 {"demo", (PyCFunction)xx_demo},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083 {NULL, NULL} /* sentinel */
84};
85
Guido van Rossum03093a21994-09-28 15:51:32 +000086static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087xx_getattr(xp, name)
88 xxobject *xp;
89 char *name;
90{
91 if (xp->x_attr != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000092 PyObject *v = PyDict_GetItemString(xp->x_attr, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 if (v != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000094 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095 return v;
96 }
97 }
Guido van Rossum03093a21994-09-28 15:51:32 +000098 return Py_FindMethod(xx_methods, (PyObject *)xp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099}
100
101static int
102xx_setattr(xp, name, v)
103 xxobject *xp;
104 char *name;
Guido van Rossum03093a21994-09-28 15:51:32 +0000105 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106{
107 if (xp->x_attr == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +0000108 xp->x_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 if (xp->x_attr == NULL)
Guido van Rossum320a5cc1991-01-02 15:11:48 +0000110 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 }
Guido van Rossum94472a01992-09-04 09:45:18 +0000112 if (v == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +0000113 int rv = PyDict_DelItemString(xp->x_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000114 if (rv < 0)
Guido van Rossum03093a21994-09-28 15:51:32 +0000115 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum94472a01992-09-04 09:45:18 +0000116 "delete non-existing xx attribute");
117 return rv;
118 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119 else
Guido van Rossum03093a21994-09-28 15:51:32 +0000120 return PyDict_SetItemString(xp->x_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121}
122
Guido van Rossum03093a21994-09-28 15:51:32 +0000123static PyTypeObject Xxtype = {
124 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125 0, /*ob_size*/
126 "xx", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000127 sizeof(xxobject), /*tp_basicsize*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 0, /*tp_itemsize*/
129 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000130 (destructor)xx_dealloc, /*tp_dealloc*/
Guido van Rossum70d7a311992-08-14 12:04:19 +0000131 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000132 (getattrfunc)xx_getattr, /*tp_getattr*/
133 (setattrfunc)xx_setattr, /*tp_setattr*/
Guido van Rossum70d7a311992-08-14 12:04:19 +0000134 0, /*tp_compare*/
135 0, /*tp_repr*/
136 0, /*tp_as_number*/
137 0, /*tp_as_sequence*/
138 0, /*tp_as_mapping*/
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000139 0, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140};