ยปCore Development>Code coverage>Modules/xxlimited.c

Python code coverage for Modules/xxlimited.c

#countcontent
1n/a
2n/a/* Use this file as a template to start implementing a module that
3n/a also declares object types. All occurrences of 'Xxo' should be changed
4n/a to something reasonable for your objects. After that, all other
5n/a occurrences of 'xx' should be changed to something reasonable for your
6n/a module. If your module is named foo your sourcefile should be named
7n/a foomodule.c.
8n/a
9n/a You will probably want to delete all references to 'x_attr' and add
10n/a your own types of attributes instead. Maybe you want to name your
11n/a local variables other than 'self'. If your object type is needed in
12n/a other files, you'll have to create a file "foobarobject.h"; see
13n/a floatobject.h for an example. */
14n/a
15n/a/* Xxo objects */
16n/a
17n/a#include "Python.h"
18n/a
19n/astatic PyObject *ErrorObject;
20n/a
21n/atypedef struct {
22n/a PyObject_HEAD
23n/a PyObject *x_attr; /* Attributes dictionary */
24n/a} XxoObject;
25n/a
26n/astatic PyObject *Xxo_Type;
27n/a
28n/a#define XxoObject_Check(v) (Py_TYPE(v) == Xxo_Type)
29n/a
30n/astatic XxoObject *
31n/anewXxoObject(PyObject *arg)
32n/a{
33n/a XxoObject *self;
34n/a self = PyObject_GC_New(XxoObject, (PyTypeObject*)Xxo_Type);
35n/a if (self == NULL)
36n/a return NULL;
37n/a self->x_attr = NULL;
38n/a return self;
39n/a}
40n/a
41n/a/* Xxo methods */
42n/a
43n/astatic int
44n/aXxo_traverse(XxoObject *self, visitproc visit, void *arg)
45n/a{
46n/a Py_VISIT(self->x_attr);
47n/a return 0;
48n/a}
49n/a
50n/astatic int
51n/aXxo_finalize(XxoObject *self)
52n/a{
53n/a Py_CLEAR(self->x_attr);
54n/a return 0;
55n/a}
56n/a
57n/astatic PyObject *
58n/aXxo_demo(XxoObject *self, PyObject *args)
59n/a{
60n/a PyObject *o = NULL;
61n/a if (!PyArg_ParseTuple(args, "|O:demo", &o))
62n/a return NULL;
63n/a /* Test availability of fast type checks */
64n/a if (o != NULL && PyUnicode_Check(o)) {
65n/a Py_INCREF(o);
66n/a return o;
67n/a }
68n/a Py_INCREF(Py_None);
69n/a return Py_None;
70n/a}
71n/a
72n/astatic PyMethodDef Xxo_methods[] = {
73n/a {"demo", (PyCFunction)Xxo_demo, METH_VARARGS,
74n/a PyDoc_STR("demo() -> None")},
75n/a {NULL, NULL} /* sentinel */
76n/a};
77n/a
78n/astatic PyObject *
79n/aXxo_getattro(XxoObject *self, PyObject *name)
80n/a{
81n/a if (self->x_attr != NULL) {
82n/a PyObject *v = PyDict_GetItem(self->x_attr, name);
83n/a if (v != NULL) {
84n/a Py_INCREF(v);
85n/a return v;
86n/a }
87n/a }
88n/a return PyObject_GenericGetAttr((PyObject *)self, name);
89n/a}
90n/a
91n/astatic int
92n/aXxo_setattr(XxoObject *self, const char *name, PyObject *v)
93n/a{
94n/a if (self->x_attr == NULL) {
95n/a self->x_attr = PyDict_New();
96n/a if (self->x_attr == NULL)
97n/a return -1;
98n/a }
99n/a if (v == NULL) {
100n/a int rv = PyDict_DelItemString(self->x_attr, name);
101n/a if (rv < 0)
102n/a PyErr_SetString(PyExc_AttributeError,
103n/a "delete non-existing Xxo attribute");
104n/a return rv;
105n/a }
106n/a else
107n/a return PyDict_SetItemString(self->x_attr, name, v);
108n/a}
109n/a
110n/astatic PyType_Slot Xxo_Type_slots[] = {
111n/a {Py_tp_doc, "The Xxo type"},
112n/a {Py_tp_traverse, Xxo_traverse},
113n/a {Py_tp_finalize, Xxo_finalize},
114n/a {Py_tp_getattro, Xxo_getattro},
115n/a {Py_tp_setattr, Xxo_setattr},
116n/a {Py_tp_methods, Xxo_methods},
117n/a {0, 0},
118n/a};
119n/a
120n/astatic PyType_Spec Xxo_Type_spec = {
121n/a "xxlimited.Xxo",
122n/a sizeof(XxoObject),
123n/a 0,
124n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE,
125n/a Xxo_Type_slots
126n/a};
127n/a
128n/a/* --------------------------------------------------------------------- */
129n/a
130n/a/* Function of two integers returning integer */
131n/a
132n/aPyDoc_STRVAR(xx_foo_doc,
133n/a"foo(i,j)\n\
134n/a\n\
135n/aReturn the sum of i and j.");
136n/a
137n/astatic PyObject *
138n/axx_foo(PyObject *self, PyObject *args)
139n/a{
140n/a long i, j;
141n/a long res;
142n/a if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
143n/a return NULL;
144n/a res = i+j; /* XXX Do something here */
145n/a return PyLong_FromLong(res);
146n/a}
147n/a
148n/a
149n/a/* Function of no arguments returning new Xxo object */
150n/a
151n/astatic PyObject *
152n/axx_new(PyObject *self, PyObject *args)
153n/a{
154n/a XxoObject *rv;
155n/a
156n/a if (!PyArg_ParseTuple(args, ":new"))
157n/a return NULL;
158n/a rv = newXxoObject(args);
159n/a if (rv == NULL)
160n/a return NULL;
161n/a return (PyObject *)rv;
162n/a}
163n/a
164n/a/* Test bad format character */
165n/a
166n/astatic PyObject *
167n/axx_roj(PyObject *self, PyObject *args)
168n/a{
169n/a PyObject *a;
170n/a long b;
171n/a if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
172n/a return NULL;
173n/a Py_INCREF(Py_None);
174n/a return Py_None;
175n/a}
176n/a
177n/a
178n/a/* ---------- */
179n/a
180n/astatic PyType_Slot Str_Type_slots[] = {
181n/a {Py_tp_base, NULL}, /* filled out in module init function */
182n/a {0, 0},
183n/a};
184n/a
185n/astatic PyType_Spec Str_Type_spec = {
186n/a "xxlimited.Str",
187n/a 0,
188n/a 0,
189n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
190n/a Str_Type_slots
191n/a};
192n/a
193n/a/* ---------- */
194n/a
195n/astatic PyObject *
196n/anull_richcompare(PyObject *self, PyObject *other, int op)
197n/a{
198n/a Py_RETURN_NOTIMPLEMENTED;
199n/a}
200n/a
201n/astatic PyType_Slot Null_Type_slots[] = {
202n/a {Py_tp_base, NULL}, /* filled out in module init */
203n/a {Py_tp_new, NULL},
204n/a {Py_tp_richcompare, null_richcompare},
205n/a {0, 0}
206n/a};
207n/a
208n/astatic PyType_Spec Null_Type_spec = {
209n/a "xxlimited.Null",
210n/a 0, /* basicsize */
211n/a 0, /* itemsize */
212n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
213n/a Null_Type_slots
214n/a};
215n/a
216n/a/* ---------- */
217n/a
218n/a/* List of functions defined in the module */
219n/a
220n/astatic PyMethodDef xx_methods[] = {
221n/a {"roj", xx_roj, METH_VARARGS,
222n/a PyDoc_STR("roj(a,b) -> None")},
223n/a {"foo", xx_foo, METH_VARARGS,
224n/a xx_foo_doc},
225n/a {"new", xx_new, METH_VARARGS,
226n/a PyDoc_STR("new() -> new Xx object")},
227n/a {NULL, NULL} /* sentinel */
228n/a};
229n/a
230n/aPyDoc_STRVAR(module_doc,
231n/a"This is a template module just for instruction.");
232n/a
233n/astatic int
234n/axx_modexec(PyObject *m)
235n/a{
236n/a PyObject *o;
237n/a
238n/a /* Due to cross platform compiler issues the slots must be filled
239n/a * here. It's required for portability to Windows without requiring
240n/a * C++. */
241n/a Null_Type_slots[0].pfunc = &PyBaseObject_Type;
242n/a Null_Type_slots[1].pfunc = PyType_GenericNew;
243n/a Str_Type_slots[0].pfunc = &PyUnicode_Type;
244n/a
245n/a Xxo_Type = PyType_FromSpec(&Xxo_Type_spec);
246n/a if (Xxo_Type == NULL)
247n/a goto fail;
248n/a
249n/a /* Add some symbolic constants to the module */
250n/a if (ErrorObject == NULL) {
251n/a ErrorObject = PyErr_NewException("xxlimited.error", NULL, NULL);
252n/a if (ErrorObject == NULL)
253n/a goto fail;
254n/a }
255n/a Py_INCREF(ErrorObject);
256n/a PyModule_AddObject(m, "error", ErrorObject);
257n/a
258n/a /* Add Xxo */
259n/a o = PyType_FromSpec(&Xxo_Type_spec);
260n/a if (o == NULL)
261n/a goto fail;
262n/a PyModule_AddObject(m, "Xxo", o);
263n/a
264n/a /* Add Str */
265n/a o = PyType_FromSpec(&Str_Type_spec);
266n/a if (o == NULL)
267n/a goto fail;
268n/a PyModule_AddObject(m, "Str", o);
269n/a
270n/a /* Add Null */
271n/a o = PyType_FromSpec(&Null_Type_spec);
272n/a if (o == NULL)
273n/a goto fail;
274n/a PyModule_AddObject(m, "Null", o);
275n/a return 0;
276n/a fail:
277n/a Py_XDECREF(m);
278n/a return -1;
279n/a}
280n/a
281n/a
282n/astatic PyModuleDef_Slot xx_slots[] = {
283n/a {Py_mod_exec, xx_modexec},
284n/a {0, NULL}
285n/a};
286n/a
287n/astatic struct PyModuleDef xxmodule = {
288n/a PyModuleDef_HEAD_INIT,
289n/a "xxlimited",
290n/a module_doc,
291n/a 0,
292n/a xx_methods,
293n/a xx_slots,
294n/a NULL,
295n/a NULL,
296n/a NULL
297n/a};
298n/a
299n/a/* Export function for the module (*must* be called PyInit_xx) */
300n/a
301n/aPyMODINIT_FUNC
302n/aPyInit_xxlimited(void)
303n/a{
304n/a return PyModuleDef_Init(&xxmodule);
305n/a}