1 | n/a | /* Cell object implementation */ |
---|
2 | n/a | |
---|
3 | n/a | #include "Python.h" |
---|
4 | n/a | |
---|
5 | n/a | PyObject * |
---|
6 | n/a | PyCell_New(PyObject *obj) |
---|
7 | n/a | { |
---|
8 | n/a | PyCellObject *op; |
---|
9 | n/a | |
---|
10 | n/a | op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type); |
---|
11 | n/a | if (op == NULL) |
---|
12 | n/a | return NULL; |
---|
13 | n/a | op->ob_ref = obj; |
---|
14 | n/a | Py_XINCREF(obj); |
---|
15 | n/a | |
---|
16 | n/a | _PyObject_GC_TRACK(op); |
---|
17 | n/a | return (PyObject *)op; |
---|
18 | n/a | } |
---|
19 | n/a | |
---|
20 | n/a | PyObject * |
---|
21 | n/a | PyCell_Get(PyObject *op) |
---|
22 | n/a | { |
---|
23 | n/a | if (!PyCell_Check(op)) { |
---|
24 | n/a | PyErr_BadInternalCall(); |
---|
25 | n/a | return NULL; |
---|
26 | n/a | } |
---|
27 | n/a | Py_XINCREF(((PyCellObject*)op)->ob_ref); |
---|
28 | n/a | return PyCell_GET(op); |
---|
29 | n/a | } |
---|
30 | n/a | |
---|
31 | n/a | int |
---|
32 | n/a | PyCell_Set(PyObject *op, PyObject *obj) |
---|
33 | n/a | { |
---|
34 | n/a | PyObject* oldobj; |
---|
35 | n/a | if (!PyCell_Check(op)) { |
---|
36 | n/a | PyErr_BadInternalCall(); |
---|
37 | n/a | return -1; |
---|
38 | n/a | } |
---|
39 | n/a | oldobj = PyCell_GET(op); |
---|
40 | n/a | Py_XINCREF(obj); |
---|
41 | n/a | PyCell_SET(op, obj); |
---|
42 | n/a | Py_XDECREF(oldobj); |
---|
43 | n/a | return 0; |
---|
44 | n/a | } |
---|
45 | n/a | |
---|
46 | n/a | static void |
---|
47 | n/a | cell_dealloc(PyCellObject *op) |
---|
48 | n/a | { |
---|
49 | n/a | _PyObject_GC_UNTRACK(op); |
---|
50 | n/a | Py_XDECREF(op->ob_ref); |
---|
51 | n/a | PyObject_GC_Del(op); |
---|
52 | n/a | } |
---|
53 | n/a | |
---|
54 | n/a | #define TEST_COND(cond) ((cond) ? Py_True : Py_False) |
---|
55 | n/a | |
---|
56 | n/a | static PyObject * |
---|
57 | n/a | cell_richcompare(PyObject *a, PyObject *b, int op) |
---|
58 | n/a | { |
---|
59 | n/a | int result; |
---|
60 | n/a | PyObject *v; |
---|
61 | n/a | |
---|
62 | n/a | /* neither argument should be NULL, unless something's gone wrong */ |
---|
63 | n/a | assert(a != NULL && b != NULL); |
---|
64 | n/a | |
---|
65 | n/a | /* both arguments should be instances of PyCellObject */ |
---|
66 | n/a | if (!PyCell_Check(a) || !PyCell_Check(b)) { |
---|
67 | n/a | v = Py_NotImplemented; |
---|
68 | n/a | Py_INCREF(v); |
---|
69 | n/a | return v; |
---|
70 | n/a | } |
---|
71 | n/a | |
---|
72 | n/a | /* compare cells by contents; empty cells come before anything else */ |
---|
73 | n/a | a = ((PyCellObject *)a)->ob_ref; |
---|
74 | n/a | b = ((PyCellObject *)b)->ob_ref; |
---|
75 | n/a | if (a != NULL && b != NULL) |
---|
76 | n/a | return PyObject_RichCompare(a, b, op); |
---|
77 | n/a | |
---|
78 | n/a | result = (b == NULL) - (a == NULL); |
---|
79 | n/a | switch (op) { |
---|
80 | n/a | case Py_EQ: |
---|
81 | n/a | v = TEST_COND(result == 0); |
---|
82 | n/a | break; |
---|
83 | n/a | case Py_NE: |
---|
84 | n/a | v = TEST_COND(result != 0); |
---|
85 | n/a | break; |
---|
86 | n/a | case Py_LE: |
---|
87 | n/a | v = TEST_COND(result <= 0); |
---|
88 | n/a | break; |
---|
89 | n/a | case Py_GE: |
---|
90 | n/a | v = TEST_COND(result >= 0); |
---|
91 | n/a | break; |
---|
92 | n/a | case Py_LT: |
---|
93 | n/a | v = TEST_COND(result < 0); |
---|
94 | n/a | break; |
---|
95 | n/a | case Py_GT: |
---|
96 | n/a | v = TEST_COND(result > 0); |
---|
97 | n/a | break; |
---|
98 | n/a | default: |
---|
99 | n/a | PyErr_BadArgument(); |
---|
100 | n/a | return NULL; |
---|
101 | n/a | } |
---|
102 | n/a | Py_INCREF(v); |
---|
103 | n/a | return v; |
---|
104 | n/a | } |
---|
105 | n/a | |
---|
106 | n/a | static PyObject * |
---|
107 | n/a | cell_repr(PyCellObject *op) |
---|
108 | n/a | { |
---|
109 | n/a | if (op->ob_ref == NULL) |
---|
110 | n/a | return PyUnicode_FromFormat("<cell at %p: empty>", op); |
---|
111 | n/a | |
---|
112 | n/a | return PyUnicode_FromFormat("<cell at %p: %.80s object at %p>", |
---|
113 | n/a | op, op->ob_ref->ob_type->tp_name, |
---|
114 | n/a | op->ob_ref); |
---|
115 | n/a | } |
---|
116 | n/a | |
---|
117 | n/a | static int |
---|
118 | n/a | cell_traverse(PyCellObject *op, visitproc visit, void *arg) |
---|
119 | n/a | { |
---|
120 | n/a | Py_VISIT(op->ob_ref); |
---|
121 | n/a | return 0; |
---|
122 | n/a | } |
---|
123 | n/a | |
---|
124 | n/a | static int |
---|
125 | n/a | cell_clear(PyCellObject *op) |
---|
126 | n/a | { |
---|
127 | n/a | Py_CLEAR(op->ob_ref); |
---|
128 | n/a | return 0; |
---|
129 | n/a | } |
---|
130 | n/a | |
---|
131 | n/a | static PyObject * |
---|
132 | n/a | cell_get_contents(PyCellObject *op, void *closure) |
---|
133 | n/a | { |
---|
134 | n/a | if (op->ob_ref == NULL) |
---|
135 | n/a | { |
---|
136 | n/a | PyErr_SetString(PyExc_ValueError, "Cell is empty"); |
---|
137 | n/a | return NULL; |
---|
138 | n/a | } |
---|
139 | n/a | Py_INCREF(op->ob_ref); |
---|
140 | n/a | return op->ob_ref; |
---|
141 | n/a | } |
---|
142 | n/a | |
---|
143 | n/a | static PyGetSetDef cell_getsetlist[] = { |
---|
144 | n/a | {"cell_contents", (getter)cell_get_contents, NULL}, |
---|
145 | n/a | {NULL} /* sentinel */ |
---|
146 | n/a | }; |
---|
147 | n/a | |
---|
148 | n/a | PyTypeObject PyCell_Type = { |
---|
149 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
150 | n/a | "cell", |
---|
151 | n/a | sizeof(PyCellObject), |
---|
152 | n/a | 0, |
---|
153 | n/a | (destructor)cell_dealloc, /* tp_dealloc */ |
---|
154 | n/a | 0, /* tp_print */ |
---|
155 | n/a | 0, /* tp_getattr */ |
---|
156 | n/a | 0, /* tp_setattr */ |
---|
157 | n/a | 0, /* tp_reserved */ |
---|
158 | n/a | (reprfunc)cell_repr, /* tp_repr */ |
---|
159 | n/a | 0, /* tp_as_number */ |
---|
160 | n/a | 0, /* tp_as_sequence */ |
---|
161 | n/a | 0, /* tp_as_mapping */ |
---|
162 | n/a | 0, /* tp_hash */ |
---|
163 | n/a | 0, /* tp_call */ |
---|
164 | n/a | 0, /* tp_str */ |
---|
165 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
166 | n/a | 0, /* tp_setattro */ |
---|
167 | n/a | 0, /* tp_as_buffer */ |
---|
168 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
---|
169 | n/a | 0, /* tp_doc */ |
---|
170 | n/a | (traverseproc)cell_traverse, /* tp_traverse */ |
---|
171 | n/a | (inquiry)cell_clear, /* tp_clear */ |
---|
172 | n/a | cell_richcompare, /* tp_richcompare */ |
---|
173 | n/a | 0, /* tp_weaklistoffset */ |
---|
174 | n/a | 0, /* tp_iter */ |
---|
175 | n/a | 0, /* tp_iternext */ |
---|
176 | n/a | 0, /* tp_methods */ |
---|
177 | n/a | 0, /* tp_members */ |
---|
178 | n/a | cell_getsetlist, /* tp_getset */ |
---|
179 | n/a | }; |
---|