blob: e659555f56b4c72b048446e532bacd9c8f6b42b5 [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{
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
44static void
45cell_dealloc(PyCellObject *op)
46{
Neil Schemenauere83c00e2001-08-29 23:54:21 +000047 _PyObject_GC_UNTRACK(op);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000048 Py_XDECREF(op->ob_ref);
Neil Schemenauere83c00e2001-08-29 23:54:21 +000049 PyObject_GC_Del(op);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000050}
51
52static int
53cell_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
64static PyObject *
65cell_repr(PyCellObject *op)
66{
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000067 if (op->ob_ref == NULL)
Barry Warsaw7ce36942001-08-24 18:34:26 +000068 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 Hyltonfbd849f2001-01-25 20:04:14 +000073}
74
75static int
76cell_traverse(PyCellObject *op, visitproc visit, void *arg)
77{
Thomas Woutersc6e55062006-04-15 21:47:09 +000078 Py_VISIT(op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000079 return 0;
80}
81
82static int
83cell_clear(PyCellObject *op)
84{
Thomas Woutersedf17d82006-04-15 17:28:34 +000085 Py_CLEAR(op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000086 return 0;
87}
88
Georg Brandlabd1ff82006-03-18 07:59:59 +000089static PyObject *
90cell_get_contents(PyCellObject *op, void *closure)
91{
Amaury Forgeot d'Arc30f61cb2007-11-24 13:53:29 +000092 if (op->ob_ref == NULL)
93 {
94 PyErr_SetString(PyExc_ValueError, "Cell is empty");
95 return NULL;
96 }
97 Py_INCREF(op->ob_ref);
Georg Brandlabd1ff82006-03-18 07:59:59 +000098 return op->ob_ref;
99}
100
101static PyGetSetDef cell_getsetlist[] = {
102 {"cell_contents", (getter)cell_get_contents, NULL},
103 {NULL} /* sentinel */
104};
105
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000106PyTypeObject PyCell_Type = {
107 PyObject_HEAD_INIT(&PyType_Type)
108 0,
109 "cell",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000110 sizeof(PyCellObject),
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000111 0,
112 (destructor)cell_dealloc, /* tp_dealloc */
113 0, /* tp_print */
114 0, /* tp_getattr */
115 0, /* tp_setattr */
116 (cmpfunc)cell_compare, /* tp_compare */
117 (reprfunc)cell_repr, /* tp_repr */
118 0, /* tp_as_number */
119 0, /* tp_as_sequence */
120 0, /* tp_as_mapping */
121 0, /* tp_hash */
122 0, /* tp_call */
123 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000124 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000125 0, /* tp_setattro */
126 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000127 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000128 0, /* tp_doc */
129 (traverseproc)cell_traverse, /* tp_traverse */
130 (inquiry)cell_clear, /* tp_clear */
Georg Brandlabd1ff82006-03-18 07:59:59 +0000131 0, /* tp_richcompare */
132 0, /* tp_weaklistoffset */
133 0, /* tp_iter */
134 0, /* tp_iternext */
135 0, /* tp_methods */
136 0, /* tp_members */
137 cell_getsetlist, /* tp_getset */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000138};