Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 1 | /* Cell object implementation */ |
| 2 | |
| 3 | #include "Python.h" |
| 4 | |
| 5 | PyObject * |
| 6 | PyCell_New(PyObject *obj) |
| 7 | { |
| 8 | PyCellObject *op; |
| 9 | |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 10 | op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type); |
Neal Norwitz | 0f415dc | 2006-06-30 07:32:46 +0000 | [diff] [blame] | 11 | if (op == NULL) |
| 12 | return NULL; |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 13 | op->ob_ref = obj; |
| 14 | Py_XINCREF(obj); |
| 15 | |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 16 | _PyObject_GC_TRACK(op); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 17 | return (PyObject *)op; |
| 18 | } |
| 19 | |
| 20 | PyObject * |
| 21 | PyCell_Get(PyObject *op) |
| 22 | { |
| 23 | if (!PyCell_Check(op)) { |
| 24 | PyErr_BadInternalCall(); |
| 25 | return NULL; |
| 26 | } |
| 27 | Py_XINCREF(((PyCellObject*)op)->ob_ref); |
| 28 | return PyCell_GET(op); |
| 29 | } |
| 30 | |
| 31 | int |
| 32 | PyCell_Set(PyObject *op, PyObject *obj) |
| 33 | { |
| 34 | if (!PyCell_Check(op)) { |
| 35 | PyErr_BadInternalCall(); |
| 36 | return -1; |
| 37 | } |
| 38 | Py_XDECREF(((PyCellObject*)op)->ob_ref); |
| 39 | Py_XINCREF(obj); |
| 40 | PyCell_SET(op, obj); |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static void |
| 45 | cell_dealloc(PyCellObject *op) |
| 46 | { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 47 | _PyObject_GC_UNTRACK(op); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 48 | Py_XDECREF(op->ob_ref); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 49 | PyObject_GC_Del(op); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static int |
| 53 | cell_compare(PyCellObject *a, PyCellObject *b) |
| 54 | { |
| 55 | if (a->ob_ref == NULL) { |
| 56 | if (b->ob_ref == NULL) |
| 57 | return 0; |
| 58 | return -1; |
| 59 | } else if (b->ob_ref == NULL) |
| 60 | return 1; |
| 61 | return PyObject_Compare(a->ob_ref, b->ob_ref); |
| 62 | } |
| 63 | |
| 64 | static PyObject * |
| 65 | cell_repr(PyCellObject *op) |
| 66 | { |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 67 | if (op->ob_ref == NULL) |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 68 | return PyString_FromFormat("<cell at %p: empty>", op); |
| 69 | |
| 70 | return PyString_FromFormat("<cell at %p: %.80s object at %p>", |
| 71 | op, op->ob_ref->ob_type->tp_name, |
| 72 | op->ob_ref); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int |
| 76 | cell_traverse(PyCellObject *op, visitproc visit, void *arg) |
| 77 | { |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 78 | Py_VISIT(op->ob_ref); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int |
| 83 | cell_clear(PyCellObject *op) |
| 84 | { |
Thomas Wouters | edf17d8 | 2006-04-15 17:28:34 +0000 | [diff] [blame] | 85 | Py_CLEAR(op->ob_ref); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
Georg Brandl | abd1ff8 | 2006-03-18 07:59:59 +0000 | [diff] [blame] | 89 | static PyObject * |
| 90 | cell_get_contents(PyCellObject *op, void *closure) |
| 91 | { |
Amaury Forgeot d'Arc | ce7d10c | 2007-11-24 13:44:17 +0000 | [diff] [blame] | 92 | if (op->ob_ref == NULL) |
| 93 | { |
| 94 | PyErr_SetString(PyExc_ValueError, "Cell is empty"); |
| 95 | return NULL; |
| 96 | } |
| 97 | Py_INCREF(op->ob_ref); |
Georg Brandl | abd1ff8 | 2006-03-18 07:59:59 +0000 | [diff] [blame] | 98 | return op->ob_ref; |
| 99 | } |
| 100 | |
| 101 | static PyGetSetDef cell_getsetlist[] = { |
| 102 | {"cell_contents", (getter)cell_get_contents, NULL}, |
| 103 | {NULL} /* sentinel */ |
| 104 | }; |
| 105 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 106 | PyTypeObject PyCell_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 107 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 108 | "cell", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 109 | sizeof(PyCellObject), |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 110 | 0, |
| 111 | (destructor)cell_dealloc, /* tp_dealloc */ |
| 112 | 0, /* tp_print */ |
| 113 | 0, /* tp_getattr */ |
| 114 | 0, /* tp_setattr */ |
| 115 | (cmpfunc)cell_compare, /* tp_compare */ |
| 116 | (reprfunc)cell_repr, /* tp_repr */ |
| 117 | 0, /* tp_as_number */ |
| 118 | 0, /* tp_as_sequence */ |
| 119 | 0, /* tp_as_mapping */ |
| 120 | 0, /* tp_hash */ |
| 121 | 0, /* tp_call */ |
| 122 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 123 | PyObject_GenericGetAttr, /* tp_getattro */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 124 | 0, /* tp_setattro */ |
| 125 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 126 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 127 | 0, /* tp_doc */ |
| 128 | (traverseproc)cell_traverse, /* tp_traverse */ |
| 129 | (inquiry)cell_clear, /* tp_clear */ |
Georg Brandl | abd1ff8 | 2006-03-18 07:59:59 +0000 | [diff] [blame] | 130 | 0, /* tp_richcompare */ |
| 131 | 0, /* tp_weaklistoffset */ |
| 132 | 0, /* tp_iter */ |
| 133 | 0, /* tp_iternext */ |
| 134 | 0, /* tp_methods */ |
| 135 | 0, /* tp_members */ |
| 136 | cell_getsetlist, /* tp_getset */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 137 | }; |