blob: 490afe0b148549cb7022ffd3bc710a616523dbe8 [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
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000054static PyObject *
55cell_repr(PyCellObject *op)
56{
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000057 if (op->ob_ref == NULL)
Walter Dörwald1ab83302007-05-18 17:15:44 +000058 return PyUnicode_FromFormat("<cell at %p: empty>", op);
Barry Warsaw7ce36942001-08-24 18:34:26 +000059
Walter Dörwald1ab83302007-05-18 17:15:44 +000060 return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +000061 op, op->ob_ref->ob_type->tp_name,
62 op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000063}
64
65static int
66cell_traverse(PyCellObject *op, visitproc visit, void *arg)
67{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068 Py_VISIT(op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000069 return 0;
70}
71
72static int
73cell_clear(PyCellObject *op)
74{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000075 Py_CLEAR(op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000076 return 0;
77}
78
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000079static PyObject *
80cell_get_contents(PyCellObject *op, void *closure)
81{
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000082 if (op->ob_ref == NULL)
83 {
84 PyErr_SetString(PyExc_ValueError, "Cell is empty");
85 return NULL;
86 }
87 Py_INCREF(op->ob_ref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000088 return op->ob_ref;
89}
90
91static PyGetSetDef cell_getsetlist[] = {
92 {"cell_contents", (getter)cell_get_contents, NULL},
93 {NULL} /* sentinel */
94};
95
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000096PyTypeObject PyCell_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000097 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000098 "cell",
Neil Schemenauere83c00e2001-08-29 23:54:21 +000099 sizeof(PyCellObject),
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000100 0,
101 (destructor)cell_dealloc, /* tp_dealloc */
102 0, /* tp_print */
103 0, /* tp_getattr */
104 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000105 0, /* tp_compare */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000106 (reprfunc)cell_repr, /* tp_repr */
107 0, /* tp_as_number */
108 0, /* tp_as_sequence */
109 0, /* tp_as_mapping */
110 0, /* tp_hash */
111 0, /* tp_call */
112 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000113 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000114 0, /* tp_setattro */
115 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000116 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000117 0, /* tp_doc */
118 (traverseproc)cell_traverse, /* tp_traverse */
119 (inquiry)cell_clear, /* tp_clear */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120 0, /* tp_richcompare */
121 0, /* tp_weaklistoffset */
122 0, /* tp_iter */
123 0, /* tp_iternext */
124 0, /* tp_methods */
125 0, /* tp_members */
126 cell_getsetlist, /* tp_getset */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000127};