- Got rid of newmodule.c
[python/dscho.git] / Objects / xxobject.c
blob117714a7b67ec1ce01b57e7d365a73845f07e95c
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. */
12 /* Xx objects */
14 #include "Python.h"
16 typedef struct {
17 PyObject_HEAD
18 PyObject *x_attr; /* Attributes dictionary */
19 } xxobject;
21 staticforward PyTypeObject Xxtype;
23 #define is_xxobject(v) ((v)->ob_type == &Xxtype)
25 static xxobject *
26 newxxobject(PyObject *arg)
28 xxobject *xp;
29 xp = PyObject_New(xxobject, &Xxtype);
30 if (xp == NULL)
31 return NULL;
32 xp->x_attr = NULL;
33 return xp;
36 /* Xx methods */
38 static void
39 xx_dealloc(xxobject *xp)
41 Py_XDECREF(xp->x_attr);
42 PyObject_Del(xp);
45 static PyObject *
46 xx_demo(xxobject *self, PyObject *args)
48 if (!PyArg_ParseTuple(args, ":demo"))
49 return NULL;
50 Py_INCREF(Py_None);
51 return Py_None;
54 static PyMethodDef xx_methods[] = {
55 {"demo", (PyCFunction)xx_demo, METH_VARARGS},
56 {NULL, NULL} /* sentinel */
59 static PyObject *
60 xx_getattr(xxobject *xp, char *name)
62 if (xp->x_attr != NULL) {
63 PyObject *v = PyDict_GetItemString(xp->x_attr, name);
64 if (v != NULL) {
65 Py_INCREF(v);
66 return v;
69 return Py_FindMethod(xx_methods, (PyObject *)xp, name);
72 static int
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)
78 return -1;
80 if (v == NULL) {
81 int rv = PyDict_DelItemString(xp->x_attr, name);
82 if (rv < 0)
83 PyErr_SetString(PyExc_AttributeError,
84 "delete non-existing xx attribute");
85 return rv;
87 else
88 return PyDict_SetItemString(xp->x_attr, name, v);
91 static PyTypeObject Xxtype = {
92 PyObject_HEAD_INIT(&PyType_Type)
93 0, /*ob_size*/
94 "xx", /*tp_name*/
95 sizeof(xxobject), /*tp_basicsize*/
96 0, /*tp_itemsize*/
97 /* methods */
98 (destructor)xx_dealloc, /*tp_dealloc*/
99 0, /*tp_print*/
100 (getattrfunc)xx_getattr, /*tp_getattr*/
101 (setattrfunc)xx_setattr, /*tp_setattr*/
102 0, /*tp_compare*/
103 0, /*tp_repr*/
104 0, /*tp_as_number*/
105 0, /*tp_as_sequence*/
106 0, /*tp_as_mapping*/
107 0, /*tp_hash*/