]>
Commit | Line | Data |
---|---|---|
53b2ba57 DM |
1 | /* Cell object implementation */\r |
2 | \r | |
3 | #include "Python.h"\r | |
4 | \r | |
5 | PyObject *\r | |
6 | PyCell_New(PyObject *obj)\r | |
7 | {\r | |
8 | PyCellObject *op;\r | |
9 | \r | |
10 | op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);\r | |
11 | if (op == NULL)\r | |
12 | return NULL;\r | |
13 | op->ob_ref = obj;\r | |
14 | Py_XINCREF(obj);\r | |
15 | \r | |
16 | _PyObject_GC_TRACK(op);\r | |
17 | return (PyObject *)op;\r | |
18 | }\r | |
19 | \r | |
20 | PyObject *\r | |
21 | PyCell_Get(PyObject *op)\r | |
22 | {\r | |
23 | if (!PyCell_Check(op)) {\r | |
24 | PyErr_BadInternalCall();\r | |
25 | return NULL;\r | |
26 | }\r | |
27 | Py_XINCREF(((PyCellObject*)op)->ob_ref);\r | |
28 | return PyCell_GET(op);\r | |
29 | }\r | |
30 | \r | |
31 | int\r | |
32 | PyCell_Set(PyObject *op, PyObject *obj)\r | |
33 | {\r | |
34 | PyObject* oldobj;\r | |
35 | if (!PyCell_Check(op)) {\r | |
36 | PyErr_BadInternalCall();\r | |
37 | return -1;\r | |
38 | }\r | |
39 | oldobj = PyCell_GET(op);\r | |
40 | Py_XINCREF(obj);\r | |
41 | PyCell_SET(op, obj);\r | |
42 | Py_XDECREF(oldobj);\r | |
43 | return 0;\r | |
44 | }\r | |
45 | \r | |
46 | static void\r | |
47 | cell_dealloc(PyCellObject *op)\r | |
48 | {\r | |
49 | _PyObject_GC_UNTRACK(op);\r | |
50 | Py_XDECREF(op->ob_ref);\r | |
51 | PyObject_GC_Del(op);\r | |
52 | }\r | |
53 | \r | |
54 | static int\r | |
55 | cell_compare(PyCellObject *a, PyCellObject *b)\r | |
56 | {\r | |
57 | /* Py3K warning for comparisons */\r | |
58 | if (PyErr_WarnPy3k("cell comparisons not supported in 3.x",\r | |
59 | 1) < 0) {\r | |
60 | return -2;\r | |
61 | }\r | |
62 | \r | |
63 | if (a->ob_ref == NULL) {\r | |
64 | if (b->ob_ref == NULL)\r | |
65 | return 0;\r | |
66 | return -1;\r | |
67 | } else if (b->ob_ref == NULL)\r | |
68 | return 1;\r | |
69 | return PyObject_Compare(a->ob_ref, b->ob_ref);\r | |
70 | }\r | |
71 | \r | |
72 | static PyObject *\r | |
73 | cell_repr(PyCellObject *op)\r | |
74 | {\r | |
75 | if (op->ob_ref == NULL)\r | |
76 | return PyString_FromFormat("<cell at %p: empty>", op);\r | |
77 | \r | |
78 | return PyString_FromFormat("<cell at %p: %.80s object at %p>",\r | |
79 | op, op->ob_ref->ob_type->tp_name,\r | |
80 | op->ob_ref);\r | |
81 | }\r | |
82 | \r | |
83 | static int\r | |
84 | cell_traverse(PyCellObject *op, visitproc visit, void *arg)\r | |
85 | {\r | |
86 | Py_VISIT(op->ob_ref);\r | |
87 | return 0;\r | |
88 | }\r | |
89 | \r | |
90 | static int\r | |
91 | cell_clear(PyCellObject *op)\r | |
92 | {\r | |
93 | Py_CLEAR(op->ob_ref);\r | |
94 | return 0;\r | |
95 | }\r | |
96 | \r | |
97 | static PyObject *\r | |
98 | cell_get_contents(PyCellObject *op, void *closure)\r | |
99 | {\r | |
100 | if (op->ob_ref == NULL)\r | |
101 | {\r | |
102 | PyErr_SetString(PyExc_ValueError, "Cell is empty");\r | |
103 | return NULL;\r | |
104 | }\r | |
105 | Py_INCREF(op->ob_ref);\r | |
106 | return op->ob_ref;\r | |
107 | }\r | |
108 | \r | |
109 | static PyGetSetDef cell_getsetlist[] = {\r | |
110 | {"cell_contents", (getter)cell_get_contents, NULL},\r | |
111 | {NULL} /* sentinel */\r | |
112 | };\r | |
113 | \r | |
114 | PyTypeObject PyCell_Type = {\r | |
115 | PyVarObject_HEAD_INIT(&PyType_Type, 0)\r | |
116 | "cell",\r | |
117 | sizeof(PyCellObject),\r | |
118 | 0,\r | |
119 | (destructor)cell_dealloc, /* tp_dealloc */\r | |
120 | 0, /* tp_print */\r | |
121 | 0, /* tp_getattr */\r | |
122 | 0, /* tp_setattr */\r | |
123 | (cmpfunc)cell_compare, /* tp_compare */\r | |
124 | (reprfunc)cell_repr, /* tp_repr */\r | |
125 | 0, /* tp_as_number */\r | |
126 | 0, /* tp_as_sequence */\r | |
127 | 0, /* tp_as_mapping */\r | |
128 | 0, /* tp_hash */\r | |
129 | 0, /* tp_call */\r | |
130 | 0, /* tp_str */\r | |
131 | PyObject_GenericGetAttr, /* tp_getattro */\r | |
132 | 0, /* tp_setattro */\r | |
133 | 0, /* tp_as_buffer */\r | |
134 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */\r | |
135 | 0, /* tp_doc */\r | |
136 | (traverseproc)cell_traverse, /* tp_traverse */\r | |
137 | (inquiry)cell_clear, /* tp_clear */\r | |
138 | 0, /* tp_richcompare */\r | |
139 | 0, /* tp_weaklistoffset */\r | |
140 | 0, /* tp_iter */\r | |
141 | 0, /* tp_iternext */\r | |
142 | 0, /* tp_methods */\r | |
143 | 0, /* tp_members */\r | |
144 | cell_getsetlist, /* tp_getset */\r | |
145 | };\r |