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); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +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 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 52 | static PyObject * |
| 53 | cell_repr(PyCellObject *op) |
| 54 | { |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 55 | if (op->ob_ref == NULL) |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 56 | return PyUnicode_FromFormat("<cell at %p: empty>", op); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 57 | |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 58 | return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 59 | op, op->ob_ref->ob_type->tp_name, |
| 60 | op->ob_ref); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static int |
| 64 | cell_traverse(PyCellObject *op, visitproc visit, void *arg) |
| 65 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 66 | Py_VISIT(op->ob_ref); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int |
| 71 | cell_clear(PyCellObject *op) |
| 72 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 73 | Py_CLEAR(op->ob_ref); |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 77 | static PyObject * |
| 78 | cell_get_contents(PyCellObject *op, void *closure) |
| 79 | { |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 80 | if (op->ob_ref == NULL) |
| 81 | { |
| 82 | PyErr_SetString(PyExc_ValueError, "Cell is empty"); |
| 83 | return NULL; |
| 84 | } |
| 85 | Py_INCREF(op->ob_ref); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 86 | return op->ob_ref; |
| 87 | } |
| 88 | |
| 89 | static PyGetSetDef cell_getsetlist[] = { |
| 90 | {"cell_contents", (getter)cell_get_contents, NULL}, |
| 91 | {NULL} /* sentinel */ |
| 92 | }; |
| 93 | |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 94 | PyTypeObject PyCell_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 95 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 96 | "cell", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 97 | sizeof(PyCellObject), |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 98 | 0, |
| 99 | (destructor)cell_dealloc, /* tp_dealloc */ |
| 100 | 0, /* tp_print */ |
| 101 | 0, /* tp_getattr */ |
| 102 | 0, /* tp_setattr */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 103 | 0, /* tp_compare */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 104 | (reprfunc)cell_repr, /* tp_repr */ |
| 105 | 0, /* tp_as_number */ |
| 106 | 0, /* tp_as_sequence */ |
| 107 | 0, /* tp_as_mapping */ |
| 108 | 0, /* tp_hash */ |
| 109 | 0, /* tp_call */ |
| 110 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 111 | PyObject_GenericGetAttr, /* tp_getattro */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 112 | 0, /* tp_setattro */ |
| 113 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 114 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 115 | 0, /* tp_doc */ |
| 116 | (traverseproc)cell_traverse, /* tp_traverse */ |
| 117 | (inquiry)cell_clear, /* tp_clear */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 118 | 0, /* tp_richcompare */ |
| 119 | 0, /* tp_weaklistoffset */ |
| 120 | 0, /* tp_iter */ |
| 121 | 0, /* tp_iternext */ |
| 122 | 0, /* tp_methods */ |
| 123 | 0, /* tp_members */ |
| 124 | cell_getsetlist, /* tp_getset */ |
Jeremy Hylton | fbd849f | 2001-01-25 20:04:14 +0000 | [diff] [blame] | 125 | }; |