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" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 5 | #include "pycore_pymem.h" |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 6 | |
| 7 | PyObject * |
| 8 | PyCell_New(PyObject *obj) |
| 9 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 10 | PyCellObject *op; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 11 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 12 | op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type); |
| 13 | if (op == NULL) |
| 14 | return NULL; |
| 15 | op->ob_ref = obj; |
| 16 | Py_XINCREF(obj); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 17 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | _PyObject_GC_TRACK(op); |
| 19 | return (PyObject *)op; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Pierre Glaser | df8d2cd | 2019-02-07 20:36:48 +0100 | [diff] [blame] | 22 | PyDoc_STRVAR(cell_new_doc, |
| 23 | "cell([contents])\n" |
| 24 | "--\n" |
| 25 | "\n" |
| 26 | "Create a new cell object.\n" |
| 27 | "\n" |
| 28 | " contents\n" |
| 29 | " the contents of the cell. If not specified, the cell will be empty,\n" |
| 30 | " and \n further attempts to access its cell_contents attribute will\n" |
| 31 | " raise a ValueError."); |
| 32 | |
| 33 | |
| 34 | static PyObject * |
| 35 | cell_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 36 | { |
| 37 | PyObject *return_value = NULL; |
| 38 | PyObject *obj = NULL; |
| 39 | |
| 40 | if (!_PyArg_NoKeywords("cell", kwargs)) { |
| 41 | goto exit; |
| 42 | } |
| 43 | /* min = 0: we allow the cell to be empty */ |
| 44 | if (!PyArg_UnpackTuple(args, "cell", 0, 1, &obj)) { |
| 45 | goto exit; |
| 46 | } |
| 47 | return_value = PyCell_New(obj); |
| 48 | |
| 49 | exit: |
| 50 | return return_value; |
| 51 | } |
| 52 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 53 | PyObject * |
| 54 | PyCell_Get(PyObject *op) |
| 55 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | if (!PyCell_Check(op)) { |
| 57 | PyErr_BadInternalCall(); |
| 58 | return NULL; |
| 59 | } |
| 60 | Py_XINCREF(((PyCellObject*)op)->ob_ref); |
| 61 | return PyCell_GET(op); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int |
| 65 | PyCell_Set(PyObject *op, PyObject *obj) |
| 66 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | PyObject* oldobj; |
| 68 | if (!PyCell_Check(op)) { |
| 69 | PyErr_BadInternalCall(); |
| 70 | return -1; |
| 71 | } |
| 72 | oldobj = PyCell_GET(op); |
| 73 | Py_XINCREF(obj); |
| 74 | PyCell_SET(op, obj); |
| 75 | Py_XDECREF(oldobj); |
| 76 | return 0; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static void |
| 80 | cell_dealloc(PyCellObject *op) |
| 81 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | _PyObject_GC_UNTRACK(op); |
| 83 | Py_XDECREF(op->ob_ref); |
| 84 | PyObject_GC_Del(op); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 87 | static PyObject * |
| 88 | cell_richcompare(PyObject *a, PyObject *b, int op) |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 89 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | /* neither argument should be NULL, unless something's gone wrong */ |
| 91 | assert(a != NULL && b != NULL); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 92 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | /* both arguments should be instances of PyCellObject */ |
| 94 | if (!PyCell_Check(a) || !PyCell_Check(b)) { |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 95 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | } |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 97 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | /* compare cells by contents; empty cells come before anything else */ |
| 99 | a = ((PyCellObject *)a)->ob_ref; |
| 100 | b = ((PyCellObject *)b)->ob_ref; |
| 101 | if (a != NULL && b != NULL) |
| 102 | return PyObject_RichCompare(a, b, op); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 103 | |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 104 | Py_RETURN_RICHCOMPARE(b == NULL, a == NULL, op); |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 107 | static PyObject * |
| 108 | cell_repr(PyCellObject *op) |
| 109 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | if (op->ob_ref == NULL) |
| 111 | return PyUnicode_FromFormat("<cell at %p: empty>", op); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>", |
Victor Stinner | 58ac700 | 2020-02-07 03:04:21 +0100 | [diff] [blame] | 114 | op, Py_TYPE(op->ob_ref)->tp_name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | op->ob_ref); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static int |
| 119 | cell_traverse(PyCellObject *op, visitproc visit, void *arg) |
| 120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | Py_VISIT(op->ob_ref); |
| 122 | return 0; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static int |
| 126 | cell_clear(PyCellObject *op) |
| 127 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | Py_CLEAR(op->ob_ref); |
| 129 | return 0; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 132 | static PyObject * |
| 133 | cell_get_contents(PyCellObject *op, void *closure) |
| 134 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | if (op->ob_ref == NULL) |
| 136 | { |
| 137 | PyErr_SetString(PyExc_ValueError, "Cell is empty"); |
| 138 | return NULL; |
| 139 | } |
| 140 | Py_INCREF(op->ob_ref); |
| 141 | return op->ob_ref; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Victor Stinner | 0ad05c3 | 2017-09-20 06:54:13 -0700 | [diff] [blame] | 144 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 145 | cell_set_contents(PyCellObject *op, PyObject *obj, void *Py_UNUSED(ignored)) |
Lisa Roach | 64505a1 | 2017-06-08 04:43:26 -0700 | [diff] [blame] | 146 | { |
| 147 | Py_XINCREF(obj); |
| 148 | Py_XSETREF(op->ob_ref, obj); |
| 149 | return 0; |
| 150 | } |
| 151 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 152 | static PyGetSetDef cell_getsetlist[] = { |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 153 | {"cell_contents", (getter)cell_get_contents, |
Lisa Roach | 64505a1 | 2017-06-08 04:43:26 -0700 | [diff] [blame] | 154 | (setter)cell_set_contents, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | {NULL} /* sentinel */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 158 | PyTypeObject PyCell_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 160 | "cell", |
| 161 | sizeof(PyCellObject), |
| 162 | 0, |
Benjamin Peterson | 07451dd | 2016-05-12 23:12:21 -0700 | [diff] [blame] | 163 | (destructor)cell_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 164 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | 0, /* tp_getattr */ |
| 166 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 167 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | (reprfunc)cell_repr, /* tp_repr */ |
| 169 | 0, /* tp_as_number */ |
| 170 | 0, /* tp_as_sequence */ |
| 171 | 0, /* tp_as_mapping */ |
| 172 | 0, /* tp_hash */ |
| 173 | 0, /* tp_call */ |
| 174 | 0, /* tp_str */ |
| 175 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 176 | 0, /* tp_setattro */ |
| 177 | 0, /* tp_as_buffer */ |
Benjamin Peterson | 07451dd | 2016-05-12 23:12:21 -0700 | [diff] [blame] | 178 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Pierre Glaser | df8d2cd | 2019-02-07 20:36:48 +0100 | [diff] [blame] | 179 | cell_new_doc, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | (traverseproc)cell_traverse, /* tp_traverse */ |
| 181 | (inquiry)cell_clear, /* tp_clear */ |
| 182 | cell_richcompare, /* tp_richcompare */ |
| 183 | 0, /* tp_weaklistoffset */ |
| 184 | 0, /* tp_iter */ |
| 185 | 0, /* tp_iternext */ |
| 186 | 0, /* tp_methods */ |
| 187 | 0, /* tp_members */ |
| 188 | cell_getsetlist, /* tp_getset */ |
Pierre Glaser | df8d2cd | 2019-02-07 20:36:48 +0100 | [diff] [blame] | 189 | 0, /* tp_base */ |
| 190 | 0, /* tp_dict */ |
| 191 | 0, /* tp_descr_get */ |
| 192 | 0, /* tp_descr_set */ |
| 193 | 0, /* tp_dictoffset */ |
| 194 | 0, /* tp_init */ |
| 195 | 0, /* tp_alloc */ |
| 196 | (newfunc)cell_new, /* tp_new */ |
| 197 | 0, /* tp_free */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 198 | }; |