ยปCore Development>Code coverage>Objects/cellobject.c

Python code coverage for Objects/cellobject.c

#countcontent
1n/a/* Cell object implementation */
2n/a
3n/a#include "Python.h"
4n/a
5n/aPyObject *
6n/aPyCell_New(PyObject *obj)
7n/a{
8n/a PyCellObject *op;
9n/a
10n/a op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
11n/a if (op == NULL)
12n/a return NULL;
13n/a op->ob_ref = obj;
14n/a Py_XINCREF(obj);
15n/a
16n/a _PyObject_GC_TRACK(op);
17n/a return (PyObject *)op;
18n/a}
19n/a
20n/aPyObject *
21n/aPyCell_Get(PyObject *op)
22n/a{
23n/a if (!PyCell_Check(op)) {
24n/a PyErr_BadInternalCall();
25n/a return NULL;
26n/a }
27n/a Py_XINCREF(((PyCellObject*)op)->ob_ref);
28n/a return PyCell_GET(op);
29n/a}
30n/a
31n/aint
32n/aPyCell_Set(PyObject *op, PyObject *obj)
33n/a{
34n/a PyObject* oldobj;
35n/a if (!PyCell_Check(op)) {
36n/a PyErr_BadInternalCall();
37n/a return -1;
38n/a }
39n/a oldobj = PyCell_GET(op);
40n/a Py_XINCREF(obj);
41n/a PyCell_SET(op, obj);
42n/a Py_XDECREF(oldobj);
43n/a return 0;
44n/a}
45n/a
46n/astatic void
47n/acell_dealloc(PyCellObject *op)
48n/a{
49n/a _PyObject_GC_UNTRACK(op);
50n/a Py_XDECREF(op->ob_ref);
51n/a PyObject_GC_Del(op);
52n/a}
53n/a
54n/a#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
55n/a
56n/astatic PyObject *
57n/acell_richcompare(PyObject *a, PyObject *b, int op)
58n/a{
59n/a int result;
60n/a PyObject *v;
61n/a
62n/a /* neither argument should be NULL, unless something's gone wrong */
63n/a assert(a != NULL && b != NULL);
64n/a
65n/a /* both arguments should be instances of PyCellObject */
66n/a if (!PyCell_Check(a) || !PyCell_Check(b)) {
67n/a v = Py_NotImplemented;
68n/a Py_INCREF(v);
69n/a return v;
70n/a }
71n/a
72n/a /* compare cells by contents; empty cells come before anything else */
73n/a a = ((PyCellObject *)a)->ob_ref;
74n/a b = ((PyCellObject *)b)->ob_ref;
75n/a if (a != NULL && b != NULL)
76n/a return PyObject_RichCompare(a, b, op);
77n/a
78n/a result = (b == NULL) - (a == NULL);
79n/a switch (op) {
80n/a case Py_EQ:
81n/a v = TEST_COND(result == 0);
82n/a break;
83n/a case Py_NE:
84n/a v = TEST_COND(result != 0);
85n/a break;
86n/a case Py_LE:
87n/a v = TEST_COND(result <= 0);
88n/a break;
89n/a case Py_GE:
90n/a v = TEST_COND(result >= 0);
91n/a break;
92n/a case Py_LT:
93n/a v = TEST_COND(result < 0);
94n/a break;
95n/a case Py_GT:
96n/a v = TEST_COND(result > 0);
97n/a break;
98n/a default:
99n/a PyErr_BadArgument();
100n/a return NULL;
101n/a }
102n/a Py_INCREF(v);
103n/a return v;
104n/a}
105n/a
106n/astatic PyObject *
107n/acell_repr(PyCellObject *op)
108n/a{
109n/a if (op->ob_ref == NULL)
110n/a return PyUnicode_FromFormat("<cell at %p: empty>", op);
111n/a
112n/a return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
113n/a op, op->ob_ref->ob_type->tp_name,
114n/a op->ob_ref);
115n/a}
116n/a
117n/astatic int
118n/acell_traverse(PyCellObject *op, visitproc visit, void *arg)
119n/a{
120n/a Py_VISIT(op->ob_ref);
121n/a return 0;
122n/a}
123n/a
124n/astatic int
125n/acell_clear(PyCellObject *op)
126n/a{
127n/a Py_CLEAR(op->ob_ref);
128n/a return 0;
129n/a}
130n/a
131n/astatic PyObject *
132n/acell_get_contents(PyCellObject *op, void *closure)
133n/a{
134n/a if (op->ob_ref == NULL)
135n/a {
136n/a PyErr_SetString(PyExc_ValueError, "Cell is empty");
137n/a return NULL;
138n/a }
139n/a Py_INCREF(op->ob_ref);
140n/a return op->ob_ref;
141n/a}
142n/a
143n/astatic PyGetSetDef cell_getsetlist[] = {
144n/a {"cell_contents", (getter)cell_get_contents, NULL},
145n/a {NULL} /* sentinel */
146n/a};
147n/a
148n/aPyTypeObject PyCell_Type = {
149n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
150n/a "cell",
151n/a sizeof(PyCellObject),
152n/a 0,
153n/a (destructor)cell_dealloc, /* tp_dealloc */
154n/a 0, /* tp_print */
155n/a 0, /* tp_getattr */
156n/a 0, /* tp_setattr */
157n/a 0, /* tp_reserved */
158n/a (reprfunc)cell_repr, /* tp_repr */
159n/a 0, /* tp_as_number */
160n/a 0, /* tp_as_sequence */
161n/a 0, /* tp_as_mapping */
162n/a 0, /* tp_hash */
163n/a 0, /* tp_call */
164n/a 0, /* tp_str */
165n/a PyObject_GenericGetAttr, /* tp_getattro */
166n/a 0, /* tp_setattro */
167n/a 0, /* tp_as_buffer */
168n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
169n/a 0, /* tp_doc */
170n/a (traverseproc)cell_traverse, /* tp_traverse */
171n/a (inquiry)cell_clear, /* tp_clear */
172n/a cell_richcompare, /* tp_richcompare */
173n/a 0, /* tp_weaklistoffset */
174n/a 0, /* tp_iter */
175n/a 0, /* tp_iternext */
176n/a 0, /* tp_methods */
177n/a 0, /* tp_members */
178n/a cell_getsetlist, /* tp_getset */
179n/a};