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