2 /* Use this file as a template to start implementing a module that
3 also declares object types. All occurrences of 'Xxo' should be changed
4 to something reasonable for your objects. After that, all other
5 occurrences of 'xx' should be changed to something reasonable for your
6 module. If your module is named foo your sourcefile should be named
9 You will probably want to delete all references to 'x_attr' and add
10 your own types of attributes instead. Maybe you want to name your
11 local variables other than 'self'. If your object type is needed in
12 other files, you'll have to create a file "foobarobject.h"; see
13 intobject.h for an example. */
19 static PyObject
*ErrorObject
;
23 PyObject
*x_attr
; /* Attributes dictionary */
26 staticforward PyTypeObject Xxo_Type
;
28 #define XxoObject_Check(v) ((v)->ob_type == &Xxo_Type)
31 newXxoObject(PyObject
*arg
)
34 self
= PyObject_New(XxoObject
, &Xxo_Type
);
44 Xxo_dealloc(XxoObject
*self
)
46 Py_XDECREF(self
->x_attr
);
51 Xxo_demo(XxoObject
*self
, PyObject
*args
)
53 if (!PyArg_ParseTuple(args
, ":demo"))
59 static PyMethodDef Xxo_methods
[] = {
60 {"demo", (PyCFunction
)Xxo_demo
, METH_VARARGS
},
61 {NULL
, NULL
} /* sentinel */
65 Xxo_getattr(XxoObject
*self
, char *name
)
67 if (self
->x_attr
!= NULL
) {
68 PyObject
*v
= PyDict_GetItemString(self
->x_attr
, name
);
74 return Py_FindMethod(Xxo_methods
, (PyObject
*)self
, name
);
78 Xxo_setattr(XxoObject
*self
, char *name
, PyObject
*v
)
80 if (self
->x_attr
== NULL
) {
81 self
->x_attr
= PyDict_New();
82 if (self
->x_attr
== NULL
)
86 int rv
= PyDict_DelItemString(self
->x_attr
, name
);
88 PyErr_SetString(PyExc_AttributeError
,
89 "delete non-existing Xxo attribute");
93 return PyDict_SetItemString(self
->x_attr
, name
, v
);
96 statichere PyTypeObject Xxo_Type
= {
97 /* The ob_type field must be initialized in the module init function
98 * to be portable to Windows without using C++. */
99 PyObject_HEAD_INIT(NULL
)
102 sizeof(XxoObject
), /*tp_basicsize*/
105 (destructor
)Xxo_dealloc
, /*tp_dealloc*/
107 (getattrfunc
)Xxo_getattr
, /*tp_getattr*/
108 (setattrfunc
)Xxo_setattr
, /*tp_setattr*/
112 0, /*tp_as_sequence*/
116 /* --------------------------------------------------------------------- */
118 /* Function of two integers returning integer */
121 xx_foo(PyObject
*self
, PyObject
*args
)
125 if (!PyArg_ParseTuple(args
, "ll:foo", &i
, &j
))
127 res
= i
+j
; /* XXX Do something here */
128 return PyInt_FromLong(res
);
132 /* Function of no arguments returning new Xxo object */
135 xx_new(PyObject
*self
, PyObject
*args
)
139 if (!PyArg_ParseTuple(args
, ":new"))
141 rv
= newXxoObject(args
);
144 return (PyObject
*)rv
;
147 /* Example with subtle bug from extensions manual ("Thin Ice"). */
150 xx_bug(PyObject
*self
, PyObject
*args
)
152 PyObject
*list
, *item
;
154 if (!PyArg_ParseTuple(args
, "O:bug", &list
))
157 item
= PyList_GetItem(list
, 0);
158 /* Py_INCREF(item); */
159 PyList_SetItem(list
, 1, PyInt_FromLong(0L));
160 PyObject_Print(item
, stdout
, 0);
162 /* Py_DECREF(item); */
168 /* Test bad format character */
171 xx_roj(PyObject
*self
, PyObject
*args
)
175 if (!PyArg_ParseTuple(args
, "O#:roj", &a
, &b
))
182 /* List of functions defined in the module */
184 static PyMethodDef xx_methods
[] = {
185 {"roj", xx_roj
, METH_VARARGS
},
186 {"foo", xx_foo
, METH_VARARGS
},
187 {"new", xx_new
, METH_VARARGS
},
188 {"bug", xx_bug
, METH_VARARGS
},
189 {NULL
, NULL
} /* sentinel */
193 /* Initialization function for the module (*must* be called initxx) */
200 /* Initialize the type of the new type object here; doing it here
201 * is required for portability to Windows without requiring C++. */
202 Xxo_Type
.ob_type
= &PyType_Type
;
204 /* Create the module and add the functions */
205 m
= Py_InitModule("xx", xx_methods
);
207 /* Add some symbolic constants to the module */
208 d
= PyModule_GetDict(m
);
209 ErrorObject
= PyErr_NewException("xx.error", NULL
, NULL
);
210 PyDict_SetItemString(d
, "error", ErrorObject
);