Bug 1277: make Maildir use the user-provided factory instead of hard-wiring MaildirMe...
[pytest.git] / Objects / cellobject.c
blobe659555f56b4c72b048446e532bacd9c8f6b42b5
1 /* Cell object implementation */
3 #include "Python.h"
5 PyObject *
6 PyCell_New(PyObject *obj)
8 PyCellObject *op;
10 op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
11 if (op == NULL)
12 return NULL;
13 op->ob_ref = obj;
14 Py_XINCREF(obj);
16 _PyObject_GC_TRACK(op);
17 return (PyObject *)op;
20 PyObject *
21 PyCell_Get(PyObject *op)
23 if (!PyCell_Check(op)) {
24 PyErr_BadInternalCall();
25 return NULL;
27 Py_XINCREF(((PyCellObject*)op)->ob_ref);
28 return PyCell_GET(op);
31 int
32 PyCell_Set(PyObject *op, PyObject *obj)
34 if (!PyCell_Check(op)) {
35 PyErr_BadInternalCall();
36 return -1;
38 Py_XDECREF(((PyCellObject*)op)->ob_ref);
39 Py_XINCREF(obj);
40 PyCell_SET(op, obj);
41 return 0;
44 static void
45 cell_dealloc(PyCellObject *op)
47 _PyObject_GC_UNTRACK(op);
48 Py_XDECREF(op->ob_ref);
49 PyObject_GC_Del(op);
52 static int
53 cell_compare(PyCellObject *a, PyCellObject *b)
55 if (a->ob_ref == NULL) {
56 if (b->ob_ref == NULL)
57 return 0;
58 return -1;
59 } else if (b->ob_ref == NULL)
60 return 1;
61 return PyObject_Compare(a->ob_ref, b->ob_ref);
64 static PyObject *
65 cell_repr(PyCellObject *op)
67 if (op->ob_ref == NULL)
68 return PyString_FromFormat("<cell at %p: empty>", op);
70 return PyString_FromFormat("<cell at %p: %.80s object at %p>",
71 op, op->ob_ref->ob_type->tp_name,
72 op->ob_ref);
75 static int
76 cell_traverse(PyCellObject *op, visitproc visit, void *arg)
78 Py_VISIT(op->ob_ref);
79 return 0;
82 static int
83 cell_clear(PyCellObject *op)
85 Py_CLEAR(op->ob_ref);
86 return 0;
89 static PyObject *
90 cell_get_contents(PyCellObject *op, void *closure)
92 if (op->ob_ref == NULL)
94 PyErr_SetString(PyExc_ValueError, "Cell is empty");
95 return NULL;
97 Py_INCREF(op->ob_ref);
98 return op->ob_ref;
101 static PyGetSetDef cell_getsetlist[] = {
102 {"cell_contents", (getter)cell_get_contents, NULL},
103 {NULL} /* sentinel */
106 PyTypeObject PyCell_Type = {
107 PyObject_HEAD_INIT(&PyType_Type)
109 "cell",
110 sizeof(PyCellObject),
112 (destructor)cell_dealloc, /* tp_dealloc */
113 0, /* tp_print */
114 0, /* tp_getattr */
115 0, /* tp_setattr */
116 (cmpfunc)cell_compare, /* tp_compare */
117 (reprfunc)cell_repr, /* tp_repr */
118 0, /* tp_as_number */
119 0, /* tp_as_sequence */
120 0, /* tp_as_mapping */
121 0, /* tp_hash */
122 0, /* tp_call */
123 0, /* tp_str */
124 PyObject_GenericGetAttr, /* tp_getattro */
125 0, /* tp_setattro */
126 0, /* tp_as_buffer */
127 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
128 0, /* tp_doc */
129 (traverseproc)cell_traverse, /* tp_traverse */
130 (inquiry)cell_clear, /* tp_clear */
131 0, /* tp_richcompare */
132 0, /* tp_weaklistoffset */
133 0, /* tp_iter */
134 0, /* tp_iternext */
135 0, /* tp_methods */
136 0, /* tp_members */
137 cell_getsetlist, /* tp_getset */