blob: 21d0aa2e1d3075e41bdc2a823fd4836bdbadb722 [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 *
Fred Drakeba096332000-07-09 07:04:36 +000035newxxobject(PyObject *arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036{
Guido van Rossum94726d51991-01-02 15:12:51 +000037 xxobject *xp;
Guido van Rossum03093a21994-09-28 15:51:32 +000038 xp = PyObject_NEW(xxobject, &Xxtype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039 if (xp == NULL)
40 return NULL;
41 xp->x_attr = NULL;
42 return xp;
43}
44
45/* Xx methods */
46
47static void
Fred Drakeba096332000-07-09 07:04:36 +000048xx_dealloc(xxobject *xp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049{
Guido van Rossum03093a21994-09-28 15:51:32 +000050 Py_XDECREF(xp->x_attr);
Guido van Rossumb18618d2000-05-03 23:44:39 +000051 PyObject_DEL(xp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052}
53
Guido van Rossum03093a21994-09-28 15:51:32 +000054static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +000055xx_demo(xxobject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056{
Fred Drakeba096332000-07-09 07:04:36 +000057 if (!PyArg_ParseTuple(args, ":demo"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 return NULL;
Guido van Rossum03093a21994-09-28 15:51:32 +000059 Py_INCREF(Py_None);
60 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061}
62
Guido van Rossum00d225e1999-01-29 14:39:12 +000063static PyMethodDef xx_methods[] = {
Fred Drakeba096332000-07-09 07:04:36 +000064 {"demo", (PyCFunction)xx_demo, METH_VARARGS},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065 {NULL, NULL} /* sentinel */
66};
67
Guido van Rossum03093a21994-09-28 15:51:32 +000068static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +000069xx_getattr(xxobject *xp, char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070{
71 if (xp->x_attr != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000072 PyObject *v = PyDict_GetItemString(xp->x_attr, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073 if (v != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000074 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075 return v;
76 }
77 }
Guido van Rossum03093a21994-09-28 15:51:32 +000078 return Py_FindMethod(xx_methods, (PyObject *)xp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079}
80
81static int
Fred Drakeba096332000-07-09 07:04:36 +000082xx_setattr(xxobject *xp, char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083{
84 if (xp->x_attr == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000085 xp->x_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086 if (xp->x_attr == NULL)
Guido van Rossum320a5cc1991-01-02 15:11:48 +000087 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 }
Guido van Rossum94472a01992-09-04 09:45:18 +000089 if (v == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000090 int rv = PyDict_DelItemString(xp->x_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +000091 if (rv < 0)
Guido van Rossum03093a21994-09-28 15:51:32 +000092 PyErr_SetString(PyExc_AttributeError,
Fred Drakeba096332000-07-09 07:04:36 +000093 "delete non-existing xx attribute");
Guido van Rossum94472a01992-09-04 09:45:18 +000094 return rv;
95 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096 else
Guido van Rossum03093a21994-09-28 15:51:32 +000097 return PyDict_SetItemString(xp->x_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098}
99
Guido van Rossum03093a21994-09-28 15:51:32 +0000100static PyTypeObject Xxtype = {
101 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 0, /*ob_size*/
103 "xx", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000104 sizeof(xxobject), /*tp_basicsize*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105 0, /*tp_itemsize*/
106 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000107 (destructor)xx_dealloc, /*tp_dealloc*/
Guido van Rossum70d7a311992-08-14 12:04:19 +0000108 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000109 (getattrfunc)xx_getattr, /*tp_getattr*/
110 (setattrfunc)xx_setattr, /*tp_setattr*/
Guido van Rossum70d7a311992-08-14 12:04:19 +0000111 0, /*tp_compare*/
112 0, /*tp_repr*/
113 0, /*tp_as_number*/
114 0, /*tp_as_sequence*/
115 0, /*tp_as_mapping*/
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000116 0, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117};