blob: 46955ab6e2ea635224f97a71ed17fe10c360ef75 [file] [log] [blame]
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +00001/* Cell object implementation */
2
3#include "Python.h"
4
5PyObject *
6PyCell_New(PyObject *obj)
7{
8 PyCellObject *op;
9
Neil Schemenauere83c00e2001-08-29 23:54:21 +000010 op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
Neal Norwitz0f415dc2006-06-30 07:32:46 +000011 if (op == NULL)
12 return NULL;
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000013 op->ob_ref = obj;
14 Py_XINCREF(obj);
15
Neil Schemenauere83c00e2001-08-29 23:54:21 +000016 _PyObject_GC_TRACK(op);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000017 return (PyObject *)op;
18}
19
20PyObject *
21PyCell_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
31int
32PyCell_Set(PyObject *op, PyObject *obj)
33{
Amaury Forgeot d'Arc632fad32008-02-16 20:55:24 +000034 PyObject* oldobj;
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000035 if (!PyCell_Check(op)) {
36 PyErr_BadInternalCall();
37 return -1;
38 }
Amaury Forgeot d'Arc632fad32008-02-16 20:55:24 +000039 oldobj = PyCell_GET(op);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000040 Py_XINCREF(obj);
41 PyCell_SET(op, obj);
Amaury Forgeot d'Arc632fad32008-02-16 20:55:24 +000042 Py_XDECREF(oldobj);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000043 return 0;
44}
45
46static void
47cell_dealloc(PyCellObject *op)
48{
Neil Schemenauere83c00e2001-08-29 23:54:21 +000049 _PyObject_GC_UNTRACK(op);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000050 Py_XDECREF(op->ob_ref);
Neil Schemenauere83c00e2001-08-29 23:54:21 +000051 PyObject_GC_Del(op);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000052}
53
54static int
55cell_compare(PyCellObject *a, PyCellObject *b)
56{
Steven Bethardae42f332008-03-18 17:26:10 +000057 /* Py3K warning for comparisons */
Georg Brandld5b635f2008-03-25 08:29:14 +000058 if (Py_Py3kWarningFlag &&
Georg Brandld65ab952008-03-25 08:31:32 +000059 PyErr_Warn(PyExc_DeprecationWarning,
Georg Brandld5b635f2008-03-25 08:29:14 +000060 "cell comparisons not supported in 3.x") < 0) {
Steven Bethardb865f052008-03-18 19:03:50 +000061 return -2;
Steven Bethardae42f332008-03-18 17:26:10 +000062 }
63
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000064 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
73static PyObject *
74cell_repr(PyCellObject *op)
75{
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000076 if (op->ob_ref == NULL)
Barry Warsaw7ce36942001-08-24 18:34:26 +000077 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 Hyltonfbd849f2001-01-25 20:04:14 +000082}
83
84static int
85cell_traverse(PyCellObject *op, visitproc visit, void *arg)
86{
Thomas Woutersc6e55062006-04-15 21:47:09 +000087 Py_VISIT(op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000088 return 0;
89}
90
91static int
92cell_clear(PyCellObject *op)
93{
Thomas Woutersedf17d82006-04-15 17:28:34 +000094 Py_CLEAR(op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000095 return 0;
96}
97
Georg Brandlabd1ff82006-03-18 07:59:59 +000098static PyObject *
99cell_get_contents(PyCellObject *op, void *closure)
100{
Amaury Forgeot d'Arcce7d10c2007-11-24 13:44:17 +0000101 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 Brandlabd1ff82006-03-18 07:59:59 +0000107 return op->ob_ref;
108}
109
110static PyGetSetDef cell_getsetlist[] = {
111 {"cell_contents", (getter)cell_get_contents, NULL},
112 {NULL} /* sentinel */
113};
114
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000115PyTypeObject PyCell_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000116 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000117 "cell",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000118 sizeof(PyCellObject),
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000119 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 Peters6d6c1a32001-08-02 04:15:00 +0000132 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000133 0, /* tp_setattro */
134 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000135 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000136 0, /* tp_doc */
137 (traverseproc)cell_traverse, /* tp_traverse */
138 (inquiry)cell_clear, /* tp_clear */
Georg Brandlabd1ff82006-03-18 07:59:59 +0000139 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 Hyltonfbd849f2001-01-25 20:04:14 +0000146};