| 1 | n/a | |
|---|
| 2 | n/a | /* Buffer object implementation */ |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | #include "Python.h" |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | typedef struct { |
|---|
| 8 | n/a | PyObject_HEAD |
|---|
| 9 | n/a | PyObject *b_base; |
|---|
| 10 | n/a | void *b_ptr; |
|---|
| 11 | n/a | Py_ssize_t b_size; |
|---|
| 12 | n/a | Py_ssize_t b_offset; |
|---|
| 13 | n/a | int b_readonly; |
|---|
| 14 | n/a | long b_hash; |
|---|
| 15 | n/a | } PyBufferObject; |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | enum buffer_t { |
|---|
| 19 | n/a | READ_BUFFER, |
|---|
| 20 | n/a | WRITE_BUFFER, |
|---|
| 21 | n/a | CHAR_BUFFER, |
|---|
| 22 | n/a | ANY_BUFFER |
|---|
| 23 | n/a | }; |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | static int |
|---|
| 26 | n/a | get_buf(PyBufferObject *self, void **ptr, Py_ssize_t *size, |
|---|
| 27 | n/a | enum buffer_t buffer_type) |
|---|
| 28 | 20332 | { |
|---|
| 29 | 20332 | if (self->b_base == NULL) { |
|---|
| 30 | 17251 | assert (ptr != NULL); |
|---|
| 31 | 17251 | *ptr = self->b_ptr; |
|---|
| 32 | 17251 | *size = self->b_size; |
|---|
| 33 | n/a | } |
|---|
| 34 | n/a | else { |
|---|
| 35 | n/a | Py_ssize_t count, offset; |
|---|
| 36 | 3081 | readbufferproc proc = 0; |
|---|
| 37 | 3081 | PyBufferProcs *bp = self->b_base->ob_type->tp_as_buffer; |
|---|
| 38 | 3081 | if ((*bp->bf_getsegcount)(self->b_base, NULL) != 1) { |
|---|
| 39 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 40 | n/a | "single-segment buffer object expected"); |
|---|
| 41 | 0 | return 0; |
|---|
| 42 | n/a | } |
|---|
| 43 | 6117 | if ((buffer_type == READ_BUFFER) || |
|---|
| 44 | n/a | ((buffer_type == ANY_BUFFER) && self->b_readonly)) |
|---|
| 45 | 3036 | proc = bp->bf_getreadbuffer; |
|---|
| 46 | 45 | else if ((buffer_type == WRITE_BUFFER) || |
|---|
| 47 | n/a | (buffer_type == ANY_BUFFER)) |
|---|
| 48 | 0 | proc = (readbufferproc)bp->bf_getwritebuffer; |
|---|
| 49 | 45 | else if (buffer_type == CHAR_BUFFER) { |
|---|
| 50 | 45 | if (!PyType_HasFeature(self->ob_type, |
|---|
| 51 | n/a | Py_TPFLAGS_HAVE_GETCHARBUFFER)) { |
|---|
| 52 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 53 | n/a | "Py_TPFLAGS_HAVE_GETCHARBUFFER needed"); |
|---|
| 54 | 0 | return 0; |
|---|
| 55 | n/a | } |
|---|
| 56 | 45 | proc = (readbufferproc)bp->bf_getcharbuffer; |
|---|
| 57 | n/a | } |
|---|
| 58 | 3081 | if (!proc) { |
|---|
| 59 | n/a | char *buffer_type_name; |
|---|
| 60 | 1 | switch (buffer_type) { |
|---|
| 61 | n/a | case READ_BUFFER: |
|---|
| 62 | 0 | buffer_type_name = "read"; |
|---|
| 63 | 0 | break; |
|---|
| 64 | n/a | case WRITE_BUFFER: |
|---|
| 65 | 0 | buffer_type_name = "write"; |
|---|
| 66 | 0 | break; |
|---|
| 67 | n/a | case CHAR_BUFFER: |
|---|
| 68 | 1 | buffer_type_name = "char"; |
|---|
| 69 | 1 | break; |
|---|
| 70 | n/a | default: |
|---|
| 71 | 0 | buffer_type_name = "no"; |
|---|
| 72 | n/a | break; |
|---|
| 73 | n/a | } |
|---|
| 74 | 1 | PyErr_Format(PyExc_TypeError, |
|---|
| 75 | n/a | "%s buffer type not available", |
|---|
| 76 | n/a | buffer_type_name); |
|---|
| 77 | 1 | return 0; |
|---|
| 78 | n/a | } |
|---|
| 79 | 3080 | if ((count = (*proc)(self->b_base, 0, ptr)) < 0) |
|---|
| 80 | 0 | return 0; |
|---|
| 81 | n/a | /* apply constraints to the start/end */ |
|---|
| 82 | 3080 | if (self->b_offset > count) |
|---|
| 83 | 0 | offset = count; |
|---|
| 84 | n/a | else |
|---|
| 85 | 3080 | offset = self->b_offset; |
|---|
| 86 | 3080 | *(char **)ptr = *(char **)ptr + offset; |
|---|
| 87 | 3080 | if (self->b_size == Py_END_OF_BUFFER) |
|---|
| 88 | 1199 | *size = count; |
|---|
| 89 | n/a | else |
|---|
| 90 | 1881 | *size = self->b_size; |
|---|
| 91 | 3080 | if (offset + *size > count) |
|---|
| 92 | 1810 | *size = count - offset; |
|---|
| 93 | n/a | } |
|---|
| 94 | 20331 | return 1; |
|---|
| 95 | n/a | } |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | static PyObject * |
|---|
| 99 | n/a | buffer_from_memory(PyObject *base, Py_ssize_t size, Py_ssize_t offset, void *ptr, |
|---|
| 100 | n/a | int readonly) |
|---|
| 101 | 9373 | { |
|---|
| 102 | n/a | PyBufferObject * b; |
|---|
| 103 | n/a | |
|---|
| 104 | 9373 | if (size < 0 && size != Py_END_OF_BUFFER) { |
|---|
| 105 | 0 | PyErr_SetString(PyExc_ValueError, |
|---|
| 106 | n/a | "size must be zero or positive"); |
|---|
| 107 | 0 | return NULL; |
|---|
| 108 | n/a | } |
|---|
| 109 | 9373 | if (offset < 0) { |
|---|
| 110 | 0 | PyErr_SetString(PyExc_ValueError, |
|---|
| 111 | n/a | "offset must be zero or positive"); |
|---|
| 112 | 0 | return NULL; |
|---|
| 113 | n/a | } |
|---|
| 114 | n/a | |
|---|
| 115 | 9373 | b = PyObject_NEW(PyBufferObject, &PyBuffer_Type); |
|---|
| 116 | 9373 | if ( b == NULL ) |
|---|
| 117 | 0 | return NULL; |
|---|
| 118 | n/a | |
|---|
| 119 | 9373 | Py_XINCREF(base); |
|---|
| 120 | 9373 | b->b_base = base; |
|---|
| 121 | 9373 | b->b_ptr = ptr; |
|---|
| 122 | 9373 | b->b_size = size; |
|---|
| 123 | 9373 | b->b_offset = offset; |
|---|
| 124 | 9373 | b->b_readonly = readonly; |
|---|
| 125 | 9373 | b->b_hash = -1; |
|---|
| 126 | n/a | |
|---|
| 127 | 9373 | return (PyObject *) b; |
|---|
| 128 | n/a | } |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | static PyObject * |
|---|
| 131 | n/a | buffer_from_object(PyObject *base, Py_ssize_t size, Py_ssize_t offset, int readonly) |
|---|
| 132 | 748 | { |
|---|
| 133 | 748 | if (offset < 0) { |
|---|
| 134 | 2 | PyErr_SetString(PyExc_ValueError, |
|---|
| 135 | n/a | "offset must be zero or positive"); |
|---|
| 136 | 2 | return NULL; |
|---|
| 137 | n/a | } |
|---|
| 138 | 746 | if ( PyBuffer_Check(base) && (((PyBufferObject *)base)->b_base) ) { |
|---|
| 139 | n/a | /* another buffer, refer to the base object */ |
|---|
| 140 | 8 | PyBufferObject *b = (PyBufferObject *)base; |
|---|
| 141 | 8 | if (b->b_size != Py_END_OF_BUFFER) { |
|---|
| 142 | 4 | Py_ssize_t base_size = b->b_size - offset; |
|---|
| 143 | 4 | if (base_size < 0) |
|---|
| 144 | 0 | base_size = 0; |
|---|
| 145 | 4 | if (size == Py_END_OF_BUFFER || size > base_size) |
|---|
| 146 | 4 | size = base_size; |
|---|
| 147 | n/a | } |
|---|
| 148 | 8 | offset += b->b_offset; |
|---|
| 149 | 8 | base = b->b_base; |
|---|
| 150 | n/a | } |
|---|
| 151 | 746 | return buffer_from_memory(base, size, offset, NULL, readonly); |
|---|
| 152 | n/a | } |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | PyObject * |
|---|
| 156 | n/a | PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size) |
|---|
| 157 | 761 | { |
|---|
| 158 | 761 | PyBufferProcs *pb = base->ob_type->tp_as_buffer; |
|---|
| 159 | n/a | |
|---|
| 160 | 761 | if ( pb == NULL || |
|---|
| 161 | n/a | pb->bf_getreadbuffer == NULL || |
|---|
| 162 | n/a | pb->bf_getsegcount == NULL ) |
|---|
| 163 | n/a | { |
|---|
| 164 | 13 | PyErr_SetString(PyExc_TypeError, "buffer object expected"); |
|---|
| 165 | 13 | return NULL; |
|---|
| 166 | n/a | } |
|---|
| 167 | n/a | |
|---|
| 168 | 748 | return buffer_from_object(base, size, offset, 1); |
|---|
| 169 | n/a | } |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | PyObject * |
|---|
| 172 | n/a | PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size) |
|---|
| 173 | 0 | { |
|---|
| 174 | 0 | PyBufferProcs *pb = base->ob_type->tp_as_buffer; |
|---|
| 175 | n/a | |
|---|
| 176 | 0 | if ( pb == NULL || |
|---|
| 177 | n/a | pb->bf_getwritebuffer == NULL || |
|---|
| 178 | n/a | pb->bf_getsegcount == NULL ) |
|---|
| 179 | n/a | { |
|---|
| 180 | 0 | PyErr_SetString(PyExc_TypeError, "buffer object expected"); |
|---|
| 181 | 0 | return NULL; |
|---|
| 182 | n/a | } |
|---|
| 183 | n/a | |
|---|
| 184 | 0 | return buffer_from_object(base, size, offset, 0); |
|---|
| 185 | n/a | } |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | PyObject * |
|---|
| 188 | n/a | PyBuffer_FromMemory(void *ptr, Py_ssize_t size) |
|---|
| 189 | 8627 | { |
|---|
| 190 | 8627 | return buffer_from_memory(NULL, size, 0, ptr, 1); |
|---|
| 191 | n/a | } |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | PyObject * |
|---|
| 194 | n/a | PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size) |
|---|
| 195 | 0 | { |
|---|
| 196 | 0 | return buffer_from_memory(NULL, size, 0, ptr, 0); |
|---|
| 197 | n/a | } |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | PyObject * |
|---|
| 200 | n/a | PyBuffer_New(Py_ssize_t size) |
|---|
| 201 | 5 | { |
|---|
| 202 | n/a | PyObject *o; |
|---|
| 203 | n/a | PyBufferObject * b; |
|---|
| 204 | n/a | |
|---|
| 205 | 5 | if (size < 0) { |
|---|
| 206 | 0 | PyErr_SetString(PyExc_ValueError, |
|---|
| 207 | n/a | "size must be zero or positive"); |
|---|
| 208 | 0 | return NULL; |
|---|
| 209 | n/a | } |
|---|
| 210 | 5 | if (sizeof(*b) > PY_SSIZE_T_MAX - size) { |
|---|
| 211 | n/a | /* unlikely */ |
|---|
| 212 | 0 | return PyErr_NoMemory(); |
|---|
| 213 | n/a | } |
|---|
| 214 | n/a | /* Inline PyObject_New */ |
|---|
| 215 | 5 | o = (PyObject *)PyObject_MALLOC(sizeof(*b) + size); |
|---|
| 216 | 5 | if ( o == NULL ) |
|---|
| 217 | 0 | return PyErr_NoMemory(); |
|---|
| 218 | 5 | b = (PyBufferObject *) PyObject_INIT(o, &PyBuffer_Type); |
|---|
| 219 | n/a | |
|---|
| 220 | 5 | b->b_base = NULL; |
|---|
| 221 | 5 | b->b_ptr = (void *)(b + 1); |
|---|
| 222 | 5 | b->b_size = size; |
|---|
| 223 | 5 | b->b_offset = 0; |
|---|
| 224 | 5 | b->b_readonly = 0; |
|---|
| 225 | 5 | b->b_hash = -1; |
|---|
| 226 | n/a | |
|---|
| 227 | 5 | return o; |
|---|
| 228 | n/a | } |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | /* Methods */ |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | static PyObject * |
|---|
| 233 | n/a | buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|---|
| 234 | 761 | { |
|---|
| 235 | n/a | PyObject *ob; |
|---|
| 236 | 761 | Py_ssize_t offset = 0; |
|---|
| 237 | 761 | Py_ssize_t size = Py_END_OF_BUFFER; |
|---|
| 238 | n/a | |
|---|
| 239 | 761 | if (PyErr_WarnPy3k("buffer() not supported in 3.x", 1) < 0) |
|---|
| 240 | 0 | return NULL; |
|---|
| 241 | n/a | |
|---|
| 242 | 761 | if (!_PyArg_NoKeywords("buffer()", kw)) |
|---|
| 243 | 0 | return NULL; |
|---|
| 244 | n/a | |
|---|
| 245 | 761 | if (!PyArg_ParseTuple(args, "O|nn:buffer", &ob, &offset, &size)) |
|---|
| 246 | 0 | return NULL; |
|---|
| 247 | 761 | return PyBuffer_FromObject(ob, offset, size); |
|---|
| 248 | n/a | } |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | PyDoc_STRVAR(buffer_doc, |
|---|
| 251 | n/a | "buffer(object [, offset[, size]])\n\ |
|---|
| 252 | n/a | \n\ |
|---|
| 253 | n/a | Create a new buffer object which references the given object.\n\ |
|---|
| 254 | n/a | The buffer will reference a slice of the target object from the\n\ |
|---|
| 255 | n/a | start of the object (or at the specified offset). The slice will\n\ |
|---|
| 256 | n/a | extend to the end of the target object (or with the specified size)."); |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | static void |
|---|
| 260 | n/a | buffer_dealloc(PyBufferObject *self) |
|---|
| 261 | 9378 | { |
|---|
| 262 | 9378 | Py_XDECREF(self->b_base); |
|---|
| 263 | 9378 | PyObject_DEL(self); |
|---|
| 264 | 9378 | } |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | static int |
|---|
| 267 | n/a | buffer_compare(PyBufferObject *self, PyBufferObject *other) |
|---|
| 268 | 4 | { |
|---|
| 269 | n/a | void *p1, *p2; |
|---|
| 270 | n/a | Py_ssize_t len_self, len_other, min_len; |
|---|
| 271 | n/a | int cmp; |
|---|
| 272 | n/a | |
|---|
| 273 | 4 | if (!get_buf(self, &p1, &len_self, ANY_BUFFER)) |
|---|
| 274 | 0 | return -1; |
|---|
| 275 | 4 | if (!get_buf(other, &p2, &len_other, ANY_BUFFER)) |
|---|
| 276 | 0 | return -1; |
|---|
| 277 | 4 | min_len = (len_self < len_other) ? len_self : len_other; |
|---|
| 278 | 4 | if (min_len > 0) { |
|---|
| 279 | 4 | cmp = memcmp(p1, p2, min_len); |
|---|
| 280 | 4 | if (cmp != 0) |
|---|
| 281 | 1 | return cmp < 0 ? -1 : 1; |
|---|
| 282 | n/a | } |
|---|
| 283 | 3 | return (len_self < len_other) ? -1 : (len_self > len_other) ? 1 : 0; |
|---|
| 284 | n/a | } |
|---|
| 285 | n/a | |
|---|
| 286 | n/a | static PyObject * |
|---|
| 287 | n/a | buffer_repr(PyBufferObject *self) |
|---|
| 288 | 1 | { |
|---|
| 289 | 1 | const char *status = self->b_readonly ? "read-only" : "read-write"; |
|---|
| 290 | n/a | |
|---|
| 291 | 1 | if ( self->b_base == NULL ) |
|---|
| 292 | 0 | return PyString_FromFormat("<%s buffer ptr %p, size %zd at %p>", |
|---|
| 293 | n/a | status, |
|---|
| 294 | n/a | self->b_ptr, |
|---|
| 295 | n/a | self->b_size, |
|---|
| 296 | n/a | self); |
|---|
| 297 | n/a | else |
|---|
| 298 | 1 | return PyString_FromFormat( |
|---|
| 299 | n/a | "<%s buffer for %p, size %zd, offset %zd at %p>", |
|---|
| 300 | n/a | status, |
|---|
| 301 | n/a | self->b_base, |
|---|
| 302 | n/a | self->b_size, |
|---|
| 303 | n/a | self->b_offset, |
|---|
| 304 | n/a | self); |
|---|
| 305 | n/a | } |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | static long |
|---|
| 308 | n/a | buffer_hash(PyBufferObject *self) |
|---|
| 309 | 1 | { |
|---|
| 310 | n/a | void *ptr; |
|---|
| 311 | n/a | Py_ssize_t size; |
|---|
| 312 | n/a | register Py_ssize_t len; |
|---|
| 313 | n/a | register unsigned char *p; |
|---|
| 314 | n/a | register long x; |
|---|
| 315 | n/a | |
|---|
| 316 | 1 | if ( self->b_hash != -1 ) |
|---|
| 317 | 0 | return self->b_hash; |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | /* XXX potential bugs here, a readonly buffer does not imply that the |
|---|
| 320 | n/a | * underlying memory is immutable. b_readonly is a necessary but not |
|---|
| 321 | n/a | * sufficient condition for a buffer to be hashable. Perhaps it would |
|---|
| 322 | n/a | * be better to only allow hashing if the underlying object is known to |
|---|
| 323 | n/a | * be immutable (e.g. PyString_Check() is true). Another idea would |
|---|
| 324 | n/a | * be to call tp_hash on the underlying object and see if it raises |
|---|
| 325 | n/a | * an error. */ |
|---|
| 326 | 1 | if ( !self->b_readonly ) |
|---|
| 327 | n/a | { |
|---|
| 328 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 329 | n/a | "writable buffers are not hashable"); |
|---|
| 330 | 0 | return -1; |
|---|
| 331 | n/a | } |
|---|
| 332 | n/a | |
|---|
| 333 | 1 | if (!get_buf(self, &ptr, &size, ANY_BUFFER)) |
|---|
| 334 | 0 | return -1; |
|---|
| 335 | 1 | p = (unsigned char *) ptr; |
|---|
| 336 | 1 | len = size; |
|---|
| 337 | 1 | x = *p << 7; |
|---|
| 338 | 6 | while (--len >= 0) |
|---|
| 339 | 4 | x = (1000003*x) ^ *p++; |
|---|
| 340 | 1 | x ^= size; |
|---|
| 341 | 1 | if (x == -1) |
|---|
| 342 | 0 | x = -2; |
|---|
| 343 | 1 | self->b_hash = x; |
|---|
| 344 | 1 | return x; |
|---|
| 345 | n/a | } |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | static PyObject * |
|---|
| 348 | n/a | buffer_str(PyBufferObject *self) |
|---|
| 349 | 22 | { |
|---|
| 350 | n/a | void *ptr; |
|---|
| 351 | n/a | Py_ssize_t size; |
|---|
| 352 | 22 | if (!get_buf(self, &ptr, &size, ANY_BUFFER)) |
|---|
| 353 | 0 | return NULL; |
|---|
| 354 | 22 | return PyString_FromStringAndSize((const char *)ptr, size); |
|---|
| 355 | n/a | } |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | /* Sequence methods */ |
|---|
| 358 | n/a | |
|---|
| 359 | n/a | static Py_ssize_t |
|---|
| 360 | n/a | buffer_length(PyBufferObject *self) |
|---|
| 361 | 625 | { |
|---|
| 362 | n/a | void *ptr; |
|---|
| 363 | n/a | Py_ssize_t size; |
|---|
| 364 | 625 | if (!get_buf(self, &ptr, &size, ANY_BUFFER)) |
|---|
| 365 | 0 | return -1; |
|---|
| 366 | 625 | return size; |
|---|
| 367 | n/a | } |
|---|
| 368 | n/a | |
|---|
| 369 | n/a | static PyObject * |
|---|
| 370 | n/a | buffer_concat(PyBufferObject *self, PyObject *other) |
|---|
| 371 | 30 | { |
|---|
| 372 | 30 | PyBufferProcs *pb = other->ob_type->tp_as_buffer; |
|---|
| 373 | n/a | void *ptr1, *ptr2; |
|---|
| 374 | n/a | char *p; |
|---|
| 375 | n/a | PyObject *ob; |
|---|
| 376 | n/a | Py_ssize_t size, count; |
|---|
| 377 | n/a | |
|---|
| 378 | 30 | if ( pb == NULL || |
|---|
| 379 | n/a | pb->bf_getreadbuffer == NULL || |
|---|
| 380 | n/a | pb->bf_getsegcount == NULL ) |
|---|
| 381 | n/a | { |
|---|
| 382 | 0 | PyErr_BadArgument(); |
|---|
| 383 | 0 | return NULL; |
|---|
| 384 | n/a | } |
|---|
| 385 | 30 | if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) |
|---|
| 386 | n/a | { |
|---|
| 387 | n/a | /* ### use a different exception type/message? */ |
|---|
| 388 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 389 | n/a | "single-segment buffer object expected"); |
|---|
| 390 | 0 | return NULL; |
|---|
| 391 | n/a | } |
|---|
| 392 | n/a | |
|---|
| 393 | 30 | if (!get_buf(self, &ptr1, &size, ANY_BUFFER)) |
|---|
| 394 | 0 | return NULL; |
|---|
| 395 | n/a | |
|---|
| 396 | n/a | /* optimize special case */ |
|---|
| 397 | 30 | if ( size == 0 ) |
|---|
| 398 | n/a | { |
|---|
| 399 | 0 | Py_INCREF(other); |
|---|
| 400 | 0 | return other; |
|---|
| 401 | n/a | } |
|---|
| 402 | n/a | |
|---|
| 403 | 30 | if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 ) |
|---|
| 404 | 0 | return NULL; |
|---|
| 405 | n/a | |
|---|
| 406 | 30 | assert(count <= PY_SIZE_MAX - size); |
|---|
| 407 | n/a | |
|---|
| 408 | 30 | ob = PyString_FromStringAndSize(NULL, size + count); |
|---|
| 409 | 30 | if ( ob == NULL ) |
|---|
| 410 | 0 | return NULL; |
|---|
| 411 | 30 | p = PyString_AS_STRING(ob); |
|---|
| 412 | 30 | memcpy(p, ptr1, size); |
|---|
| 413 | 30 | memcpy(p + size, ptr2, count); |
|---|
| 414 | n/a | |
|---|
| 415 | n/a | /* there is an extra byte in the string object, so this is safe */ |
|---|
| 416 | 30 | p[size + count] = '\0'; |
|---|
| 417 | n/a | |
|---|
| 418 | 30 | return ob; |
|---|
| 419 | n/a | } |
|---|
| 420 | n/a | |
|---|
| 421 | n/a | static PyObject * |
|---|
| 422 | n/a | buffer_repeat(PyBufferObject *self, Py_ssize_t count) |
|---|
| 423 | 3 | { |
|---|
| 424 | n/a | PyObject *ob; |
|---|
| 425 | n/a | register char *p; |
|---|
| 426 | n/a | void *ptr; |
|---|
| 427 | n/a | Py_ssize_t size; |
|---|
| 428 | n/a | |
|---|
| 429 | 3 | if ( count < 0 ) |
|---|
| 430 | 0 | count = 0; |
|---|
| 431 | 3 | if (!get_buf(self, &ptr, &size, ANY_BUFFER)) |
|---|
| 432 | 0 | return NULL; |
|---|
| 433 | 3 | if (count > PY_SSIZE_T_MAX / size) { |
|---|
| 434 | 0 | PyErr_SetString(PyExc_MemoryError, "result too large"); |
|---|
| 435 | 0 | return NULL; |
|---|
| 436 | n/a | } |
|---|
| 437 | 3 | ob = PyString_FromStringAndSize(NULL, size * count); |
|---|
| 438 | 3 | if ( ob == NULL ) |
|---|
| 439 | 0 | return NULL; |
|---|
| 440 | n/a | |
|---|
| 441 | 3 | p = PyString_AS_STRING(ob); |
|---|
| 442 | 5158 | while ( count-- ) |
|---|
| 443 | n/a | { |
|---|
| 444 | 5152 | memcpy(p, ptr, size); |
|---|
| 445 | 5152 | p += size; |
|---|
| 446 | n/a | } |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | /* there is an extra byte in the string object, so this is safe */ |
|---|
| 449 | 3 | *p = '\0'; |
|---|
| 450 | n/a | |
|---|
| 451 | 3 | return ob; |
|---|
| 452 | n/a | } |
|---|
| 453 | n/a | |
|---|
| 454 | n/a | static PyObject * |
|---|
| 455 | n/a | buffer_item(PyBufferObject *self, Py_ssize_t idx) |
|---|
| 456 | 21 | { |
|---|
| 457 | n/a | void *ptr; |
|---|
| 458 | n/a | Py_ssize_t size; |
|---|
| 459 | 21 | if (!get_buf(self, &ptr, &size, ANY_BUFFER)) |
|---|
| 460 | 0 | return NULL; |
|---|
| 461 | 21 | if ( idx < 0 || idx >= size ) { |
|---|
| 462 | 0 | PyErr_SetString(PyExc_IndexError, "buffer index out of range"); |
|---|
| 463 | 0 | return NULL; |
|---|
| 464 | n/a | } |
|---|
| 465 | 21 | return PyString_FromStringAndSize((char *)ptr + idx, 1); |
|---|
| 466 | n/a | } |
|---|
| 467 | n/a | |
|---|
| 468 | n/a | static PyObject * |
|---|
| 469 | n/a | buffer_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right) |
|---|
| 470 | 24 | { |
|---|
| 471 | n/a | void *ptr; |
|---|
| 472 | n/a | Py_ssize_t size; |
|---|
| 473 | 24 | if (!get_buf(self, &ptr, &size, ANY_BUFFER)) |
|---|
| 474 | 0 | return NULL; |
|---|
| 475 | 24 | if ( left < 0 ) |
|---|
| 476 | 0 | left = 0; |
|---|
| 477 | 24 | if ( right < 0 ) |
|---|
| 478 | 0 | right = 0; |
|---|
| 479 | 24 | if ( right > size ) |
|---|
| 480 | 3 | right = size; |
|---|
| 481 | 24 | if ( right < left ) |
|---|
| 482 | 0 | right = left; |
|---|
| 483 | 24 | return PyString_FromStringAndSize((char *)ptr + left, |
|---|
| 484 | n/a | right - left); |
|---|
| 485 | n/a | } |
|---|
| 486 | n/a | |
|---|
| 487 | n/a | static PyObject * |
|---|
| 488 | n/a | buffer_subscript(PyBufferObject *self, PyObject *item) |
|---|
| 489 | 921 | { |
|---|
| 490 | n/a | void *p; |
|---|
| 491 | n/a | Py_ssize_t size; |
|---|
| 492 | n/a | |
|---|
| 493 | 921 | if (!get_buf(self, &p, &size, ANY_BUFFER)) |
|---|
| 494 | 0 | return NULL; |
|---|
| 495 | 921 | if (PyIndex_Check(item)) { |
|---|
| 496 | 21 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|---|
| 497 | 21 | if (i == -1 && PyErr_Occurred()) |
|---|
| 498 | 0 | return NULL; |
|---|
| 499 | 21 | if (i < 0) |
|---|
| 500 | 0 | i += size; |
|---|
| 501 | 21 | return buffer_item(self, i); |
|---|
| 502 | n/a | } |
|---|
| 503 | 900 | else if (PySlice_Check(item)) { |
|---|
| 504 | n/a | Py_ssize_t start, stop, step, slicelength, cur, i; |
|---|
| 505 | n/a | |
|---|
| 506 | 900 | if (PySlice_GetIndicesEx((PySliceObject*)item, size, |
|---|
| 507 | n/a | &start, &stop, &step, &slicelength) < 0) { |
|---|
| 508 | 0 | return NULL; |
|---|
| 509 | n/a | } |
|---|
| 510 | n/a | |
|---|
| 511 | 900 | if (slicelength <= 0) |
|---|
| 512 | 441 | return PyString_FromStringAndSize("", 0); |
|---|
| 513 | 459 | else if (step == 1) |
|---|
| 514 | 102 | return PyString_FromStringAndSize((char *)p + start, |
|---|
| 515 | n/a | stop - start); |
|---|
| 516 | n/a | else { |
|---|
| 517 | n/a | PyObject *result; |
|---|
| 518 | 357 | char *source_buf = (char *)p; |
|---|
| 519 | 357 | char *result_buf = (char *)PyMem_Malloc(slicelength); |
|---|
| 520 | n/a | |
|---|
| 521 | 357 | if (result_buf == NULL) |
|---|
| 522 | 0 | return PyErr_NoMemory(); |
|---|
| 523 | n/a | |
|---|
| 524 | 15474 | for (cur = start, i = 0; i < slicelength; |
|---|
| 525 | 14760 | cur += step, i++) { |
|---|
| 526 | 14760 | result_buf[i] = source_buf[cur]; |
|---|
| 527 | n/a | } |
|---|
| 528 | n/a | |
|---|
| 529 | 357 | result = PyString_FromStringAndSize(result_buf, |
|---|
| 530 | n/a | slicelength); |
|---|
| 531 | 357 | PyMem_Free(result_buf); |
|---|
| 532 | 357 | return result; |
|---|
| 533 | n/a | } |
|---|
| 534 | n/a | } |
|---|
| 535 | n/a | else { |
|---|
| 536 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 537 | n/a | "sequence index must be integer"); |
|---|
| 538 | 0 | return NULL; |
|---|
| 539 | n/a | } |
|---|
| 540 | n/a | } |
|---|
| 541 | n/a | |
|---|
| 542 | n/a | static int |
|---|
| 543 | n/a | buffer_ass_item(PyBufferObject *self, Py_ssize_t idx, PyObject *other) |
|---|
| 544 | 0 | { |
|---|
| 545 | n/a | PyBufferProcs *pb; |
|---|
| 546 | n/a | void *ptr1, *ptr2; |
|---|
| 547 | n/a | Py_ssize_t size; |
|---|
| 548 | n/a | Py_ssize_t count; |
|---|
| 549 | n/a | |
|---|
| 550 | 0 | if ( self->b_readonly ) { |
|---|
| 551 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 552 | n/a | "buffer is read-only"); |
|---|
| 553 | 0 | return -1; |
|---|
| 554 | n/a | } |
|---|
| 555 | n/a | |
|---|
| 556 | 0 | if (!get_buf(self, &ptr1, &size, ANY_BUFFER)) |
|---|
| 557 | 0 | return -1; |
|---|
| 558 | n/a | |
|---|
| 559 | 0 | if (idx < 0 || idx >= size) { |
|---|
| 560 | 0 | PyErr_SetString(PyExc_IndexError, |
|---|
| 561 | n/a | "buffer assignment index out of range"); |
|---|
| 562 | 0 | return -1; |
|---|
| 563 | n/a | } |
|---|
| 564 | n/a | |
|---|
| 565 | 0 | pb = other ? other->ob_type->tp_as_buffer : NULL; |
|---|
| 566 | 0 | if ( pb == NULL || |
|---|
| 567 | n/a | pb->bf_getreadbuffer == NULL || |
|---|
| 568 | n/a | pb->bf_getsegcount == NULL ) |
|---|
| 569 | n/a | { |
|---|
| 570 | 0 | PyErr_BadArgument(); |
|---|
| 571 | 0 | return -1; |
|---|
| 572 | n/a | } |
|---|
| 573 | 0 | if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) |
|---|
| 574 | n/a | { |
|---|
| 575 | n/a | /* ### use a different exception type/message? */ |
|---|
| 576 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 577 | n/a | "single-segment buffer object expected"); |
|---|
| 578 | 0 | return -1; |
|---|
| 579 | n/a | } |
|---|
| 580 | n/a | |
|---|
| 581 | 0 | if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 ) |
|---|
| 582 | 0 | return -1; |
|---|
| 583 | 0 | if ( count != 1 ) { |
|---|
| 584 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 585 | n/a | "right operand must be a single byte"); |
|---|
| 586 | 0 | return -1; |
|---|
| 587 | n/a | } |
|---|
| 588 | n/a | |
|---|
| 589 | 0 | ((char *)ptr1)[idx] = *(char *)ptr2; |
|---|
| 590 | 0 | return 0; |
|---|
| 591 | n/a | } |
|---|
| 592 | n/a | |
|---|
| 593 | n/a | static int |
|---|
| 594 | n/a | buffer_ass_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right, PyObject *other) |
|---|
| 595 | 1 | { |
|---|
| 596 | n/a | PyBufferProcs *pb; |
|---|
| 597 | n/a | void *ptr1, *ptr2; |
|---|
| 598 | n/a | Py_ssize_t size; |
|---|
| 599 | n/a | Py_ssize_t slice_len; |
|---|
| 600 | n/a | Py_ssize_t count; |
|---|
| 601 | n/a | |
|---|
| 602 | 1 | if ( self->b_readonly ) { |
|---|
| 603 | 1 | PyErr_SetString(PyExc_TypeError, |
|---|
| 604 | n/a | "buffer is read-only"); |
|---|
| 605 | 1 | return -1; |
|---|
| 606 | n/a | } |
|---|
| 607 | n/a | |
|---|
| 608 | 0 | pb = other ? other->ob_type->tp_as_buffer : NULL; |
|---|
| 609 | 0 | if ( pb == NULL || |
|---|
| 610 | n/a | pb->bf_getreadbuffer == NULL || |
|---|
| 611 | n/a | pb->bf_getsegcount == NULL ) |
|---|
| 612 | n/a | { |
|---|
| 613 | 0 | PyErr_BadArgument(); |
|---|
| 614 | 0 | return -1; |
|---|
| 615 | n/a | } |
|---|
| 616 | 0 | if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) |
|---|
| 617 | n/a | { |
|---|
| 618 | n/a | /* ### use a different exception type/message? */ |
|---|
| 619 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 620 | n/a | "single-segment buffer object expected"); |
|---|
| 621 | 0 | return -1; |
|---|
| 622 | n/a | } |
|---|
| 623 | 0 | if (!get_buf(self, &ptr1, &size, ANY_BUFFER)) |
|---|
| 624 | 0 | return -1; |
|---|
| 625 | 0 | if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 ) |
|---|
| 626 | 0 | return -1; |
|---|
| 627 | n/a | |
|---|
| 628 | 0 | if ( left < 0 ) |
|---|
| 629 | 0 | left = 0; |
|---|
| 630 | 0 | else if ( left > size ) |
|---|
| 631 | 0 | left = size; |
|---|
| 632 | 0 | if ( right < left ) |
|---|
| 633 | 0 | right = left; |
|---|
| 634 | 0 | else if ( right > size ) |
|---|
| 635 | 0 | right = size; |
|---|
| 636 | 0 | slice_len = right - left; |
|---|
| 637 | n/a | |
|---|
| 638 | 0 | if ( count != slice_len ) { |
|---|
| 639 | 0 | PyErr_SetString( |
|---|
| 640 | n/a | PyExc_TypeError, |
|---|
| 641 | n/a | "right operand length must match slice length"); |
|---|
| 642 | 0 | return -1; |
|---|
| 643 | n/a | } |
|---|
| 644 | n/a | |
|---|
| 645 | 0 | if ( slice_len ) |
|---|
| 646 | 0 | memcpy((char *)ptr1 + left, ptr2, slice_len); |
|---|
| 647 | n/a | |
|---|
| 648 | 0 | return 0; |
|---|
| 649 | n/a | } |
|---|
| 650 | n/a | |
|---|
| 651 | n/a | static int |
|---|
| 652 | n/a | buffer_ass_subscript(PyBufferObject *self, PyObject *item, PyObject *value) |
|---|
| 653 | 1 | { |
|---|
| 654 | n/a | PyBufferProcs *pb; |
|---|
| 655 | n/a | void *ptr1, *ptr2; |
|---|
| 656 | n/a | Py_ssize_t selfsize; |
|---|
| 657 | n/a | Py_ssize_t othersize; |
|---|
| 658 | n/a | |
|---|
| 659 | 1 | if ( self->b_readonly ) { |
|---|
| 660 | 1 | PyErr_SetString(PyExc_TypeError, |
|---|
| 661 | n/a | "buffer is read-only"); |
|---|
| 662 | 1 | return -1; |
|---|
| 663 | n/a | } |
|---|
| 664 | n/a | |
|---|
| 665 | 0 | pb = value ? value->ob_type->tp_as_buffer : NULL; |
|---|
| 666 | 0 | if ( pb == NULL || |
|---|
| 667 | n/a | pb->bf_getreadbuffer == NULL || |
|---|
| 668 | n/a | pb->bf_getsegcount == NULL ) |
|---|
| 669 | n/a | { |
|---|
| 670 | 0 | PyErr_BadArgument(); |
|---|
| 671 | 0 | return -1; |
|---|
| 672 | n/a | } |
|---|
| 673 | 0 | if ( (*pb->bf_getsegcount)(value, NULL) != 1 ) |
|---|
| 674 | n/a | { |
|---|
| 675 | n/a | /* ### use a different exception type/message? */ |
|---|
| 676 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 677 | n/a | "single-segment buffer object expected"); |
|---|
| 678 | 0 | return -1; |
|---|
| 679 | n/a | } |
|---|
| 680 | 0 | if (!get_buf(self, &ptr1, &selfsize, ANY_BUFFER)) |
|---|
| 681 | 0 | return -1; |
|---|
| 682 | 0 | if (PyIndex_Check(item)) { |
|---|
| 683 | 0 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|---|
| 684 | 0 | if (i == -1 && PyErr_Occurred()) |
|---|
| 685 | 0 | return -1; |
|---|
| 686 | 0 | if (i < 0) |
|---|
| 687 | 0 | i += selfsize; |
|---|
| 688 | 0 | return buffer_ass_item(self, i, value); |
|---|
| 689 | n/a | } |
|---|
| 690 | 0 | else if (PySlice_Check(item)) { |
|---|
| 691 | n/a | Py_ssize_t start, stop, step, slicelength; |
|---|
| 692 | n/a | |
|---|
| 693 | 0 | if (PySlice_GetIndicesEx((PySliceObject *)item, selfsize, |
|---|
| 694 | n/a | &start, &stop, &step, &slicelength) < 0) |
|---|
| 695 | 0 | return -1; |
|---|
| 696 | n/a | |
|---|
| 697 | 0 | if ((othersize = (*pb->bf_getreadbuffer)(value, 0, &ptr2)) < 0) |
|---|
| 698 | 0 | return -1; |
|---|
| 699 | n/a | |
|---|
| 700 | 0 | if (othersize != slicelength) { |
|---|
| 701 | 0 | PyErr_SetString( |
|---|
| 702 | n/a | PyExc_TypeError, |
|---|
| 703 | n/a | "right operand length must match slice length"); |
|---|
| 704 | 0 | return -1; |
|---|
| 705 | n/a | } |
|---|
| 706 | n/a | |
|---|
| 707 | 0 | if (slicelength == 0) |
|---|
| 708 | 0 | return 0; |
|---|
| 709 | 0 | else if (step == 1) { |
|---|
| 710 | 0 | memcpy((char *)ptr1 + start, ptr2, slicelength); |
|---|
| 711 | 0 | return 0; |
|---|
| 712 | n/a | } |
|---|
| 713 | n/a | else { |
|---|
| 714 | n/a | Py_ssize_t cur, i; |
|---|
| 715 | n/a | |
|---|
| 716 | 0 | for (cur = start, i = 0; i < slicelength; |
|---|
| 717 | 0 | cur += step, i++) { |
|---|
| 718 | 0 | ((char *)ptr1)[cur] = ((char *)ptr2)[i]; |
|---|
| 719 | n/a | } |
|---|
| 720 | n/a | |
|---|
| 721 | 0 | return 0; |
|---|
| 722 | n/a | } |
|---|
| 723 | n/a | } else { |
|---|
| 724 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 725 | n/a | "buffer indices must be integers"); |
|---|
| 726 | 0 | return -1; |
|---|
| 727 | n/a | } |
|---|
| 728 | n/a | } |
|---|
| 729 | n/a | |
|---|
| 730 | n/a | /* Buffer methods */ |
|---|
| 731 | n/a | |
|---|
| 732 | n/a | static Py_ssize_t |
|---|
| 733 | n/a | buffer_getreadbuf(PyBufferObject *self, Py_ssize_t idx, void **pp) |
|---|
| 734 | 9290 | { |
|---|
| 735 | n/a | Py_ssize_t size; |
|---|
| 736 | 9290 | if ( idx != 0 ) { |
|---|
| 737 | 0 | PyErr_SetString(PyExc_SystemError, |
|---|
| 738 | n/a | "accessing non-existent buffer segment"); |
|---|
| 739 | 0 | return -1; |
|---|
| 740 | n/a | } |
|---|
| 741 | 9290 | if (!get_buf(self, pp, &size, READ_BUFFER)) |
|---|
| 742 | 0 | return -1; |
|---|
| 743 | 9290 | return size; |
|---|
| 744 | n/a | } |
|---|
| 745 | n/a | |
|---|
| 746 | n/a | static Py_ssize_t |
|---|
| 747 | n/a | buffer_getwritebuf(PyBufferObject *self, Py_ssize_t idx, void **pp) |
|---|
| 748 | 5 | { |
|---|
| 749 | n/a | Py_ssize_t size; |
|---|
| 750 | n/a | |
|---|
| 751 | 5 | if ( self->b_readonly ) |
|---|
| 752 | n/a | { |
|---|
| 753 | 0 | PyErr_SetString(PyExc_TypeError, "buffer is read-only"); |
|---|
| 754 | 0 | return -1; |
|---|
| 755 | n/a | } |
|---|
| 756 | n/a | |
|---|
| 757 | 5 | if ( idx != 0 ) { |
|---|
| 758 | 0 | PyErr_SetString(PyExc_SystemError, |
|---|
| 759 | n/a | "accessing non-existent buffer segment"); |
|---|
| 760 | 0 | return -1; |
|---|
| 761 | n/a | } |
|---|
| 762 | 5 | if (!get_buf(self, pp, &size, WRITE_BUFFER)) |
|---|
| 763 | 0 | return -1; |
|---|
| 764 | 5 | return size; |
|---|
| 765 | n/a | } |
|---|
| 766 | n/a | |
|---|
| 767 | n/a | static Py_ssize_t |
|---|
| 768 | n/a | buffer_getsegcount(PyBufferObject *self, Py_ssize_t *lenp) |
|---|
| 769 | 9337 | { |
|---|
| 770 | n/a | void *ptr; |
|---|
| 771 | n/a | Py_ssize_t size; |
|---|
| 772 | 9337 | if (!get_buf(self, &ptr, &size, ANY_BUFFER)) |
|---|
| 773 | 0 | return -1; |
|---|
| 774 | 9337 | if (lenp) |
|---|
| 775 | 0 | *lenp = size; |
|---|
| 776 | 9337 | return 1; |
|---|
| 777 | n/a | } |
|---|
| 778 | n/a | |
|---|
| 779 | n/a | static Py_ssize_t |
|---|
| 780 | n/a | buffer_getcharbuf(PyBufferObject *self, Py_ssize_t idx, const char **pp) |
|---|
| 781 | 45 | { |
|---|
| 782 | n/a | void *ptr; |
|---|
| 783 | n/a | Py_ssize_t size; |
|---|
| 784 | 45 | if ( idx != 0 ) { |
|---|
| 785 | 0 | PyErr_SetString(PyExc_SystemError, |
|---|
| 786 | n/a | "accessing non-existent buffer segment"); |
|---|
| 787 | 0 | return -1; |
|---|
| 788 | n/a | } |
|---|
| 789 | 45 | if (!get_buf(self, &ptr, &size, CHAR_BUFFER)) |
|---|
| 790 | 1 | return -1; |
|---|
| 791 | 44 | *pp = (const char *)ptr; |
|---|
| 792 | 44 | return size; |
|---|
| 793 | n/a | } |
|---|
| 794 | n/a | |
|---|
| 795 | n/a | static PySequenceMethods buffer_as_sequence = { |
|---|
| 796 | n/a | (lenfunc)buffer_length, /*sq_length*/ |
|---|
| 797 | n/a | (binaryfunc)buffer_concat, /*sq_concat*/ |
|---|
| 798 | n/a | (ssizeargfunc)buffer_repeat, /*sq_repeat*/ |
|---|
| 799 | n/a | (ssizeargfunc)buffer_item, /*sq_item*/ |
|---|
| 800 | n/a | (ssizessizeargfunc)buffer_slice, /*sq_slice*/ |
|---|
| 801 | n/a | (ssizeobjargproc)buffer_ass_item, /*sq_ass_item*/ |
|---|
| 802 | n/a | (ssizessizeobjargproc)buffer_ass_slice, /*sq_ass_slice*/ |
|---|
| 803 | n/a | }; |
|---|
| 804 | n/a | |
|---|
| 805 | n/a | static PyMappingMethods buffer_as_mapping = { |
|---|
| 806 | n/a | (lenfunc)buffer_length, |
|---|
| 807 | n/a | (binaryfunc)buffer_subscript, |
|---|
| 808 | n/a | (objobjargproc)buffer_ass_subscript, |
|---|
| 809 | n/a | }; |
|---|
| 810 | n/a | |
|---|
| 811 | n/a | static PyBufferProcs buffer_as_buffer = { |
|---|
| 812 | n/a | (readbufferproc)buffer_getreadbuf, |
|---|
| 813 | n/a | (writebufferproc)buffer_getwritebuf, |
|---|
| 814 | n/a | (segcountproc)buffer_getsegcount, |
|---|
| 815 | n/a | (charbufferproc)buffer_getcharbuf, |
|---|
| 816 | n/a | }; |
|---|
| 817 | n/a | |
|---|
| 818 | n/a | PyTypeObject PyBuffer_Type = { |
|---|
| 819 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 820 | n/a | "buffer", |
|---|
| 821 | n/a | sizeof(PyBufferObject), |
|---|
| 822 | n/a | 0, |
|---|
| 823 | n/a | (destructor)buffer_dealloc, /* tp_dealloc */ |
|---|
| 824 | n/a | 0, /* tp_print */ |
|---|
| 825 | n/a | 0, /* tp_getattr */ |
|---|
| 826 | n/a | 0, /* tp_setattr */ |
|---|
| 827 | n/a | (cmpfunc)buffer_compare, /* tp_compare */ |
|---|
| 828 | n/a | (reprfunc)buffer_repr, /* tp_repr */ |
|---|
| 829 | n/a | 0, /* tp_as_number */ |
|---|
| 830 | n/a | &buffer_as_sequence, /* tp_as_sequence */ |
|---|
| 831 | n/a | &buffer_as_mapping, /* tp_as_mapping */ |
|---|
| 832 | n/a | (hashfunc)buffer_hash, /* tp_hash */ |
|---|
| 833 | n/a | 0, /* tp_call */ |
|---|
| 834 | n/a | (reprfunc)buffer_str, /* tp_str */ |
|---|
| 835 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 836 | n/a | 0, /* tp_setattro */ |
|---|
| 837 | n/a | &buffer_as_buffer, /* tp_as_buffer */ |
|---|
| 838 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GETCHARBUFFER, /* tp_flags */ |
|---|
| 839 | n/a | buffer_doc, /* tp_doc */ |
|---|
| 840 | n/a | 0, /* tp_traverse */ |
|---|
| 841 | n/a | 0, /* tp_clear */ |
|---|
| 842 | n/a | 0, /* tp_richcompare */ |
|---|
| 843 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 844 | n/a | 0, /* tp_iter */ |
|---|
| 845 | n/a | 0, /* tp_iternext */ |
|---|
| 846 | n/a | 0, /* tp_methods */ |
|---|
| 847 | n/a | 0, /* tp_members */ |
|---|
| 848 | n/a | 0, /* tp_getset */ |
|---|
| 849 | n/a | 0, /* tp_base */ |
|---|
| 850 | n/a | 0, /* tp_dict */ |
|---|
| 851 | n/a | 0, /* tp_descr_get */ |
|---|
| 852 | n/a | 0, /* tp_descr_set */ |
|---|
| 853 | n/a | 0, /* tp_dictoffset */ |
|---|
| 854 | n/a | 0, /* tp_init */ |
|---|
| 855 | n/a | 0, /* tp_alloc */ |
|---|
| 856 | n/a | buffer_new, /* tp_new */ |
|---|
| 857 | n/a | }; |
|---|