Apparently the code to forestall Tk eating events was too aggressive (Tk user input...
[python/dscho.git] / Objects / cellobject.c
blob66fc8d1d52134c0165371a82501bea407ef08102
1 /* Cell object implementation */
3 #include "Python.h"
5 PyObject *
6 PyCell_New(PyObject *obj)
8 PyCellObject *op;
10 op = (PyCellObject *)PyObject_New(PyCellObject, &PyCell_Type);
11 op->ob_ref = obj;
12 Py_XINCREF(obj);
14 PyObject_GC_Init(op);
15 return (PyObject *)op;
18 PyObject *
19 PyCell_Get(PyObject *op)
21 if (!PyCell_Check(op)) {
22 PyErr_BadInternalCall();
23 return NULL;
25 Py_XINCREF(((PyCellObject*)op)->ob_ref);
26 return PyCell_GET(op);
29 int
30 PyCell_Set(PyObject *op, PyObject *obj)
32 if (!PyCell_Check(op)) {
33 PyErr_BadInternalCall();
34 return -1;
36 Py_XDECREF(((PyCellObject*)op)->ob_ref);
37 Py_XINCREF(obj);
38 PyCell_SET(op, obj);
39 return 0;
42 static void
43 cell_dealloc(PyCellObject *op)
45 PyObject_GC_Fini(op);
46 Py_XDECREF(op->ob_ref);
47 PyObject_Del(op);
50 static int
51 cell_compare(PyCellObject *a, PyCellObject *b)
53 if (a->ob_ref == NULL) {
54 if (b->ob_ref == NULL)
55 return 0;
56 return -1;
57 } else if (b->ob_ref == NULL)
58 return 1;
59 return PyObject_Compare(a->ob_ref, b->ob_ref);
62 static PyObject *
63 cell_repr(PyCellObject *op)
65 char buf[256];
67 if (op->ob_ref == NULL)
68 sprintf(buf, "<cell at %p: empty>", op);
69 else
70 sprintf(buf, "<cell at %p: %.80s object at %p>",
71 op, op->ob_ref->ob_type->tp_name, op->ob_ref);
72 return PyString_FromString(buf);
75 static int
76 cell_traverse(PyCellObject *op, visitproc visit, void *arg)
78 if (op->ob_ref)
79 return visit(op->ob_ref, arg);
80 return 0;
83 static int
84 cell_clear(PyCellObject *op)
86 Py_XDECREF(op->ob_ref);
87 op->ob_ref = NULL;
88 return 0;
91 PyTypeObject PyCell_Type = {
92 PyObject_HEAD_INIT(&PyType_Type)
94 "cell",
95 sizeof(PyCellObject) + PyGC_HEAD_SIZE,
97 (destructor)cell_dealloc, /* tp_dealloc */
98 0, /* tp_print */
99 0, /* tp_getattr */
100 0, /* tp_setattr */
101 (cmpfunc)cell_compare, /* tp_compare */
102 (reprfunc)cell_repr, /* tp_repr */
103 0, /* tp_as_number */
104 0, /* tp_as_sequence */
105 0, /* tp_as_mapping */
106 0, /* tp_hash */
107 0, /* tp_call */
108 0, /* tp_str */
109 0, /* tp_getattro */
110 0, /* tp_setattro */
111 0, /* tp_as_buffer */
112 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
113 0, /* tp_doc */
114 (traverseproc)cell_traverse, /* tp_traverse */
115 (inquiry)cell_clear, /* tp_clear */