blob: 6309931f15e3d50aa3c5ce3ae743de98094edbbe [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 Rossumfd71b9e2000-06-30 23:50:40 +00007Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000012See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000014
15******************************************************************/
16
Guido van Rossum320a5cc1991-01-02 15:11:48 +000017/* Use this file as a template to start implementing a new object type.
18 If your objects will be called foobar, start by copying this file to
19 foobarobject.c, changing all occurrences of xx to foobar and all
20 occurrences of Xx by Foobar. You will probably want to delete all
21 references to 'x_attr' and add your own types of attributes
22 instead. Maybe you want to name your local variables other than
23 'xp'. If your object type is needed in other files, you'll have to
24 create a file "foobarobject.h"; see intobject.h for an example. */
25
26
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027/* Xx objects */
28
Guido van Rossum03093a21994-09-28 15:51:32 +000029#include "Python.h"
Guido van Rossum320a5cc1991-01-02 15:11:48 +000030
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031typedef struct {
Guido van Rossum03093a21994-09-28 15:51:32 +000032 PyObject_HEAD
33 PyObject *x_attr; /* Attributes dictionary */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034} xxobject;
35
Guido van Rossum03093a21994-09-28 15:51:32 +000036staticforward PyTypeObject Xxtype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037
Guido van Rossum320a5cc1991-01-02 15:11:48 +000038#define is_xxobject(v) ((v)->ob_type == &Xxtype)
39
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040static xxobject *
41newxxobject(arg)
Guido van Rossum03093a21994-09-28 15:51:32 +000042 PyObject *arg;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043{
Guido van Rossum94726d51991-01-02 15:12:51 +000044 xxobject *xp;
Guido van Rossum03093a21994-09-28 15:51:32 +000045 xp = PyObject_NEW(xxobject, &Xxtype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 if (xp == NULL)
47 return NULL;
48 xp->x_attr = NULL;
49 return xp;
50}
51
52/* Xx methods */
53
54static void
55xx_dealloc(xp)
56 xxobject *xp;
57{
Guido van Rossum03093a21994-09-28 15:51:32 +000058 Py_XDECREF(xp->x_attr);
Guido van Rossumb18618d2000-05-03 23:44:39 +000059 PyObject_DEL(xp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060}
61
Guido van Rossum03093a21994-09-28 15:51:32 +000062static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063xx_demo(self, args)
64 xxobject *self;
Guido van Rossum03093a21994-09-28 15:51:32 +000065 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066{
Guido van Rossum03093a21994-09-28 15:51:32 +000067 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068 return NULL;
Guido van Rossum03093a21994-09-28 15:51:32 +000069 Py_INCREF(Py_None);
70 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071}
72
Guido van Rossum00d225e1999-01-29 14:39:12 +000073static PyMethodDef xx_methods[] = {
Guido van Rossum03093a21994-09-28 15:51:32 +000074 {"demo", (PyCFunction)xx_demo},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075 {NULL, NULL} /* sentinel */
76};
77
Guido van Rossum03093a21994-09-28 15:51:32 +000078static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079xx_getattr(xp, name)
80 xxobject *xp;
81 char *name;
82{
83 if (xp->x_attr != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000084 PyObject *v = PyDict_GetItemString(xp->x_attr, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 if (v != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000086 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087 return v;
88 }
89 }
Guido van Rossum03093a21994-09-28 15:51:32 +000090 return Py_FindMethod(xx_methods, (PyObject *)xp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091}
92
93static int
94xx_setattr(xp, name, v)
95 xxobject *xp;
96 char *name;
Guido van Rossum03093a21994-09-28 15:51:32 +000097 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098{
99 if (xp->x_attr == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +0000100 xp->x_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 if (xp->x_attr == NULL)
Guido van Rossum320a5cc1991-01-02 15:11:48 +0000102 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 }
Guido van Rossum94472a01992-09-04 09:45:18 +0000104 if (v == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +0000105 int rv = PyDict_DelItemString(xp->x_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000106 if (rv < 0)
Guido van Rossum03093a21994-09-28 15:51:32 +0000107 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum94472a01992-09-04 09:45:18 +0000108 "delete non-existing xx attribute");
109 return rv;
110 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 else
Guido van Rossum03093a21994-09-28 15:51:32 +0000112 return PyDict_SetItemString(xp->x_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113}
114
Guido van Rossum03093a21994-09-28 15:51:32 +0000115static PyTypeObject Xxtype = {
116 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117 0, /*ob_size*/
118 "xx", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000119 sizeof(xxobject), /*tp_basicsize*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 0, /*tp_itemsize*/
121 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000122 (destructor)xx_dealloc, /*tp_dealloc*/
Guido van Rossum70d7a311992-08-14 12:04:19 +0000123 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000124 (getattrfunc)xx_getattr, /*tp_getattr*/
125 (setattrfunc)xx_setattr, /*tp_setattr*/
Guido van Rossum70d7a311992-08-14 12:04:19 +0000126 0, /*tp_compare*/
127 0, /*tp_repr*/
128 0, /*tp_as_number*/
129 0, /*tp_as_sequence*/
130 0, /*tp_as_mapping*/
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000131 0, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132};