2 /* 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. */
18 PyObject
*x_attr
; /* Attributes dictionary */
21 staticforward PyTypeObject Xxtype
;
23 #define is_xxobject(v) ((v)->ob_type == &Xxtype)
26 newxxobject(PyObject
*arg
)
29 xp
= PyObject_New(xxobject
, &Xxtype
);
39 xx_dealloc(xxobject
*xp
)
41 Py_XDECREF(xp
->x_attr
);
46 xx_demo(xxobject
*self
, PyObject
*args
)
48 if (!PyArg_ParseTuple(args
, ":demo"))
54 static PyMethodDef xx_methods
[] = {
55 {"demo", (PyCFunction
)xx_demo
, METH_VARARGS
},
56 {NULL
, NULL
} /* sentinel */
60 xx_getattr(xxobject
*xp
, char *name
)
62 if (xp
->x_attr
!= NULL
) {
63 PyObject
*v
= PyDict_GetItemString(xp
->x_attr
, name
);
69 return Py_FindMethod(xx_methods
, (PyObject
*)xp
, name
);
73 xx_setattr(xxobject
*xp
, char *name
, PyObject
*v
)
75 if (xp
->x_attr
== NULL
) {
76 xp
->x_attr
= PyDict_New();
77 if (xp
->x_attr
== NULL
)
81 int rv
= PyDict_DelItemString(xp
->x_attr
, name
);
83 PyErr_SetString(PyExc_AttributeError
,
84 "delete non-existing xx attribute");
88 return PyDict_SetItemString(xp
->x_attr
, name
, v
);
91 static PyTypeObject Xxtype
= {
92 PyObject_HEAD_INIT(&PyType_Type
)
95 sizeof(xxobject
), /*tp_basicsize*/
98 (destructor
)xx_dealloc
, /*tp_dealloc*/
100 (getattrfunc
)xx_getattr
, /*tp_getattr*/
101 (setattrfunc
)xx_setattr
, /*tp_setattr*/
105 0, /*tp_as_sequence*/