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