blob: bb8a4022e3f0d13793a56d87d44323286a8ec73b [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum320a5cc1991-01-02 15:11:48 +000011/* 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 Rossum85a5fbb1990-10-14 12:07:46 +000021/* Xx objects */
22
Guido van Rossum03093a21994-09-28 15:51:32 +000023#include "Python.h"
Guido van Rossum320a5cc1991-01-02 15:11:48 +000024
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025typedef struct {
Guido van Rossum03093a21994-09-28 15:51:32 +000026 PyObject_HEAD
27 PyObject *x_attr; /* Attributes dictionary */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028} xxobject;
29
Guido van Rossum03093a21994-09-28 15:51:32 +000030staticforward PyTypeObject Xxtype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031
Guido van Rossum320a5cc1991-01-02 15:11:48 +000032#define is_xxobject(v) ((v)->ob_type == &Xxtype)
33
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034static xxobject *
35newxxobject(arg)
Guido van Rossum03093a21994-09-28 15:51:32 +000036 PyObject *arg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037{
Guido van Rossum94726d51991-01-02 15:12:51 +000038 xxobject *xp;
Guido van Rossum03093a21994-09-28 15:51:32 +000039 xp = PyObject_NEW(xxobject, &Xxtype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040 if (xp == NULL)
41 return NULL;
42 xp->x_attr = NULL;
43 return xp;
44}
45
46/* Xx methods */
47
48static void
49xx_dealloc(xp)
50 xxobject *xp;
51{
Guido van Rossum03093a21994-09-28 15:51:32 +000052 Py_XDECREF(xp->x_attr);
Guido van Rossumb18618d2000-05-03 23:44:39 +000053 PyObject_DEL(xp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054}
55
Guido van Rossum03093a21994-09-28 15:51:32 +000056static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057xx_demo(self, args)
58 xxobject *self;
Guido van Rossum03093a21994-09-28 15:51:32 +000059 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060{
Guido van Rossum03093a21994-09-28 15:51:32 +000061 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062 return NULL;
Guido van Rossum03093a21994-09-28 15:51:32 +000063 Py_INCREF(Py_None);
64 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065}
66
Guido van Rossum00d225e1999-01-29 14:39:12 +000067static PyMethodDef xx_methods[] = {
Guido van Rossum03093a21994-09-28 15:51:32 +000068 {"demo", (PyCFunction)xx_demo},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069 {NULL, NULL} /* sentinel */
70};
71
Guido van Rossum03093a21994-09-28 15:51:32 +000072static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073xx_getattr(xp, name)
74 xxobject *xp;
75 char *name;
76{
77 if (xp->x_attr != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000078 PyObject *v = PyDict_GetItemString(xp->x_attr, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 if (v != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000080 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081 return v;
82 }
83 }
Guido van Rossum03093a21994-09-28 15:51:32 +000084 return Py_FindMethod(xx_methods, (PyObject *)xp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085}
86
87static int
88xx_setattr(xp, name, v)
89 xxobject *xp;
90 char *name;
Guido van Rossum03093a21994-09-28 15:51:32 +000091 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092{
93 if (xp->x_attr == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000094 xp->x_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095 if (xp->x_attr == NULL)
Guido van Rossum320a5cc1991-01-02 15:11:48 +000096 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 }
Guido van Rossum94472a01992-09-04 09:45:18 +000098 if (v == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000099 int rv = PyDict_DelItemString(xp->x_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000100 if (rv < 0)
Guido van Rossum03093a21994-09-28 15:51:32 +0000101 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum94472a01992-09-04 09:45:18 +0000102 "delete non-existing xx attribute");
103 return rv;
104 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105 else
Guido van Rossum03093a21994-09-28 15:51:32 +0000106 return PyDict_SetItemString(xp->x_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107}
108
Guido van Rossum03093a21994-09-28 15:51:32 +0000109static PyTypeObject Xxtype = {
110 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 0, /*ob_size*/
112 "xx", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000113 sizeof(xxobject), /*tp_basicsize*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 0, /*tp_itemsize*/
115 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000116 (destructor)xx_dealloc, /*tp_dealloc*/
Guido van Rossum70d7a311992-08-14 12:04:19 +0000117 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000118 (getattrfunc)xx_getattr, /*tp_getattr*/
119 (setattrfunc)xx_setattr, /*tp_setattr*/
Guido van Rossum70d7a311992-08-14 12:04:19 +0000120 0, /*tp_compare*/
121 0, /*tp_repr*/
122 0, /*tp_as_number*/
123 0, /*tp_as_sequence*/
124 0, /*tp_as_mapping*/
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000125 0, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126};