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

Python code coverage for Doc/includes/noddy3.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 void
12n/aNoddy_dealloc(Noddy* self)
13n/a{
14n/a Py_XDECREF(self->first);
15n/a Py_XDECREF(self->last);
16n/a Py_TYPE(self)->tp_free((PyObject*)self);
17n/a}
18n/a
19n/astatic PyObject *
20n/aNoddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
21n/a{
22n/a Noddy *self;
23n/a
24n/a self = (Noddy *)type->tp_alloc(type, 0);
25n/a if (self != NULL) {
26n/a self->first = PyUnicode_FromString("");
27n/a if (self->first == NULL) {
28n/a Py_DECREF(self);
29n/a return NULL;
30n/a }
31n/a
32n/a self->last = PyUnicode_FromString("");
33n/a if (self->last == NULL) {
34n/a Py_DECREF(self);
35n/a return NULL;
36n/a }
37n/a
38n/a self->number = 0;
39n/a }
40n/a
41n/a return (PyObject *)self;
42n/a}
43n/a
44n/astatic int
45n/aNoddy_init(Noddy *self, PyObject *args, PyObject *kwds)
46n/a{
47n/a PyObject *first=NULL, *last=NULL, *tmp;
48n/a
49n/a static char *kwlist[] = {"first", "last", "number", NULL};
50n/a
51n/a if (! PyArg_ParseTupleAndKeywords(args, kwds, "|SSi", kwlist,
52n/a &first, &last,
53n/a &self->number))
54n/a return -1;
55n/a
56n/a if (first) {
57n/a tmp = self->first;
58n/a Py_INCREF(first);
59n/a self->first = first;
60n/a Py_DECREF(tmp);
61n/a }
62n/a
63n/a if (last) {
64n/a tmp = self->last;
65n/a Py_INCREF(last);
66n/a self->last = last;
67n/a Py_DECREF(tmp);
68n/a }
69n/a
70n/a return 0;
71n/a}
72n/a
73n/astatic PyMemberDef Noddy_members[] = {
74n/a {"number", T_INT, offsetof(Noddy, number), 0,
75n/a "noddy number"},
76n/a {NULL} /* Sentinel */
77n/a};
78n/a
79n/astatic PyObject *
80n/aNoddy_getfirst(Noddy *self, void *closure)
81n/a{
82n/a Py_INCREF(self->first);
83n/a return self->first;
84n/a}
85n/a
86n/astatic int
87n/aNoddy_setfirst(Noddy *self, PyObject *value, void *closure)
88n/a{
89n/a if (value == NULL) {
90n/a PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
91n/a return -1;
92n/a }
93n/a
94n/a if (! PyUnicode_Check(value)) {
95n/a PyErr_SetString(PyExc_TypeError,
96n/a "The first attribute value must be a string");
97n/a return -1;
98n/a }
99n/a
100n/a Py_DECREF(self->first);
101n/a Py_INCREF(value);
102n/a self->first = value;
103n/a
104n/a return 0;
105n/a}
106n/a
107n/astatic PyObject *
108n/aNoddy_getlast(Noddy *self, void *closure)
109n/a{
110n/a Py_INCREF(self->last);
111n/a return self->last;
112n/a}
113n/a
114n/astatic int
115n/aNoddy_setlast(Noddy *self, PyObject *value, void *closure)
116n/a{
117n/a if (value == NULL) {
118n/a PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
119n/a return -1;
120n/a }
121n/a
122n/a if (! PyUnicode_Check(value)) {
123n/a PyErr_SetString(PyExc_TypeError,
124n/a "The last attribute value must be a string");
125n/a return -1;
126n/a }
127n/a
128n/a Py_DECREF(self->last);
129n/a Py_INCREF(value);
130n/a self->last = value;
131n/a
132n/a return 0;
133n/a}
134n/a
135n/astatic PyGetSetDef Noddy_getseters[] = {
136n/a {"first",
137n/a (getter)Noddy_getfirst, (setter)Noddy_setfirst,
138n/a "first name",
139n/a NULL},
140n/a {"last",
141n/a (getter)Noddy_getlast, (setter)Noddy_setlast,
142n/a "last name",
143n/a NULL},
144n/a {NULL} /* Sentinel */
145n/a};
146n/a
147n/astatic PyObject *
148n/aNoddy_name(Noddy* self)
149n/a{
150n/a return PyUnicode_FromFormat("%S %S", self->first, self->last);
151n/a}
152n/a
153n/astatic PyMethodDef Noddy_methods[] = {
154n/a {"name", (PyCFunction)Noddy_name, METH_NOARGS,
155n/a "Return the name, combining the first and last name"
156n/a },
157n/a {NULL} /* Sentinel */
158n/a};
159n/a
160n/astatic PyTypeObject NoddyType = {
161n/a PyVarObject_HEAD_INIT(NULL, 0)
162n/a "noddy.Noddy", /* tp_name */
163n/a sizeof(Noddy), /* tp_basicsize */
164n/a 0, /* tp_itemsize */
165n/a (destructor)Noddy_dealloc, /* tp_dealloc */
166n/a 0, /* tp_print */
167n/a 0, /* tp_getattr */
168n/a 0, /* tp_setattr */
169n/a 0, /* tp_reserved */
170n/a 0, /* tp_repr */
171n/a 0, /* tp_as_number */
172n/a 0, /* tp_as_sequence */
173n/a 0, /* tp_as_mapping */
174n/a 0, /* tp_hash */
175n/a 0, /* tp_call */
176n/a 0, /* tp_str */
177n/a 0, /* tp_getattro */
178n/a 0, /* tp_setattro */
179n/a 0, /* tp_as_buffer */
180n/a Py_TPFLAGS_DEFAULT |
181n/a Py_TPFLAGS_BASETYPE, /* tp_flags */
182n/a "Noddy objects", /* tp_doc */
183n/a 0, /* tp_traverse */
184n/a 0, /* tp_clear */
185n/a 0, /* tp_richcompare */
186n/a 0, /* tp_weaklistoffset */
187n/a 0, /* tp_iter */
188n/a 0, /* tp_iternext */
189n/a Noddy_methods, /* tp_methods */
190n/a Noddy_members, /* tp_members */
191n/a Noddy_getseters, /* tp_getset */
192n/a 0, /* tp_base */
193n/a 0, /* tp_dict */
194n/a 0, /* tp_descr_get */
195n/a 0, /* tp_descr_set */
196n/a 0, /* tp_dictoffset */
197n/a (initproc)Noddy_init, /* tp_init */
198n/a 0, /* tp_alloc */
199n/a Noddy_new, /* tp_new */
200n/a};
201n/a
202n/astatic PyModuleDef noddy3module = {
203n/a PyModuleDef_HEAD_INIT,
204n/a "noddy3",
205n/a "Example module that creates an extension type.",
206n/a -1,
207n/a NULL, NULL, NULL, NULL, NULL
208n/a};
209n/a
210n/aPyMODINIT_FUNC
211n/aPyInit_noddy3(void)
212n/a{
213n/a PyObject* m;
214n/a
215n/a if (PyType_Ready(&NoddyType) < 0)
216n/a return NULL;
217n/a
218n/a m = PyModule_Create(&noddy3module);
219n/a if (m == NULL)
220n/a return NULL;
221n/a
222n/a Py_INCREF(&NoddyType);
223n/a PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);
224n/a return m;
225n/a}