blob: 21a00b09e19d2fe6df49883a83b96aed3ad2ca3f [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum320a5cc1991-01-02 15:11:48 +00002/* Use this file as a template to start implementing a new object type.
3 If your objects will be called foobar, start by copying this file to
4 foobarobject.c, changing all occurrences of xx to foobar and all
5 occurrences of Xx by Foobar. You will probably want to delete all
6 references to 'x_attr' and add your own types of attributes
7 instead. Maybe you want to name your local variables other than
8 'xp'. If your object type is needed in other files, you'll have to
9 create a file "foobarobject.h"; see intobject.h for an example. */
10
11
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012/* Xx objects */
13
Guido van Rossum03093a21994-09-28 15:51:32 +000014#include "Python.h"
Guido van Rossum320a5cc1991-01-02 15:11:48 +000015
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000016typedef struct {
Guido van Rossum03093a21994-09-28 15:51:32 +000017 PyObject_HEAD
18 PyObject *x_attr; /* Attributes dictionary */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019} xxobject;
20
Guido van Rossum03093a21994-09-28 15:51:32 +000021staticforward PyTypeObject Xxtype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Guido van Rossum320a5cc1991-01-02 15:11:48 +000023#define is_xxobject(v) ((v)->ob_type == &Xxtype)
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025static xxobject *
Fred Drakeba096332000-07-09 07:04:36 +000026newxxobject(PyObject *arg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027{
Guido van Rossum94726d51991-01-02 15:12:51 +000028 xxobject *xp;
Guido van Rossum03093a21994-09-28 15:51:32 +000029 xp = PyObject_NEW(xxobject, &Xxtype);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030 if (xp == NULL)
31 return NULL;
32 xp->x_attr = NULL;
33 return xp;
34}
35
36/* Xx methods */
37
38static void
Fred Drakeba096332000-07-09 07:04:36 +000039xx_dealloc(xxobject *xp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040{
Guido van Rossum03093a21994-09-28 15:51:32 +000041 Py_XDECREF(xp->x_attr);
Guido van Rossumb18618d2000-05-03 23:44:39 +000042 PyObject_DEL(xp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043}
44
Guido van Rossum03093a21994-09-28 15:51:32 +000045static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +000046xx_demo(xxobject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047{
Fred Drakeba096332000-07-09 07:04:36 +000048 if (!PyArg_ParseTuple(args, ":demo"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049 return NULL;
Guido van Rossum03093a21994-09-28 15:51:32 +000050 Py_INCREF(Py_None);
51 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052}
53
Guido van Rossum00d225e1999-01-29 14:39:12 +000054static PyMethodDef xx_methods[] = {
Fred Drakeba096332000-07-09 07:04:36 +000055 {"demo", (PyCFunction)xx_demo, METH_VARARGS},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056 {NULL, NULL} /* sentinel */
57};
58
Guido van Rossum03093a21994-09-28 15:51:32 +000059static PyObject *
Fred Drakeba096332000-07-09 07:04:36 +000060xx_getattr(xxobject *xp, char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061{
62 if (xp->x_attr != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000063 PyObject *v = PyDict_GetItemString(xp->x_attr, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 if (v != NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000065 Py_INCREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 return v;
67 }
68 }
Guido van Rossum03093a21994-09-28 15:51:32 +000069 return Py_FindMethod(xx_methods, (PyObject *)xp, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070}
71
72static int
Fred Drakeba096332000-07-09 07:04:36 +000073xx_setattr(xxobject *xp, char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074{
75 if (xp->x_attr == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000076 xp->x_attr = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077 if (xp->x_attr == NULL)
Guido van Rossum320a5cc1991-01-02 15:11:48 +000078 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079 }
Guido van Rossum94472a01992-09-04 09:45:18 +000080 if (v == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +000081 int rv = PyDict_DelItemString(xp->x_attr, name);
Guido van Rossum94472a01992-09-04 09:45:18 +000082 if (rv < 0)
Guido van Rossum03093a21994-09-28 15:51:32 +000083 PyErr_SetString(PyExc_AttributeError,
Fred Drakeba096332000-07-09 07:04:36 +000084 "delete non-existing xx attribute");
Guido van Rossum94472a01992-09-04 09:45:18 +000085 return rv;
86 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087 else
Guido van Rossum03093a21994-09-28 15:51:32 +000088 return PyDict_SetItemString(xp->x_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089}
90
Guido van Rossum03093a21994-09-28 15:51:32 +000091static PyTypeObject Xxtype = {
92 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 0, /*ob_size*/
94 "xx", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +000095 sizeof(xxobject), /*tp_basicsize*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096 0, /*tp_itemsize*/
97 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +000098 (destructor)xx_dealloc, /*tp_dealloc*/
Guido van Rossum70d7a311992-08-14 12:04:19 +000099 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000100 (getattrfunc)xx_getattr, /*tp_getattr*/
101 (setattrfunc)xx_setattr, /*tp_setattr*/
Guido van Rossum70d7a311992-08-14 12:04:19 +0000102 0, /*tp_compare*/
103 0, /*tp_repr*/
104 0, /*tp_as_number*/
105 0, /*tp_as_sequence*/
106 0, /*tp_as_mapping*/
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000107 0, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108};