Debian Codegraph @

src/c/minibuffer.h

raw
1
2 /* Implementation of a C object with the 'buffer' or 'memoryview'
3 * interface at C-level (as appropriate for the version of Python we're
4 * compiling for), but only a minimal but *consistent* part of the
5 * 'buffer' interface at application level.
6 */
7
8 typedef struct {
9 PyObject_HEAD
10 char *mb_data;
11 Py_ssize_t mb_size;
12 PyObject *mb_keepalive;
13 PyObject *mb_weakreflist; /* weakref support */
14 } MiniBufferObj;
15
16 static Py_ssize_t mb_length(MiniBufferObj *self)
17 {
18 return self->mb_size;
19 }
20
21 static PyObject *mb_item(MiniBufferObj *self, Py_ssize_t idx)
22 {
23 if (idx < 0 || idx >= self->mb_size ) {
24 PyErr_SetString(PyExc_IndexError, "buffer index out of range");
25 return NULL;
26 }
27 return PyBytes_FromStringAndSize(self->mb_data + idx, 1);
28 }
29
30 static PyObject *mb_slice(MiniBufferObj *self,
31 Py_ssize_t left, Py_ssize_t right)
32 {
33 Py_ssize_t size = self->mb_size;
34 if (left < 0) left = 0;
35 if (right > size) right = size;
36 if (left > right) left = right;
37 return PyBytes_FromStringAndSize(self->mb_data + left, right - left);
38 }
39
40 static int mb_ass_item(MiniBufferObj *self, Py_ssize_t idx, PyObject *other)
41 {
42 if (idx < 0 || idx >= self->mb_size) {
43 PyErr_SetString(PyExc_IndexError,
44 "buffer assignment index out of range");
45 return -1;
46 }
47 if (PyBytes_Check(other) && PyBytes_GET_SIZE(other) == 1) {
48 self->mb_data[idx] = PyBytes_AS_STRING(other)[0];
49 return 0;
50 }
51 else {
52 PyErr_Format(PyExc_TypeError,
53 "must assign a bytes"
54 " of length 1, not %.200s", Py_TYPE(other)->tp_name);
55 return -1;
56 }
57 }
58
59 /* forward: from _cffi_backend.c */
60 static int _fetch_as_buffer(PyObject *x, Py_buffer *view, int writable_only);
61
62 static int mb_ass_slice(MiniBufferObj *self,
63 Py_ssize_t left, Py_ssize_t right, PyObject *other)
64 {
65 Py_ssize_t count;
66 Py_ssize_t size = self->mb_size;
67 Py_buffer src_view;
68
69 if (_fetch_as_buffer(other, &src_view, 0) < 0)
70 return -1;
71
72 if (left < 0) left = 0;
73 if (right > size) right = size;
74 if (left > right) left = right;
75
76 count = right - left;
77 if (count != src_view.len) {
78 PyBuffer_Release(&src_view);
79 PyErr_SetString(PyExc_ValueError,
80 "right operand length must match slice length");
81 return -1;
82 }
83 memcpy(self->mb_data + left, src_view.buf, count);
84 PyBuffer_Release(&src_view);
85 return 0;
86 }
87
88 static int mb_getbuf(MiniBufferObj *self, Py_buffer *view, int flags)
89 {
90 return PyBuffer_FillInfo(view, (PyObject *)self,
91 self->mb_data, self->mb_size,
92 /*readonly=*/0, flags);
93 }
94
95 static PySequenceMethods mb_as_sequence = {
96 (lenfunc)mb_length, /*sq_length*/
97 (binaryfunc)0, /*sq_concat*/
98 (ssizeargfunc)0, /*sq_repeat*/
99 (ssizeargfunc)mb_item, /*sq_item*/
100 (ssizessizeargfunc)mb_slice, /*sq_slice*/
101 (ssizeobjargproc)mb_ass_item, /*sq_ass_item*/
102 (ssizessizeobjargproc)mb_ass_slice, /*sq_ass_slice*/
103 };
104
105 static PyBufferProcs mb_as_buffer = {
106 (getbufferproc)mb_getbuf,
107 (releasebufferproc)0,
108 };
109
110 static void
111 mb_dealloc(MiniBufferObj *ob)
112 {
113 PyObject_GC_UnTrack(ob);
114 PyObject_ClearWeakRefs((PyObject *)ob);
115 Py_XDECREF(ob->mb_keepalive);
116 Py_TYPE(ob)->tp_free((PyObject *)ob);
117 }
118
119 static int
120 mb_traverse(MiniBufferObj *ob, visitproc visit, void *arg)
121 {
122 Py_VISIT(ob->mb_keepalive);
123 return 0;
124 }
125
126 static int
127 mb_clear(MiniBufferObj *ob)
128 {
129 Py_CLEAR(ob->mb_keepalive);
130 return 0;
131 }
132
133 static PyObject *
134 mb_richcompare(PyObject *self, PyObject *other, int op)
135 {
136 Py_ssize_t self_size, other_size;
137 Py_buffer self_bytes, other_bytes;
138 PyObject *res;
139 Py_ssize_t minsize;
140 int cmp, rc;
141
142 /* Bytes can be compared to anything that supports the (binary)
143 buffer API. Except that a comparison with Unicode is always an
144 error, even if the comparison is for equality. */
145 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
146 if (!rc)
147 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
148 if (rc < 0)
149 return NULL;
150 if (rc) {
151 Py_INCREF(Py_NotImplemented);
152 return Py_NotImplemented;
153 }
154
155 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
156 PyErr_Clear();
157 Py_INCREF(Py_NotImplemented);
158 return Py_NotImplemented;
159
160 }
161 self_size = self_bytes.len;
162
163 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
164 PyErr_Clear();
165 PyBuffer_Release(&self_bytes);
166 Py_INCREF(Py_NotImplemented);
167 return Py_NotImplemented;
168
169 }
170 other_size = other_bytes.len;
171
172 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
173 /* Shortcut: if the lengths differ, the objects differ */
174 cmp = (op == Py_NE);
175 }
176 else {
177 minsize = self_size;
178 if (other_size < minsize)
179 minsize = other_size;
180
181 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
182 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
183
184 if (cmp == 0) {
185 if (self_size < other_size)
186 cmp = -1;
187 else if (self_size > other_size)
188 cmp = 1;
189 }
190
191 switch (op) {
192 case Py_LT: cmp = cmp < 0; break;
193 case Py_LE: cmp = cmp <= 0; break;
194 case Py_EQ: cmp = cmp == 0; break;
195 case Py_NE: cmp = cmp != 0; break;
196 case Py_GT: cmp = cmp > 0; break;
197 case Py_GE: cmp = cmp >= 0; break;
198 }
199 }
200
201 res = cmp ? Py_True : Py_False;
202 PyBuffer_Release(&self_bytes);
203 PyBuffer_Release(&other_bytes);
204 Py_INCREF(res);
205 return res;
206 }
207
208 /* pfffffffffffff pages of copy-paste from listobject.c */
209
210 static PyObject *mb_subscript(MiniBufferObj *self, PyObject *item)
211 {
212 if (PyIndex_Check(item)) {
213 Py_ssize_t i;
214 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
215 if (i == -1 && PyErr_Occurred())
216 return NULL;
217 if (i < 0)
218 i += self->mb_size;
219 return mb_item(self, i);
220 }
221 else if (PySlice_Check(item)) {
222 Py_ssize_t start, stop, step, slicelength;
223
224 if (PySlice_GetIndicesEx(item, self->mb_size,
225 &start, &stop, &step, &slicelength) < 0)
226 return NULL;
227
228 if (step == 1)
229 return mb_slice(self, start, stop);
230 else {
231 PyErr_SetString(PyExc_TypeError,
232 "buffer doesn't support slicing with step != 1");
233 return NULL;
234 }
235 }
236 else {
237 PyErr_Format(PyExc_TypeError,
238 "buffer indices must be integers, not %.200s",
239 item->ob_type->tp_name);
240 return NULL;
241 }
242 }
243 static int
244 mb_ass_subscript(MiniBufferObj* self, PyObject* item, PyObject* value)
245 {
246 if (value == NULL) {
247 PyErr_SetString(PyExc_TypeError,
248 "'del x[n]' not supported for buffer objects");
249 return -1;
250 }
251 if (PyIndex_Check(item)) {
252 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
253 if (i == -1 && PyErr_Occurred())
254 return -1;
255 if (i < 0)
256 i += self->mb_size;
257 return mb_ass_item(self, i, value);
258 }
259 else if (PySlice_Check(item)) {
260 Py_ssize_t start, stop, step, slicelength;
261
262 if (PySlice_GetIndicesEx(item, self->mb_size,
263 &start, &stop, &step, &slicelength) < 0) {
264 return -1;
265 }
266
267 if (step == 1)
268 return mb_ass_slice(self, start, stop, value);
269 else {
270 PyErr_SetString(PyExc_TypeError,
271 "buffer doesn't support slicing with step != 1");
272 return -1;
273 }
274 }
275 else {
276 PyErr_Format(PyExc_TypeError,
277 "buffer indices must be integers, not %.200s",
278 item->ob_type->tp_name);
279 return -1;
280 }
281 }
282
283 static PyMappingMethods mb_as_mapping = {
284 (lenfunc)mb_length, /*mp_length*/
285 (binaryfunc)mb_subscript, /*mp_subscript*/
286 (objobjargproc)mb_ass_subscript, /*mp_ass_subscript*/
287 };
288
289 PyDoc_STRVAR(ffi_buffer_doc,
290 "ffi.buffer(cdata[, byte_size]):\n"
291 "Return a read-write buffer object that references the raw C data\n"
292 "pointed to by the given 'cdata'. The 'cdata' must be a pointer or an\n"
293 "array. Can be passed to functions expecting a buffer, or directly\n"
294 "manipulated with:\n"
295 "\n"
296 " buf[:] get a copy of it in a regular string, or\n"
297 " buf[idx] as a single character\n"
298 " buf[:] = ...\n"
299 " buf[idx] = ... change the content");
300
301 static PyObject * /* forward, implemented in _cffi_backend.c */
302 b_buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
303
304
305 static PyTypeObject MiniBuffer_Type = {
306 PyVarObject_HEAD_INIT(NULL, 0)
307 "_cffi_backend.buffer",
308 sizeof(MiniBufferObj),
309 0,
310 (destructor)mb_dealloc, /* tp_dealloc */
311 0, /* tp_print */
312 0, /* tp_getattr */
313 0, /* tp_setattr */
314 0, /* tp_compare */
315 0, /* tp_repr */
316 0, /* tp_as_number */
317 &mb_as_sequence, /* tp_as_sequence */
318 &mb_as_mapping, /* tp_as_mapping */
319 0, /* tp_hash */
320 0, /* tp_call */
321 0, /* tp_str */
322 PyObject_GenericGetAttr, /* tp_getattro */
323 0, /* tp_setattro */
324 &mb_as_buffer, /* tp_as_buffer */
325 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
326 ffi_buffer_doc, /* tp_doc */
327 (traverseproc)mb_traverse, /* tp_traverse */
328 (inquiry)mb_clear, /* tp_clear */
329 (richcmpfunc)mb_richcompare, /* tp_richcompare */
330 offsetof(MiniBufferObj, mb_weakreflist), /* tp_weaklistoffset */
331 0, /* tp_iter */
332 0, /* tp_iternext */
333 0, /* tp_methods */
334 0, /* tp_members */
335 0, /* tp_getset */
336 0, /* tp_base */
337 0, /* tp_dict */
338 0, /* tp_descr_get */
339 0, /* tp_descr_set */
340 0, /* tp_dictoffset */
341 0, /* tp_init */
342 0, /* tp_alloc */
343 b_buffer_new, /* tp_new */
344 0, /* tp_free */
345 };
346
347 static PyObject *minibuffer_new(char *data, Py_ssize_t size,
348 PyObject *keepalive)
349 {
350 MiniBufferObj *ob = PyObject_GC_New(MiniBufferObj, &MiniBuffer_Type);
351 if (ob != NULL) {
352 ob->mb_data = data;
353 ob->mb_size = size;
354 ob->mb_keepalive = keepalive; Py_INCREF(keepalive);
355 ob->mb_weakreflist = NULL;
356 PyObject_GC_Track(ob);
357 }
358 return (PyObject *)ob;
359 }