1 /* Cell object implementation */
6 PyCell_New(PyObject
*obj
)
10 op
= (PyCellObject
*)PyObject_GC_New(PyCellObject
, &PyCell_Type
);
16 _PyObject_GC_TRACK(op
);
17 return (PyObject
*)op
;
21 PyCell_Get(PyObject
*op
)
23 if (!PyCell_Check(op
)) {
24 PyErr_BadInternalCall();
27 Py_XINCREF(((PyCellObject
*)op
)->ob_ref
);
28 return PyCell_GET(op
);
32 PyCell_Set(PyObject
*op
, PyObject
*obj
)
35 if (!PyCell_Check(op
)) {
36 PyErr_BadInternalCall();
39 oldobj
= PyCell_GET(op
);
47 cell_dealloc(PyCellObject
*op
)
49 _PyObject_GC_UNTRACK(op
);
50 Py_XDECREF(op
->ob_ref
);
54 #define TEST_COND(cond) ((cond) ? Py_True : Py_False)
57 cell_richcompare(PyObject
*a
, PyObject
*b
, int op
)
62 /* neither argument should be NULL, unless something's gone wrong */
63 assert(a
!= NULL
&& b
!= NULL
);
65 /* both arguments should be instances of PyCellObject */
66 if (!PyCell_Check(a
) || !PyCell_Check(b
)) {
67 v
= Py_NotImplemented
;
72 /* compare cells by contents; empty cells come before anything else */
73 a
= ((PyCellObject
*)a
)->ob_ref
;
74 b
= ((PyCellObject
*)b
)->ob_ref
;
75 if (a
!= NULL
&& b
!= NULL
)
76 return PyObject_RichCompare(a
, b
, op
);
78 result
= (b
== NULL
) - (a
== NULL
);
81 v
= TEST_COND(result
== 0);
84 v
= TEST_COND(result
!= 0);
87 v
= TEST_COND(result
<= 0);
90 v
= TEST_COND(result
>= 0);
93 v
= TEST_COND(result
< 0);
96 v
= TEST_COND(result
> 0);
107 cell_repr(PyCellObject
*op
)
109 if (op
->ob_ref
== NULL
)
110 return PyUnicode_FromFormat("<cell at %p: empty>", op
);
112 return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>",
113 op
, op
->ob_ref
->ob_type
->tp_name
,
118 cell_traverse(PyCellObject
*op
, visitproc visit
, void *arg
)
120 Py_VISIT(op
->ob_ref
);
125 cell_clear(PyCellObject
*op
)
127 Py_CLEAR(op
->ob_ref
);
132 cell_get_contents(PyCellObject
*op
, void *closure
)
134 if (op
->ob_ref
== NULL
)
136 PyErr_SetString(PyExc_ValueError
, "Cell is empty");
139 Py_INCREF(op
->ob_ref
);
143 static PyGetSetDef cell_getsetlist
[] = {
144 {"cell_contents", (getter
)cell_get_contents
, NULL
},
145 {NULL
} /* sentinel */
148 PyTypeObject PyCell_Type
= {
149 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
151 sizeof(PyCellObject
),
153 (destructor
)cell_dealloc
, /* tp_dealloc */
158 (reprfunc
)cell_repr
, /* tp_repr */
159 0, /* tp_as_number */
160 0, /* tp_as_sequence */
161 0, /* tp_as_mapping */
165 PyObject_GenericGetAttr
, /* tp_getattro */
167 0, /* tp_as_buffer */
168 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
,/* tp_flags */
170 (traverseproc
)cell_traverse
, /* tp_traverse */
171 (inquiry
)cell_clear
, /* tp_clear */
172 cell_richcompare
, /* tp_richcompare */
173 0, /* tp_weaklistoffset */
178 cell_getsetlist
, /* tp_getset */