blob: c5b518f7bdde7aca619848a7c8a2554edef3781e [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
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum320a5cc1991-01-02 15:11:48 +000032/* 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 Rossum85a5fbb1990-10-14 12:07:46 +000042/* Xx objects */
43
Guido van Rossum03093a21994-09-28 15:51:32 +000044#include "Python.h"
Guido van Rossum320a5cc1991-01-02 15:11:48 +000045
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046typedef struct {
Guido van Rossum03093a21994-09-28 15:51:32 +000047 PyObject_HEAD
48 PyObject *x_attr; /* Attributes dictionary */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049} xxobject;
50
Guido van Rossum03093a21994-09-28 15:51:32 +000051staticforward PyTypeObject Xxtype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052
Guido van Rossum320a5cc1991-01-02 15:11:48 +000053#define is_xxobject(v) ((v)->ob_type == &Xxtype)
54
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055static xxobject *
56newxxobject(arg)
Guido van Rossum03093a21994-09-28 15:51:32 +000057 PyObject *arg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058{
Guido van Rossum94726d51991-01-02 15:12:51 +000059 xxobject *xp;
Guido van Rossum03093a21994-09-28 15:51:32 +000060 xp = PyObject_NEW(xxobject, &Xxtype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061 if (xp == NULL)
62 return NULL;
63 xp->x_attr = NULL;
64 return xp;
65}
66
67/* Xx methods */
68
69static void
70xx_dealloc(xp)
71 xxobject *xp;
72{
Guido van Rossum03093a21994-09-28 15:51:32 +000073 Py_XDECREF(xp->x_attr);
74 PyMem_DEL(xp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075}
76
Guido van Rossum03093a21994-09-28 15:51:32 +000077static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078xx_demo(self, args)
79 xxobject *self;
Guido van Rossum03093a21994-09-28 15:51:32 +000080 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081{
Guido van Rossum03093a21994-09-28 15:51:32 +000082 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083 return NULL;
Guido van Rossum03093a21994-09-28 15:51:32 +000084 Py_INCREF(Py_None);
85 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086}
87
Guido van Rossum00d225e1999-01-29 14:39:12 +000088static PyMethodDef xx_methods[] = {
Guido van Rossum03093a21994-09-28 15:51:32 +000089 {"demo", (PyCFunction)xx_demo},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 {NULL, NULL} /* sentinel */
91};
92
Guido van Rossum03093a21994-09-28 15:51:32 +000093static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094xx_getattr(xp, name)
95 xxobject *xp;
96 char *name;
97{
98 if (xp->x_attr != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000099 PyObject *v = PyDict_GetItemString(xp->x_attr, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100 if (v != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +0000101 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 return v;
103 }
104 }
Guido van Rossum03093a21994-09-28 15:51:32 +0000105 return Py_FindMethod(xx_methods, (PyObject *)xp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106}
107
108static int
109xx_setattr(xp, name, v)
110 xxobject *xp;
111 char *name;
Guido van Rossum03093a21994-09-28 15:51:32 +0000112 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113{
114 if (xp->x_attr == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +0000115 xp->x_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116 if (xp->x_attr == NULL)
Guido van Rossum320a5cc1991-01-02 15:11:48 +0000117 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118 }
Guido van Rossum94472a01992-09-04 09:45:18 +0000119 if (v == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +0000120 int rv = PyDict_DelItemString(xp->x_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000121 if (rv < 0)
Guido van Rossum03093a21994-09-28 15:51:32 +0000122 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum94472a01992-09-04 09:45:18 +0000123 "delete non-existing xx attribute");
124 return rv;
125 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 else
Guido van Rossum03093a21994-09-28 15:51:32 +0000127 return PyDict_SetItemString(xp->x_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128}
129
Guido van Rossum03093a21994-09-28 15:51:32 +0000130static PyTypeObject Xxtype = {
131 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132 0, /*ob_size*/
133 "xx", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000134 sizeof(xxobject), /*tp_basicsize*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135 0, /*tp_itemsize*/
136 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137 (destructor)xx_dealloc, /*tp_dealloc*/
Guido van Rossum70d7a311992-08-14 12:04:19 +0000138 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000139 (getattrfunc)xx_getattr, /*tp_getattr*/
140 (setattrfunc)xx_setattr, /*tp_setattr*/
Guido van Rossum70d7a311992-08-14 12:04:19 +0000141 0, /*tp_compare*/
142 0, /*tp_repr*/
143 0, /*tp_as_number*/
144 0, /*tp_as_sequence*/
145 0, /*tp_as_mapping*/
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000146 0, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147};