Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 1 | /* Cell object implementation */ |
| 2 | |
| 3 | #include "Python.h" |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 4 | #include "pycore_object.h" |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 5 | |
| 6 | PyObject * |
| 7 | PyCell_New(PyObject *obj) |
| 8 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 9 | PyCellObject *op; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 10 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 11 | 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 Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 16 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 17 | _PyObject_GC_TRACK(op); |
| 18 | return (PyObject *)op; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Pierre Glaser | df8d2cd | 2019-02-07 20:36:48 +0100 | [diff] [blame] | 21 | PyDoc_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 | |
| 33 | static PyObject * |
| 34 | cell_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 | |
| 48 | exit: |
| 49 | return return_value; |
| 50 | } |
| 51 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 52 | PyObject * |
| 53 | PyCell_Get(PyObject *op) |
| 54 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | if (!PyCell_Check(op)) { |
| 56 | PyErr_BadInternalCall(); |
| 57 | return NULL; |
| 58 | } |
| 59 | Py_XINCREF(((PyCellObject*)op)->ob_ref); |
| 60 | return PyCell_GET(op); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | int |
| 64 | PyCell_Set(PyObject *op, PyObject *obj) |
| 65 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | 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 Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void |
| 79 | cell_dealloc(PyCellObject *op) |
| 80 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | _PyObject_GC_UNTRACK(op); |
| 82 | Py_XDECREF(op->ob_ref); |
| 83 | PyObject_GC_Del(op); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 86 | static PyObject * |
| 87 | cell_richcompare(PyObject *a, PyObject *b, int op) |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 88 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | /* neither argument should be NULL, unless something's gone wrong */ |
| 90 | assert(a != NULL && b != NULL); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 91 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | /* both arguments should be instances of PyCellObject */ |
| 93 | if (!PyCell_Check(a) || !PyCell_Check(b)) { |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 94 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | } |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | /* 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 Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 102 | |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 103 | Py_RETURN_RICHCOMPARE(b == NULL, a == NULL, op); |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 106 | static PyObject * |
| 107 | cell_repr(PyCellObject *op) |
| 108 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | if (op->ob_ref == NULL) |
| 110 | return PyUnicode_FromFormat("<cell at %p: empty>", op); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>", |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 113 | op, Py_TYPE(op->ob_ref)->tp_name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | op->ob_ref); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int |
| 118 | cell_traverse(PyCellObject *op, visitproc visit, void *arg) |
| 119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | Py_VISIT(op->ob_ref); |
| 121 | return 0; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static int |
| 125 | cell_clear(PyCellObject *op) |
| 126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | Py_CLEAR(op->ob_ref); |
| 128 | return 0; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 131 | static PyObject * |
| 132 | cell_get_contents(PyCellObject *op, void *closure) |
| 133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Victor Stinner | 0ad05c3 | 2017-09-20 06:54:13 -0700 | [diff] [blame] | 143 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 144 | cell_set_contents(PyCellObject *op, PyObject *obj, void *Py_UNUSED(ignored)) |
Lisa Roach | 64505a1 | 2017-06-08 04:43:26 -0700 | [diff] [blame] | 145 | { |
| 146 | Py_XINCREF(obj); |
| 147 | Py_XSETREF(op->ob_ref, obj); |
| 148 | return 0; |
| 149 | } |
| 150 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 151 | static PyGetSetDef cell_getsetlist[] = { |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 152 | {"cell_contents", (getter)cell_get_contents, |
Lisa Roach | 64505a1 | 2017-06-08 04:43:26 -0700 | [diff] [blame] | 153 | (setter)cell_set_contents, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | {NULL} /* sentinel */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 157 | PyTypeObject PyCell_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 159 | "cell", |
| 160 | sizeof(PyCellObject), |
| 161 | 0, |
Benjamin Peterson | 07451dd | 2016-05-12 23:12:21 -0700 | [diff] [blame] | 162 | (destructor)cell_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 163 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | 0, /* tp_getattr */ |
| 165 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 166 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | (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 Peterson | 07451dd | 2016-05-12 23:12:21 -0700 | [diff] [blame] | 177 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Pierre Glaser | df8d2cd | 2019-02-07 20:36:48 +0100 | [diff] [blame] | 178 | cell_new_doc, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 179 | (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 Glaser | df8d2cd | 2019-02-07 20:36:48 +0100 | [diff] [blame] | 188 | 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 Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 197 | }; |