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

Python code coverage for Doc/includes/noddy2.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; /* first name */
7n/a PyObject *last; /* last name */
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, "|OOi", 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_XDECREF(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_XDECREF(tmp);
68n/a }
69n/a
70n/a return 0;
71n/a}
72n/a
73n/a
74n/astatic PyMemberDef Noddy_members[] = {
75n/a {"first", T_OBJECT_EX, offsetof(Noddy, first), 0,
76n/a "first name"},
77n/a {"last", T_OBJECT_EX, offsetof(Noddy, last), 0,
78n/a "last name"},
79n/a {"number", T_INT, offsetof(Noddy, number), 0,
80n/a "noddy number"},
81n/a {NULL} /* Sentinel */
82n/a};
83n/a
84n/astatic PyObject *
85n/aNoddy_name(Noddy* self)
86n/a{
87n/a if (self->first == NULL) {
88n/a PyErr_SetString(PyExc_AttributeError, "first");
89n/a return NULL;
90n/a }
91n/a
92n/a if (self->last == NULL) {
93n/a PyErr_SetString(PyExc_AttributeError, "last");
94n/a return NULL;
95n/a }
96n/a
97n/a return PyUnicode_FromFormat("%S %S", self->first, self->last);
98n/a}
99n/a
100n/astatic PyMethodDef Noddy_methods[] = {
101n/a {"name", (PyCFunction)Noddy_name, METH_NOARGS,
102n/a "Return the name, combining the first and last name"
103n/a },
104n/a {NULL} /* Sentinel */
105n/a};
106n/a
107n/astatic PyTypeObject NoddyType = {
108n/a PyVarObject_HEAD_INIT(NULL, 0)
109n/a "noddy.Noddy", /* tp_name */
110n/a sizeof(Noddy), /* tp_basicsize */
111n/a 0, /* tp_itemsize */
112n/a (destructor)Noddy_dealloc, /* tp_dealloc */
113n/a 0, /* tp_print */
114n/a 0, /* tp_getattr */
115n/a 0, /* tp_setattr */
116n/a 0, /* tp_reserved */
117n/a 0, /* tp_repr */
118n/a 0, /* tp_as_number */
119n/a 0, /* tp_as_sequence */
120n/a 0, /* tp_as_mapping */
121n/a 0, /* tp_hash */
122n/a 0, /* tp_call */
123n/a 0, /* tp_str */
124n/a 0, /* tp_getattro */
125n/a 0, /* tp_setattro */
126n/a 0, /* tp_as_buffer */
127n/a Py_TPFLAGS_DEFAULT |
128n/a Py_TPFLAGS_BASETYPE, /* tp_flags */
129n/a "Noddy objects", /* tp_doc */
130n/a 0, /* tp_traverse */
131n/a 0, /* tp_clear */
132n/a 0, /* tp_richcompare */
133n/a 0, /* tp_weaklistoffset */
134n/a 0, /* tp_iter */
135n/a 0, /* tp_iternext */
136n/a Noddy_methods, /* tp_methods */
137n/a Noddy_members, /* tp_members */
138n/a 0, /* tp_getset */
139n/a 0, /* tp_base */
140n/a 0, /* tp_dict */
141n/a 0, /* tp_descr_get */
142n/a 0, /* tp_descr_set */
143n/a 0, /* tp_dictoffset */
144n/a (initproc)Noddy_init, /* tp_init */
145n/a 0, /* tp_alloc */
146n/a Noddy_new, /* tp_new */
147n/a};
148n/a
149n/astatic PyModuleDef noddy2module = {
150n/a PyModuleDef_HEAD_INIT,
151n/a "noddy2",
152n/a "Example module that creates an extension type.",
153n/a -1,
154n/a NULL, NULL, NULL, NULL, NULL
155n/a};
156n/a
157n/aPyMODINIT_FUNC
158n/aPyInit_noddy2(void)
159n/a{
160n/a PyObject* m;
161n/a
162n/a if (PyType_Ready(&NoddyType) < 0)
163n/a return NULL;
164n/a
165n/a m = PyModule_Create(&noddy2module);
166n/a if (m == NULL)
167n/a return NULL;
168n/a
169n/a Py_INCREF(&NoddyType);
170n/a PyModule_AddObject(m, "Noddy", (PyObject *)&NoddyType);
171n/a return m;
172n/a}