Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 1 | /* Cell object implementation */ |
| 2 | |
| 3 | #include "Python.h" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 4 | #include "internal/mem.h" |
| 5 | #include "internal/pystate.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 | |
| 22 | PyObject * |
| 23 | PyCell_Get(PyObject *op) |
| 24 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | if (!PyCell_Check(op)) { |
| 26 | PyErr_BadInternalCall(); |
| 27 | return NULL; |
| 28 | } |
| 29 | Py_XINCREF(((PyCellObject*)op)->ob_ref); |
| 30 | return PyCell_GET(op); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | int |
| 34 | PyCell_Set(PyObject *op, PyObject *obj) |
| 35 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | PyObject* oldobj; |
| 37 | if (!PyCell_Check(op)) { |
| 38 | PyErr_BadInternalCall(); |
| 39 | return -1; |
| 40 | } |
| 41 | oldobj = PyCell_GET(op); |
| 42 | Py_XINCREF(obj); |
| 43 | PyCell_SET(op, obj); |
| 44 | Py_XDECREF(oldobj); |
| 45 | return 0; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static void |
| 49 | cell_dealloc(PyCellObject *op) |
| 50 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | _PyObject_GC_UNTRACK(op); |
| 52 | Py_XDECREF(op->ob_ref); |
| 53 | PyObject_GC_Del(op); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 56 | #define TEST_COND(cond) ((cond) ? Py_True : Py_False) |
| 57 | |
| 58 | static PyObject * |
| 59 | cell_richcompare(PyObject *a, PyObject *b, int op) |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 60 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | int result; |
| 62 | PyObject *v; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 63 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | /* neither argument should be NULL, unless something's gone wrong */ |
| 65 | assert(a != NULL && b != NULL); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 66 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | /* both arguments should be instances of PyCellObject */ |
| 68 | if (!PyCell_Check(a) || !PyCell_Check(b)) { |
| 69 | v = Py_NotImplemented; |
| 70 | Py_INCREF(v); |
| 71 | return v; |
| 72 | } |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 73 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | /* compare cells by contents; empty cells come before anything else */ |
| 75 | a = ((PyCellObject *)a)->ob_ref; |
| 76 | b = ((PyCellObject *)b)->ob_ref; |
| 77 | if (a != NULL && b != NULL) |
| 78 | return PyObject_RichCompare(a, b, op); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 79 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | result = (b == NULL) - (a == NULL); |
| 81 | switch (op) { |
| 82 | case Py_EQ: |
| 83 | v = TEST_COND(result == 0); |
| 84 | break; |
| 85 | case Py_NE: |
| 86 | v = TEST_COND(result != 0); |
| 87 | break; |
| 88 | case Py_LE: |
| 89 | v = TEST_COND(result <= 0); |
| 90 | break; |
| 91 | case Py_GE: |
| 92 | v = TEST_COND(result >= 0); |
| 93 | break; |
| 94 | case Py_LT: |
| 95 | v = TEST_COND(result < 0); |
| 96 | break; |
| 97 | case Py_GT: |
| 98 | v = TEST_COND(result > 0); |
| 99 | break; |
| 100 | default: |
| 101 | PyErr_BadArgument(); |
| 102 | return NULL; |
| 103 | } |
| 104 | Py_INCREF(v); |
| 105 | return v; |
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 | |
Lisa Roach | 64505a1 | 2017-06-08 04:43:26 -0700 | [diff] [blame] | 145 | int |
| 146 | cell_set_contents(PyCellObject *op, PyObject *obj) |
| 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 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | 0, /* tp_doc */ |
| 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 */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 190 | }; |