blob: 50a3897f48a23a70095d059beac8d9eb50f8aee2 [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);
Thomas Wouters0e3f5912006-08-11 14:57:12 +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{
Christian Heimes18c66892008-02-17 13:31:39 +000034 PyObject* oldobj;
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000035 if (!PyCell_Check(op)) {
36 PyErr_BadInternalCall();
37 return -1;
38 }
Christian Heimes18c66892008-02-17 13:31:39 +000039 oldobj = PyCell_GET(op);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000040 Py_XINCREF(obj);
41 PyCell_SET(op, obj);
Christian Heimes18c66892008-02-17 13:31:39 +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
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000054static int
55cell_compare(PyCellObject *a, PyCellObject *b)
56{
57 if (a->ob_ref == NULL) {
58 if (b->ob_ref == NULL)
59 return 0;
60 return -1;
61 } else if (b->ob_ref == NULL)
62 return 1;
63 return PyObject_Compare(a->ob_ref, b->ob_ref);
64}
65
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000066static PyObject *
67cell_repr(PyCellObject *op)
68{
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000069 if (op->ob_ref == NULL)
Walter Dörwald1ab83302007-05-18 17:15:44 +000070 return PyUnicode_FromFormat("<cell at %p: empty>", op);
Barry Warsaw7ce36942001-08-24 18:34:26 +000071
Walter Dörwald1ab83302007-05-18 17:15:44 +000072 return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +000073 op, op->ob_ref->ob_type->tp_name,
74 op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000075}
76
77static int
78cell_traverse(PyCellObject *op, visitproc visit, void *arg)
79{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000080 Py_VISIT(op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000081 return 0;
82}
83
84static int
85cell_clear(PyCellObject *op)
86{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000087 Py_CLEAR(op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000088 return 0;
89}
90
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000091static PyObject *
92cell_get_contents(PyCellObject *op, void *closure)
93{
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000094 if (op->ob_ref == NULL)
95 {
96 PyErr_SetString(PyExc_ValueError, "Cell is empty");
97 return NULL;
98 }
99 Py_INCREF(op->ob_ref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000100 return op->ob_ref;
101}
102
103static PyGetSetDef cell_getsetlist[] = {
104 {"cell_contents", (getter)cell_get_contents, NULL},
105 {NULL} /* sentinel */
106};
107
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000108PyTypeObject PyCell_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000109 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000110 "cell",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000111 sizeof(PyCellObject),
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000112 0,
113 (destructor)cell_dealloc, /* tp_dealloc */
114 0, /* tp_print */
115 0, /* tp_getattr */
116 0, /* tp_setattr */
Christian Heimesd5e2b6f2008-03-19 21:50:51 +0000117 (cmpfunc)cell_compare, /* tp_compare */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000118 (reprfunc)cell_repr, /* tp_repr */
119 0, /* tp_as_number */
120 0, /* tp_as_sequence */
121 0, /* tp_as_mapping */
122 0, /* tp_hash */
123 0, /* tp_call */
124 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000125 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000126 0, /* tp_setattro */
127 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000128 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000129 0, /* tp_doc */
130 (traverseproc)cell_traverse, /* tp_traverse */
131 (inquiry)cell_clear, /* tp_clear */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132 0, /* tp_richcompare */
133 0, /* tp_weaklistoffset */
134 0, /* tp_iter */
135 0, /* tp_iternext */
136 0, /* tp_methods */
137 0, /* tp_members */
138 cell_getsetlist, /* tp_getset */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000139};