blob: 86a89f02e60d3c55e8ac784e918bf541f5b6fe6a [file] [log] [blame]
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +00001/* Cell object implementation */
2
3#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01004#include "pycore_object.h"
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +00005
6PyObject *
7PyCell_New(PyObject *obj)
8{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00009 PyCellObject *op;
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
12 if (op == NULL)
13 return NULL;
14 op->ob_ref = obj;
15 Py_XINCREF(obj);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 _PyObject_GC_TRACK(op);
18 return (PyObject *)op;
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000019}
20
Pierre Glaserdf8d2cd2019-02-07 20:36:48 +010021PyDoc_STRVAR(cell_new_doc,
22"cell([contents])\n"
23"--\n"
24"\n"
25"Create a new cell object.\n"
26"\n"
27" contents\n"
28" the contents of the cell. If not specified, the cell will be empty,\n"
29" and \n further attempts to access its cell_contents attribute will\n"
30" raise a ValueError.");
31
32
33static PyObject *
34cell_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
35{
36 PyObject *return_value = NULL;
37 PyObject *obj = NULL;
38
39 if (!_PyArg_NoKeywords("cell", kwargs)) {
40 goto exit;
41 }
42 /* min = 0: we allow the cell to be empty */
43 if (!PyArg_UnpackTuple(args, "cell", 0, 1, &obj)) {
44 goto exit;
45 }
46 return_value = PyCell_New(obj);
47
48exit:
49 return return_value;
50}
51
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000052PyObject *
53PyCell_Get(PyObject *op)
54{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 if (!PyCell_Check(op)) {
56 PyErr_BadInternalCall();
57 return NULL;
58 }
59 Py_XINCREF(((PyCellObject*)op)->ob_ref);
60 return PyCell_GET(op);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000061}
62
63int
64PyCell_Set(PyObject *op, PyObject *obj)
65{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 PyObject* oldobj;
67 if (!PyCell_Check(op)) {
68 PyErr_BadInternalCall();
69 return -1;
70 }
71 oldobj = PyCell_GET(op);
72 Py_XINCREF(obj);
73 PyCell_SET(op, obj);
74 Py_XDECREF(oldobj);
75 return 0;
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000076}
77
78static void
79cell_dealloc(PyCellObject *op)
80{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 _PyObject_GC_UNTRACK(op);
82 Py_XDECREF(op->ob_ref);
83 PyObject_GC_Del(op);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000084}
85
Mark Dickinson211c6252009-02-01 10:28:51 +000086static PyObject *
87cell_richcompare(PyObject *a, PyObject *b, int op)
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 /* neither argument should be NULL, unless something's gone wrong */
90 assert(a != NULL && b != NULL);
Mark Dickinson211c6252009-02-01 10:28:51 +000091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 /* both arguments should be instances of PyCellObject */
93 if (!PyCell_Check(a) || !PyCell_Check(b)) {
stratakise8b19652017-11-02 11:32:54 +010094 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 }
Mark Dickinson211c6252009-02-01 10:28:51 +000096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 /* compare cells by contents; empty cells come before anything else */
98 a = ((PyCellObject *)a)->ob_ref;
99 b = ((PyCellObject *)b)->ob_ref;
100 if (a != NULL && b != NULL)
101 return PyObject_RichCompare(a, b, op);
Mark Dickinson211c6252009-02-01 10:28:51 +0000102
stratakise8b19652017-11-02 11:32:54 +0100103 Py_RETURN_RICHCOMPARE(b == NULL, a == NULL, op);
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000104}
105
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000106static PyObject *
107cell_repr(PyCellObject *op)
108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 if (op->ob_ref == NULL)
110 return PyUnicode_FromFormat("<cell at %p: empty>", op);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
Victor Stinner58ac7002020-02-07 03:04:21 +0100113 op, Py_TYPE(op->ob_ref)->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000115}
116
117static int
118cell_traverse(PyCellObject *op, visitproc visit, void *arg)
119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 Py_VISIT(op->ob_ref);
121 return 0;
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000122}
123
124static int
125cell_clear(PyCellObject *op)
126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 Py_CLEAR(op->ob_ref);
128 return 0;
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000129}
130
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000131static PyObject *
132cell_get_contents(PyCellObject *op, void *closure)
133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 if (op->ob_ref == NULL)
135 {
136 PyErr_SetString(PyExc_ValueError, "Cell is empty");
137 return NULL;
138 }
139 Py_INCREF(op->ob_ref);
140 return op->ob_ref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000141}
142
Victor Stinner0ad05c32017-09-20 06:54:13 -0700143static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200144cell_set_contents(PyCellObject *op, PyObject *obj, void *Py_UNUSED(ignored))
Lisa Roach64505a12017-06-08 04:43:26 -0700145{
146 Py_XINCREF(obj);
147 Py_XSETREF(op->ob_ref, obj);
148 return 0;
149}
150
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000151static PyGetSetDef cell_getsetlist[] = {
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300152 {"cell_contents", (getter)cell_get_contents,
Lisa Roach64505a12017-06-08 04:43:26 -0700153 (setter)cell_set_contents, NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 {NULL} /* sentinel */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000155};
156
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000157PyTypeObject PyCell_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 PyVarObject_HEAD_INIT(&PyType_Type, 0)
159 "cell",
160 sizeof(PyCellObject),
161 0,
Benjamin Peterson07451dd2016-05-12 23:12:21 -0700162 (destructor)cell_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200163 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 0, /* tp_getattr */
165 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200166 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 (reprfunc)cell_repr, /* tp_repr */
168 0, /* tp_as_number */
169 0, /* tp_as_sequence */
170 0, /* tp_as_mapping */
171 0, /* tp_hash */
172 0, /* tp_call */
173 0, /* tp_str */
174 PyObject_GenericGetAttr, /* tp_getattro */
175 0, /* tp_setattro */
176 0, /* tp_as_buffer */
Benjamin Peterson07451dd2016-05-12 23:12:21 -0700177 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Pierre Glaserdf8d2cd2019-02-07 20:36:48 +0100178 cell_new_doc, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 (traverseproc)cell_traverse, /* tp_traverse */
180 (inquiry)cell_clear, /* tp_clear */
181 cell_richcompare, /* tp_richcompare */
182 0, /* tp_weaklistoffset */
183 0, /* tp_iter */
184 0, /* tp_iternext */
185 0, /* tp_methods */
186 0, /* tp_members */
187 cell_getsetlist, /* tp_getset */
Pierre Glaserdf8d2cd2019-02-07 20:36:48 +0100188 0, /* tp_base */
189 0, /* tp_dict */
190 0, /* tp_descr_get */
191 0, /* tp_descr_set */
192 0, /* tp_dictoffset */
193 0, /* tp_init */
194 0, /* tp_alloc */
195 (newfunc)cell_new, /* tp_new */
196 0, /* tp_free */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000197};