2001-01-25 16:04:14 -04:00
|
|
|
/* Cell object implementation */
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyCell_New(PyObject *obj)
|
|
|
|
{
|
|
|
|
PyCellObject *op;
|
|
|
|
|
2001-08-29 20:54:21 -03:00
|
|
|
op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
|
2006-06-30 04:32:46 -03:00
|
|
|
if (op == NULL)
|
|
|
|
return NULL;
|
2001-01-25 16:04:14 -04:00
|
|
|
op->ob_ref = obj;
|
|
|
|
Py_XINCREF(obj);
|
|
|
|
|
2001-08-29 20:54:21 -03:00
|
|
|
_PyObject_GC_TRACK(op);
|
2001-01-25 16:04:14 -04:00
|
|
|
return (PyObject *)op;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyCell_Get(PyObject *op)
|
|
|
|
{
|
|
|
|
if (!PyCell_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_XINCREF(((PyCellObject*)op)->ob_ref);
|
|
|
|
return PyCell_GET(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PyCell_Set(PyObject *op, PyObject *obj)
|
|
|
|
{
|
2008-02-16 16:55:24 -04:00
|
|
|
PyObject* oldobj;
|
2001-01-25 16:04:14 -04:00
|
|
|
if (!PyCell_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
2008-02-16 16:55:24 -04:00
|
|
|
oldobj = PyCell_GET(op);
|
2001-01-25 16:04:14 -04:00
|
|
|
Py_XINCREF(obj);
|
|
|
|
PyCell_SET(op, obj);
|
2008-02-16 16:55:24 -04:00
|
|
|
Py_XDECREF(oldobj);
|
2001-01-25 16:04:14 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cell_dealloc(PyCellObject *op)
|
|
|
|
{
|
2001-08-29 20:54:21 -03:00
|
|
|
_PyObject_GC_UNTRACK(op);
|
2001-01-25 16:04:14 -04:00
|
|
|
Py_XDECREF(op->ob_ref);
|
2001-08-29 20:54:21 -03:00
|
|
|
PyObject_GC_Del(op);
|
2001-01-25 16:04:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cell_compare(PyCellObject *a, PyCellObject *b)
|
|
|
|
{
|
2008-03-18 14:26:10 -03:00
|
|
|
/* Py3K warning for comparisons */
|
2008-04-27 00:01:45 -03:00
|
|
|
if (PyErr_WarnPy3k("cell comparisons not supported in 3.x",
|
2008-04-27 15:40:21 -03:00
|
|
|
1) < 0) {
|
2008-03-18 16:03:50 -03:00
|
|
|
return -2;
|
2008-03-18 14:26:10 -03:00
|
|
|
}
|
|
|
|
|
2001-01-25 16:04:14 -04:00
|
|
|
if (a->ob_ref == NULL) {
|
|
|
|
if (b->ob_ref == NULL)
|
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
} else if (b->ob_ref == NULL)
|
|
|
|
return 1;
|
|
|
|
return PyObject_Compare(a->ob_ref, b->ob_ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
cell_repr(PyCellObject *op)
|
|
|
|
{
|
|
|
|
if (op->ob_ref == NULL)
|
2008-05-26 09:51:38 -03:00
|
|
|
return PyBytes_FromFormat("<cell at %p: empty>", op);
|
2001-08-24 15:34:26 -03:00
|
|
|
|
2008-05-26 09:51:38 -03:00
|
|
|
return PyBytes_FromFormat("<cell at %p: %.80s object at %p>",
|
2001-08-24 15:34:26 -03:00
|
|
|
op, op->ob_ref->ob_type->tp_name,
|
|
|
|
op->ob_ref);
|
2001-01-25 16:04:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cell_traverse(PyCellObject *op, visitproc visit, void *arg)
|
|
|
|
{
|
2006-04-15 18:47:09 -03:00
|
|
|
Py_VISIT(op->ob_ref);
|
2001-01-25 16:04:14 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cell_clear(PyCellObject *op)
|
|
|
|
{
|
2006-04-15 14:28:34 -03:00
|
|
|
Py_CLEAR(op->ob_ref);
|
2001-01-25 16:04:14 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-18 03:59:59 -04:00
|
|
|
static PyObject *
|
|
|
|
cell_get_contents(PyCellObject *op, void *closure)
|
|
|
|
{
|
2007-11-24 09:44:17 -04:00
|
|
|
if (op->ob_ref == NULL)
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Cell is empty");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(op->ob_ref);
|
2006-03-18 03:59:59 -04:00
|
|
|
return op->ob_ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef cell_getsetlist[] = {
|
|
|
|
{"cell_contents", (getter)cell_get_contents, NULL},
|
|
|
|
{NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
2001-01-25 16:04:14 -04:00
|
|
|
PyTypeObject PyCell_Type = {
|
2007-07-21 03:55:02 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2001-01-25 16:04:14 -04:00
|
|
|
"cell",
|
2001-08-29 20:54:21 -03:00
|
|
|
sizeof(PyCellObject),
|
2001-01-25 16:04:14 -04:00
|
|
|
0,
|
|
|
|
(destructor)cell_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
(cmpfunc)cell_compare, /* tp_compare */
|
|
|
|
(reprfunc)cell_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
2001-01-25 16:04:14 -04:00
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2001-08-29 20:54:21 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
|
2001-01-25 16:04:14 -04:00
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)cell_traverse, /* tp_traverse */
|
|
|
|
(inquiry)cell_clear, /* tp_clear */
|
2006-03-18 03:59:59 -04:00
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
cell_getsetlist, /* tp_getset */
|
2001-01-25 16:04:14 -04:00
|
|
|
};
|