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 | { |
| 92 | Py_XINCREF(op->ob_ref); |
| 93 | return op->ob_ref; |
| 94 | } |
| 95 | |
| 96 | static PyGetSetDef cell_getsetlist[] = { |
| 97 | {"cell_contents", (getter)cell_get_contents, NULL}, |
| 98 | {NULL} /* sentinel */ |
| 99 | }; |
| 100 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 101 | PyTypeObject PyCell_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 102 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 103 | "cell", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 104 | sizeof(PyCellObject), |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 105 | 0, |
| 106 | (destructor)cell_dealloc, /* tp_dealloc */ |
| 107 | 0, /* tp_print */ |
| 108 | 0, /* tp_getattr */ |
| 109 | 0, /* tp_setattr */ |
| 110 | (cmpfunc)cell_compare, /* tp_compare */ |
| 111 | (reprfunc)cell_repr, /* tp_repr */ |
| 112 | 0, /* tp_as_number */ |
| 113 | 0, /* tp_as_sequence */ |
| 114 | 0, /* tp_as_mapping */ |
| 115 | 0, /* tp_hash */ |
| 116 | 0, /* tp_call */ |
| 117 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 118 | PyObject_GenericGetAttr, /* tp_getattro */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 119 | 0, /* tp_setattro */ |
| 120 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 121 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 122 | 0, /* tp_doc */ |
| 123 | (traverseproc)cell_traverse, /* tp_traverse */ |
| 124 | (inquiry)cell_clear, /* tp_clear */ |
Georg Brandl | abd1ff8 | 2006-03-18 07:59:59 +0000 | [diff] [blame] | 125 | 0, /* tp_richcompare */ |
| 126 | 0, /* tp_weaklistoffset */ |
| 127 | 0, /* tp_iter */ |
| 128 | 0, /* tp_iternext */ |
| 129 | 0, /* tp_methods */ |
| 130 | 0, /* tp_members */ |
| 131 | cell_getsetlist, /* tp_getset */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 132 | }; |