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