/src/cpython/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 | 21.4M | #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 | 16.8M | { |
19 | 16.8M | PyCellObject *op; |
20 | | |
21 | 16.8M | op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type); |
22 | 16.8M | if (op == NULL) |
23 | 0 | return NULL; |
24 | 16.8M | op->ob_ref = Py_XNewRef(obj); |
25 | | |
26 | 16.8M | _PyObject_GC_TRACK(op); |
27 | 16.8M | return (PyObject *)op; |
28 | 16.8M | } |
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 | 10.1k | { |
67 | 10.1k | if (!PyCell_Check(op)) { |
68 | 0 | PyErr_BadInternalCall(); |
69 | 0 | return -1; |
70 | 0 | } |
71 | 10.1k | PyCell_SetTakeRef((PyCellObject *)op, Py_XNewRef(value)); |
72 | 10.1k | return 0; |
73 | 10.1k | } |
74 | | |
75 | | static void |
76 | | cell_dealloc(PyObject *self) |
77 | 16.8M | { |
78 | 16.8M | PyCellObject *op = _PyCell_CAST(self); |
79 | 16.8M | _PyObject_GC_UNTRACK(op); |
80 | 16.8M | Py_XDECREF(op->ob_ref); |
81 | 16.8M | PyObject_GC_Del(op); |
82 | 16.8M | } |
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 | 3.36M | { |
132 | 3.36M | PyCellObject *op = _PyCell_CAST(self); |
133 | 3.36M | Py_VISIT(op->ob_ref); |
134 | 3.36M | return 0; |
135 | 3.36M | } |
136 | | |
137 | | static int |
138 | | cell_clear(PyObject *self) |
139 | 1.27M | { |
140 | 1.27M | PyCellObject *op = _PyCell_CAST(self); |
141 | 1.27M | Py_CLEAR(op->ob_ref); |
142 | 1.27M | return 0; |
143 | 1.27M | } |
144 | | |
145 | | static PyObject * |
146 | | cell_get_contents(PyObject *self, void *closure) |
147 | 164 | { |
148 | 164 | PyCellObject *op = _PyCell_CAST(self); |
149 | 164 | PyObject *res = PyCell_GetRef(op); |
150 | 164 | if (res == NULL) { |
151 | 0 | PyErr_SetString(PyExc_ValueError, "Cell is empty"); |
152 | 0 | return NULL; |
153 | 0 | } |
154 | 164 | return res; |
155 | 164 | } |
156 | | |
157 | | static int |
158 | | cell_set_contents(PyObject *self, PyObject *obj, void *Py_UNUSED(ignored)) |
159 | 128 | { |
160 | 128 | PyCellObject *cell = _PyCell_CAST(self); |
161 | 128 | Py_XINCREF(obj); |
162 | 128 | PyCell_SetTakeRef((PyCellObject *)cell, obj); |
163 | 128 | return 0; |
164 | 128 | } |
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 | | }; |