/* Cell object implementation */
#include "Python.h"
#include "pycore_object.h"
PyObject *
PyCell_New(PyObject *obj)
{
PyCellObject *op;
op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
if (op == NULL)
return NULL;
op->ob_ref = Py_XNewRef(obj);
_PyObject_GC_TRACK(op);
return (PyObject *)op;
}
PyDoc_STRVAR(cell_new_doc,
"cell([contents])\n"
"--\n"
"\n"
"Create a new cell object.\n"
"\n"
" contents\n"
" the contents of the cell. If not specified, the cell will be empty,\n"
" and \n further attempts to access its cell_contents attribute will\n"
" raise a ValueError.");
static PyObject *
cell_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
PyObject *obj = NULL;
if (!_PyArg_NoKeywords("cell", kwargs)) {
goto exit;
}
/* min = 0: we allow the cell to be empty */
if (!PyArg_UnpackTuple(args, "cell", 0, 1, &obj)) {
goto exit;
}
return_value = PyCell_New(obj);
exit:
return return_value;
}
PyObject *
PyCell_Get(PyObject *op)
{
if (!PyCell_Check(op)) {
PyErr_BadInternalCall();
return NULL;
}
PyObject *value = PyCell_GET(op);
return Py_XNewRef(value);
}
int
PyCell_Set(PyObject *op, PyObject *value)
{
if (!PyCell_Check(op)) {
PyErr_BadInternalCall();
return -1;
}
PyObject *old_value = PyCell_GET(op);
PyCell_SET(op, Py_XNewRef(value));
Py_XDECREF(old_value);
return 0;
}
static void
cell_dealloc(PyCellObject *op)
{
_PyObject_GC_UNTRACK(op);
Py_XDECREF(op->ob_ref);
PyObject_GC_Del(op);
}
static PyObject *
cell_richcompare(PyObject *a, PyObject *b, int op)
{
/* neither argument should be NULL, unless something's gone wrong */
assert(a != NULL && b != NULL);
/* both arguments should be instances of PyCellObject */
if (!PyCell_Check(a) || !PyCell_Check(b)) {
Py_RETURN_NOTIMPLEMENTED;
}
/* compare cells by contents; empty cells come before anything else */
a = ((PyCellObject *)a)->ob_ref;
b = ((PyCellObject *)b)->ob_ref;
if (a != NULL && b != NULL)
return PyObject_RichCompare(a, b, op);
Py_RETURN_RICHCOMPARE(b == NULL, a == NULL, op);
}
static PyObject *
cell_repr(PyCellObject *op)
{
if (op->ob_ref == NULL)
return PyUnicode_FromFormat("