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