ยปCore Development>Code coverage>Doc/includes/noddy4.c

Python code coverage for Doc/includes/noddy4.c

#countcontent
1n/a#include <Python.h>
2n/a#include "structmember.h"
3n/a
4n/atypedef struct {
5n/a PyObject_HEAD
6n/a PyObject *first;
7n/a PyObject *last;
8n/a int number;
9n/a} Noddy;
10n/a
11n/astatic int
12n/aNoddy_traverse(Noddy *self, visitproc visit, void *arg)
13n/a{
14n/a int vret;
15n/a
16n/a if (self->first) {
17n/a vret = visit(self->first, arg);
18n/a if (vret != 0)
19n/a return vret;
20n/a }
21n/a if (self->last) {
22n/a vret = visit(self->last, arg);
23n/a if (vret != 0)
24n/a return vret;
25n/a }
26n/a
27n/a return 0;
28n/a}
29n/a
30n/astatic int
31n/aNoddy_clear(Noddy *self)
32n/a{
33n/a PyObject *tmp;
34n/a
35n/a tmp = self->first;
36n/a self->first = NULL;
37n/a Py_XDECREF(tmp);
38n/a
39n/a tmp = self->last;
40n/a self->last = NULL;
41n/a Py_XDECREF(tmp);
42n/a
43n/a return 0;
44n/a}
45n/a
46n/astatic void
47n/aNoddy_dealloc(Noddy* self)
48n/a{
49n/a Noddy_clear(self);
50n/a Py_TYPE(self)->tp_free((PyObject*)self);
51n/a}
52n/a
53n/astatic PyObject *
54n/aNoddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
55n/a{
56n/a Noddy *self;
57n/a
58n/a self = (Noddy *)type->tp_alloc(type, 0);
59n/a if (self != NULL) {
60n/a self->first = PyUnicode_FromString("");
61n/a if (self->first == NULL) {
62n/a Py_DECREF(self);
63n/a return NULL;
64n/a }
65n/a
66n/a self->last = PyUnicode_FromString("");
67n/a if (self->last == NULL) {
68n/a Py_DECREF(self);
69n/a return NULL;
70n/a }
71n/a
72n/a self->number = 0;
73n/a }
74n/a
75n/a return (PyObject *)self;
76n/a}
77n/a
78n/astatic int
79n/aNoddy_init(Noddy *self, PyObject *args, PyObject *kwds)
80n/a{
81n/a PyObject *first=NULL, *last=NULL, *tmp;
82n/a
83n/a static char *kwlist[] = {"first", "last", "number", NULL};
84n/a
85n/a if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
86n/a &first, &last,
87n/a &self->number))
88n/a return -1;
89n/a
90n/a if (first) {
91n/a tmp = self->first;
92n/a Py_INCREF(first);
93n/a self->first = first;
94n/a Py_XDECREF(tmp);
95n/a }
96n/a
97n/a if (last) {
98n/a tmp = self->last;
99n/a Py_INCREF(last);
100n/a self->last = last;
101n/a Py_XDECREF(tmp);
102n/a }
103n/a
104n/a return 0;
105n/a}
106n/a
107n/a
108n/astatic PyMemberDef Noddy_members[] = {
109n/a {"first", T_OBJECT_EX, offsetof(Noddy, first), 0,
110n/a "first name"},
111n/a {"last", T_OBJECT_EX, offsetof(Noddy, last), 0,
112n/a "last name"},
113n/a {"number", T_INT, offsetof(Noddy, number), 0,
114n/a "noddy number"},
115n/a {NULL} /* Sentinel */
116n/a};
117n/a
118n/astatic PyObject *
119n/aNoddy_name(Noddy* self)
120n/a{
121n/a if (self->first == NULL) {
122n/a PyErr_SetString(PyExc_AttributeError, "first");
123n/a return NULL;
124n/a }
125n/a
126n/a if (self->last == NULL) {
127n/a PyErr_SetString(PyExc_AttributeError, "last");
128n/a return NULL;
129n/a }
130n/a
131n/a return PyUnicode_FromFormat("%S %S", self->first, self->last);
132n/a}
133n/a
134n/astatic PyMethodDef Noddy_methods[] = {
135n/a {"name", (PyCFunction)Noddy_name, METH_NOARGS,
136n/a "Return the name, combining the first and last name"
137n/a },
138n/a {NULL} /* Sentinel */
139n/a};
140n/a
141n/astatic PyTypeObject NoddyType = {
142n/a PyVarObject_HEAD_INIT(NULL, 0)
143n/a "noddy.Noddy", /* tp_name */
144n/a sizeof(Noddy), /* tp_basicsize */
145n/a 0, /* tp_itemsize */
146n/a (destructor)Noddy_dealloc, /* tp_dealloc */
147n/a 0, /* tp_print */
148n/a 0, /* tp_getattr */
149n/a 0, /* tp_setattr */
150n/a 0, /* tp_reserved */
151n/a 0, /* tp_repr */
152n/a 0, /* tp_as_number */
153n/a 0, /* tp_as_sequence */
154n/a 0, /* tp_as_mapping */
155n/a 0, /* tp_hash */
156n/a 0, /* tp_call */
157n/a 0, /* tp_str */
158n/a 0, /* tp_getattro */
159n/a 0, /* tp_setattro */
160n/a 0, /* tp_as_buffer */
161n/a Py_TPFLAGS_DEFAULT |
162n/a Py_TPFLAGS_BASETYPE |
163n/a Py_TPFLAGS_HAVE_GC, /* tp_flags */
164n/a "Noddy objects", /* tp_doc */
165n/a (traverseproc)Noddy_traverse, /* tp_traverse */
166n/a (inquiry)Noddy_clear, /* tp_clear */
167n/a 0, /* tp_richcompare */
168n/a 0, /* tp_weaklistoffset */
169n/a 0, /* tp_iter */
170n/a 0, /* tp_iternext */
171n/a Noddy_methods, /* tp_methods */
172n/a Noddy_members, /* tp_members */
173n/a 0, /* tp_getset */
174n/a 0, /* tp_base */
175n/a 0, /* tp_dict */
176n/a 0, /* tp_descr_get */
177n/a 0, /* tp_descr_set */
178n/a 0, /* tp_dictoffset */
179n/a (initproc)Noddy_init, /* tp_init */
180n/a 0, /* tp_alloc */
181n/a Noddy_new, /* tp_new */
182n/a};
183n/a
184n/astatic PyModuleDef noddy4module = {
185n/a PyModuleDef_HEAD_INIT,
186n/a "noddy4",
187n/a "Example module that creates an extension type.",
188n/a -1,
189n/a NULL, NULL, NULL, NULL, NULL
190n/a};
191n/a
192n/aPyMODINIT_FUNC
193n/aPyInit_noddy4(void)
194n/a{
195n/a PyObject* m;
196n/a
197n/a if (PyType_Ready(&NoddyType) < 0)
198n/a return NULL;
199n/a
200n/a m = PyModule_Create(&noddy4module);
201n/a if (m == NULL)
202n/a return NULL;
203n/a
204n/a Py_INCREF(&NoddyType);
205n/a PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);
206n/a return m;
207n/a}