blob: 49843663adab17a27908c2edbbe84f4ab0061d9d [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{
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
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000052static PyObject *
53cell_repr(PyCellObject *op)
54{
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000055 if (op->ob_ref == NULL)
Walter Dörwald1ab83302007-05-18 17:15:44 +000056 return PyUnicode_FromFormat("<cell at %p: empty>", op);
Barry Warsaw7ce36942001-08-24 18:34:26 +000057
Walter Dörwald1ab83302007-05-18 17:15:44 +000058 return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +000059 op, op->ob_ref->ob_type->tp_name,
60 op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000061}
62
63static int
64cell_traverse(PyCellObject *op, visitproc visit, void *arg)
65{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000066 Py_VISIT(op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000067 return 0;
68}
69
70static int
71cell_clear(PyCellObject *op)
72{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073 Py_CLEAR(op->ob_ref);
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000074 return 0;
75}
76
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000077static PyObject *
78cell_get_contents(PyCellObject *op, void *closure)
79{
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000080 if (op->ob_ref == NULL)
81 {
82 PyErr_SetString(PyExc_ValueError, "Cell is empty");
83 return NULL;
84 }
85 Py_INCREF(op->ob_ref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 return op->ob_ref;
87}
88
89static PyGetSetDef cell_getsetlist[] = {
90 {"cell_contents", (getter)cell_get_contents, NULL},
91 {NULL} /* sentinel */
92};
93
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000094PyTypeObject PyCell_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000095 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000096 "cell",
Neil Schemenauere83c00e2001-08-29 23:54:21 +000097 sizeof(PyCellObject),
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +000098 0,
99 (destructor)cell_dealloc, /* tp_dealloc */
100 0, /* tp_print */
101 0, /* tp_getattr */
102 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000103 0, /* tp_compare */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000104 (reprfunc)cell_repr, /* tp_repr */
105 0, /* tp_as_number */
106 0, /* tp_as_sequence */
107 0, /* tp_as_mapping */
108 0, /* tp_hash */
109 0, /* tp_call */
110 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000111 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000112 0, /* tp_setattro */
113 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000114 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000115 0, /* tp_doc */
116 (traverseproc)cell_traverse, /* tp_traverse */
117 (inquiry)cell_clear, /* tp_clear */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 0, /* tp_richcompare */
119 0, /* tp_weaklistoffset */
120 0, /* tp_iter */
121 0, /* tp_iternext */
122 0, /* tp_methods */
123 0, /* tp_members */
124 cell_getsetlist, /* tp_getset */
Jeremy Hyltonfbd849f2001-01-25 20:04:14 +0000125};