Coverage Report

Created: 2026-11-30 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython3/Objects/cellobject.c
Line
Count
Source
1
/* Cell object implementation */
2
3
#include "Python.h"
4
#include "pycore_cell.h"          // PyCell_GetRef()
5
#include "pycore_object.h"
6
7
#include "clinic/cellobject.c.h"
8
9
5.92M
#define _PyCell_CAST(op) _Py_CAST(PyCellObject*, (op))
10
11
/*[clinic input]
12
class cell "PyCellObject *" "&PyCell_Type"
13
[clinic start generated code]*/
14
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3d6baba8cf810af0]*/
15
16
PyObject *
17
PyCell_New(PyObject *obj)
18
5.65M
{
19
5.65M
    PyCellObject *op;
20
21
5.65M
    op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
22
5.65M
    if (op == NULL)
23
0
        return NULL;
24
5.65M
    op->ob_ref = Py_XNewRef(obj);
25
26
5.65M
    _PyObject_GC_TRACK(op);
27
5.65M
    return (PyObject *)op;
28
5.65M
}
29
30
/*[clinic input]
31
@vectorcall
32
@classmethod
33
@text_signature "([contents])"
34
cell.__new__ as cell_new
35
36
    contents: object = NULL
37
    /
38
39
Create a new cell object.
40
41
  contents
42
    the contents of the cell. If not specified, the cell will be empty,
43
    and further attempts to access its cell_contents attribute will
44
    raise a ValueError.
45
[clinic start generated code]*/
46
47
static PyObject *
48
cell_new_impl(PyTypeObject *type, PyObject *contents)
49
/*[clinic end generated code: output=0b7db264f0621bb4 input=4460339a82243798]*/
50
0
{
51
0
    return PyCell_New(contents);
52
0
}
53
54
PyObject *
55
PyCell_Get(PyObject *op)
56
0
{
57
0
    if (!PyCell_Check(op)) {
58
0
        PyErr_BadInternalCall();
59
0
        return NULL;
60
0
    }
61
0
    return PyCell_GetRef((PyCellObject *)op);
62
0
}
63
64
int
65
PyCell_Set(PyObject *op, PyObject *value)
66
2.98k
{
67
2.98k
    if (!PyCell_Check(op)) {
68
0
        PyErr_BadInternalCall();
69
0
        return -1;
70
0
    }
71
2.98k
    PyCell_SetTakeRef((PyCellObject *)op, Py_XNewRef(value));
72
2.98k
    return 0;
73
2.98k
}
74
75
static void
76
cell_dealloc(PyObject *self)
77
5.65M
{
78
5.65M
    PyCellObject *op = _PyCell_CAST(self);
79
5.65M
    _PyObject_GC_UNTRACK(op);
80
5.65M
    Py_XDECREF(op->ob_ref);
81
5.65M
    PyObject_GC_Del(op);
82
5.65M
}
83
84
static PyObject *
85
cell_compare_impl(PyObject *a, PyObject *b, int op)
86
0
{
87
0
    if (a != NULL && b != NULL) {
88
0
        return PyObject_RichCompare(a, b, op);
89
0
    }
90
0
    else {
91
0
        Py_RETURN_RICHCOMPARE(b == NULL, a == NULL, op);
92
0
    }
93
0
}
94
95
static PyObject *
96
cell_richcompare(PyObject *a, PyObject *b, int op)
97
0
{
98
    /* neither argument should be NULL, unless something's gone wrong */
99
0
    assert(a != NULL && b != NULL);
100
101
    /* both arguments should be instances of PyCellObject */
102
0
    if (!PyCell_Check(a) || !PyCell_Check(b)) {
103
0
        Py_RETURN_NOTIMPLEMENTED;
104
0
    }
105
0
    PyObject *a_ref = PyCell_GetRef((PyCellObject *)a);
106
0
    PyObject *b_ref = PyCell_GetRef((PyCellObject *)b);
107
108
    /* compare cells by contents; empty cells come before anything else */
109
0
    PyObject *res = cell_compare_impl(a_ref, b_ref, op);
110
111
0
    Py_XDECREF(a_ref);
112
0
    Py_XDECREF(b_ref);
113
0
    return res;
114
0
}
115
116
static PyObject *
117
cell_repr(PyObject *self)
118
0
{
119
0
    PyObject *ref = PyCell_GetRef((PyCellObject *)self);
120
0
    if (ref == NULL) {
121
0
        return PyUnicode_FromFormat("<cell at %p: empty>", self);
122
0
    }
123
0
    PyObject *res = PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
124
0
                                         self, Py_TYPE(ref)->tp_name, ref);
125
0
    Py_DECREF(ref);
126
0
    return res;
127
0
}
128
129
static int
130
cell_traverse(PyObject *self, visitproc visit, void *arg)
131
207k
{
132
207k
    PyCellObject *op = _PyCell_CAST(self);
133
207k
    Py_VISIT(op->ob_ref);
134
207k
    return 0;
135
207k
}
136
137
static int
138
cell_clear(PyObject *self)
139
61.5k
{
140
61.5k
    PyCellObject *op = _PyCell_CAST(self);
141
61.5k
    Py_CLEAR(op->ob_ref);
142
61.5k
    return 0;
143
61.5k
}
144
145
static PyObject *
146
cell_get_contents(PyObject *self, void *closure)
147
0
{
148
0
    PyCellObject *op = _PyCell_CAST(self);
149
0
    PyObject *res = PyCell_GetRef(op);
150
0
    if (res == NULL) {
151
0
        PyErr_SetString(PyExc_ValueError, "Cell is empty");
152
0
        return NULL;
153
0
    }
154
0
    return res;
155
0
}
156
157
static int
158
cell_set_contents(PyObject *self, PyObject *obj, void *Py_UNUSED(ignored))
159
0
{
160
0
    PyCellObject *cell = _PyCell_CAST(self);
161
0
    Py_XINCREF(obj);
162
0
    PyCell_SetTakeRef((PyCellObject *)cell, obj);
163
0
    return 0;
164
0
}
165
166
static PyGetSetDef cell_getsetlist[] = {
167
    {"cell_contents", cell_get_contents, cell_set_contents, NULL},
168
    {NULL} /* sentinel */
169
};
170
171
PyTypeObject PyCell_Type = {
172
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
173
    "cell",
174
    sizeof(PyCellObject),
175
    0,
176
    cell_dealloc,                               /* tp_dealloc */
177
    0,                                          /* tp_vectorcall_offset */
178
    0,                                          /* tp_getattr */
179
    0,                                          /* tp_setattr */
180
    0,                                          /* tp_as_async */
181
    cell_repr,                                  /* tp_repr */
182
    0,                                          /* tp_as_number */
183
    0,                                          /* tp_as_sequence */
184
    0,                                          /* tp_as_mapping */
185
    0,                                          /* tp_hash */
186
    0,                                          /* tp_call */
187
    0,                                          /* tp_str */
188
    PyObject_GenericGetAttr,                    /* tp_getattro */
189
    0,                                          /* tp_setattro */
190
    0,                                          /* tp_as_buffer */
191
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
192
    cell_new__doc__,                            /* tp_doc */
193
    cell_traverse,                              /* tp_traverse */
194
    cell_clear,                                 /* tp_clear */
195
    cell_richcompare,                           /* tp_richcompare */
196
    0,                                          /* tp_weaklistoffset */
197
    0,                                          /* tp_iter */
198
    0,                                          /* tp_iternext */
199
    0,                                          /* tp_methods */
200
    0,                                          /* tp_members */
201
    cell_getsetlist,                            /* tp_getset */
202
    0,                                          /* tp_base */
203
    0,                                          /* tp_dict */
204
    0,                                          /* tp_descr_get */
205
    0,                                          /* tp_descr_set */
206
    0,                                          /* tp_dictoffset */
207
    0,                                          /* tp_init */
208
    0,                                          /* tp_alloc */
209
    cell_new,                                   /* tp_new */
210
    0,                                          /* tp_free */
211
    .tp_vectorcall = cell_vectorcall,
212
};