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 | |
| 23 | PyObject * |
| 24 | PyCell_Get(PyObject *op) |
| 25 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | if (!PyCell_Check(op)) { |
| 27 | PyErr_BadInternalCall(); |
| 28 | return NULL; |
| 29 | } |
| 30 | Py_XINCREF(((PyCellObject*)op)->ob_ref); |
| 31 | return PyCell_GET(op); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | int |
| 35 | PyCell_Set(PyObject *op, PyObject *obj) |
| 36 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | PyObject* oldobj; |
| 38 | if (!PyCell_Check(op)) { |
| 39 | PyErr_BadInternalCall(); |
| 40 | return -1; |
| 41 | } |
| 42 | oldobj = PyCell_GET(op); |
| 43 | Py_XINCREF(obj); |
| 44 | PyCell_SET(op, obj); |
| 45 | Py_XDECREF(oldobj); |
| 46 | return 0; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static void |
| 50 | cell_dealloc(PyCellObject *op) |
| 51 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | _PyObject_GC_UNTRACK(op); |
| 53 | Py_XDECREF(op->ob_ref); |
| 54 | PyObject_GC_Del(op); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 57 | static PyObject * |
| 58 | cell_richcompare(PyObject *a, PyObject *b, int op) |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 59 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | /* neither argument should be NULL, unless something's gone wrong */ |
| 61 | assert(a != NULL && b != NULL); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 62 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | /* both arguments should be instances of PyCellObject */ |
| 64 | if (!PyCell_Check(a) || !PyCell_Check(b)) { |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 65 | Py_RETURN_NOTIMPLEMENTED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | } |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 67 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | /* compare cells by contents; empty cells come before anything else */ |
| 69 | a = ((PyCellObject *)a)->ob_ref; |
| 70 | b = ((PyCellObject *)b)->ob_ref; |
| 71 | if (a != NULL && b != NULL) |
| 72 | return PyObject_RichCompare(a, b, op); |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 73 | |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 74 | Py_RETURN_RICHCOMPARE(b == NULL, a == NULL, op); |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 77 | static PyObject * |
| 78 | cell_repr(PyCellObject *op) |
| 79 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | if (op->ob_ref == NULL) |
| 81 | return PyUnicode_FromFormat("<cell at %p: empty>", op); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 82 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>", |
| 84 | op, op->ob_ref->ob_type->tp_name, |
| 85 | op->ob_ref); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static int |
| 89 | cell_traverse(PyCellObject *op, visitproc visit, void *arg) |
| 90 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | Py_VISIT(op->ob_ref); |
| 92 | return 0; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static int |
| 96 | cell_clear(PyCellObject *op) |
| 97 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | Py_CLEAR(op->ob_ref); |
| 99 | return 0; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 102 | static PyObject * |
| 103 | cell_get_contents(PyCellObject *op, void *closure) |
| 104 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | if (op->ob_ref == NULL) |
| 106 | { |
| 107 | PyErr_SetString(PyExc_ValueError, "Cell is empty"); |
| 108 | return NULL; |
| 109 | } |
| 110 | Py_INCREF(op->ob_ref); |
| 111 | return op->ob_ref; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Victor Stinner | 0ad05c3 | 2017-09-20 06:54:13 -0700 | [diff] [blame] | 114 | static int |
Lisa Roach | 64505a1 | 2017-06-08 04:43:26 -0700 | [diff] [blame] | 115 | cell_set_contents(PyCellObject *op, PyObject *obj) |
| 116 | { |
| 117 | Py_XINCREF(obj); |
| 118 | Py_XSETREF(op->ob_ref, obj); |
| 119 | return 0; |
| 120 | } |
| 121 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 122 | static PyGetSetDef cell_getsetlist[] = { |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 123 | {"cell_contents", (getter)cell_get_contents, |
Lisa Roach | 64505a1 | 2017-06-08 04:43:26 -0700 | [diff] [blame] | 124 | (setter)cell_set_contents, NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | {NULL} /* sentinel */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 128 | PyTypeObject PyCell_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 130 | "cell", |
| 131 | sizeof(PyCellObject), |
| 132 | 0, |
Benjamin Peterson | 07451dd | 2016-05-12 23:12:21 -0700 | [diff] [blame] | 133 | (destructor)cell_dealloc, /* tp_dealloc */ |
| 134 | 0, /* tp_print */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | 0, /* tp_getattr */ |
| 136 | 0, /* tp_setattr */ |
| 137 | 0, /* tp_reserved */ |
| 138 | (reprfunc)cell_repr, /* tp_repr */ |
| 139 | 0, /* tp_as_number */ |
| 140 | 0, /* tp_as_sequence */ |
| 141 | 0, /* tp_as_mapping */ |
| 142 | 0, /* tp_hash */ |
| 143 | 0, /* tp_call */ |
| 144 | 0, /* tp_str */ |
| 145 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 146 | 0, /* tp_setattro */ |
| 147 | 0, /* tp_as_buffer */ |
Benjamin Peterson | 07451dd | 2016-05-12 23:12:21 -0700 | [diff] [blame] | 148 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | 0, /* tp_doc */ |
| 150 | (traverseproc)cell_traverse, /* tp_traverse */ |
| 151 | (inquiry)cell_clear, /* tp_clear */ |
| 152 | cell_richcompare, /* tp_richcompare */ |
| 153 | 0, /* tp_weaklistoffset */ |
| 154 | 0, /* tp_iter */ |
| 155 | 0, /* tp_iternext */ |
| 156 | 0, /* tp_methods */ |
| 157 | 0, /* tp_members */ |
| 158 | cell_getsetlist, /* tp_getset */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 159 | }; |