Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* Buffer object implementation */ |
| 12 | |
| 13 | #include "Python.h" |
| 14 | |
| 15 | |
| 16 | typedef struct { |
| 17 | PyObject_HEAD |
| 18 | PyObject *b_base; |
| 19 | void *b_ptr; |
| 20 | int b_size; |
| 21 | int b_readonly; |
| 22 | #ifdef CACHE_HASH |
| 23 | long b_hash; |
| 24 | #endif |
| 25 | } PyBufferObject; |
| 26 | |
| 27 | |
| 28 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 29 | _PyBuffer_FromMemory(PyObject *base, void *ptr, int size, int readonly) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 30 | { |
| 31 | PyBufferObject * b; |
| 32 | |
Guido van Rossum | 49ded3e | 1999-03-19 19:04:25 +0000 | [diff] [blame] | 33 | if ( size < 0 ) { |
| 34 | PyErr_SetString(PyExc_ValueError, |
| 35 | "size must be zero or positive"); |
| 36 | return NULL; |
| 37 | } |
| 38 | |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 39 | b = PyObject_NEW(PyBufferObject, &PyBuffer_Type); |
| 40 | if ( b == NULL ) |
| 41 | return NULL; |
| 42 | |
| 43 | Py_XINCREF(base); |
| 44 | b->b_base = base; |
| 45 | b->b_ptr = ptr; |
| 46 | b->b_size = size; |
| 47 | b->b_readonly = readonly; |
| 48 | #ifdef CACHE_HASH |
| 49 | b->b_hash = -1; |
| 50 | #endif |
| 51 | |
| 52 | return (PyObject *) b; |
| 53 | } |
| 54 | |
| 55 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 56 | _PyBuffer_FromObject(PyObject *base, int offset, int size, |
| 57 | getreadbufferproc proc, int readonly) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 58 | { |
| 59 | PyBufferProcs *pb = base->ob_type->tp_as_buffer; |
| 60 | void *p; |
| 61 | int count; |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | 49ded3e | 1999-03-19 19:04:25 +0000 | [diff] [blame] | 63 | if ( offset < 0 ) { |
| 64 | PyErr_SetString(PyExc_ValueError, |
| 65 | "offset must be zero or positive"); |
| 66 | return NULL; |
| 67 | } |
| 68 | |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 69 | if ( (*pb->bf_getsegcount)(base, NULL) != 1 ) |
| 70 | { |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 71 | PyErr_SetString(PyExc_TypeError, |
| 72 | "single-segment buffer object expected"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 73 | return NULL; |
| 74 | } |
| 75 | if ( (count = (*proc)(base, 0, &p)) < 0 ) |
| 76 | return NULL; |
| 77 | |
| 78 | /* apply constraints to the start/end */ |
Guido van Rossum | 49ded3e | 1999-03-19 19:04:25 +0000 | [diff] [blame] | 79 | if ( size == Py_END_OF_BUFFER || size < 0 ) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 80 | size = count; |
| 81 | if ( offset > count ) |
| 82 | offset = count; |
| 83 | if ( offset + size > count ) |
| 84 | size = count - offset; |
| 85 | |
| 86 | /* if the base object is another buffer, then "deref" it */ |
| 87 | if ( PyBuffer_Check(base) ) |
| 88 | base = ((PyBufferObject *)base)->b_base; |
| 89 | |
| 90 | return _PyBuffer_FromMemory(base, (char *)p + offset, size, readonly); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 95 | PyBuffer_FromObject(PyObject *base, int offset, int size) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 96 | { |
| 97 | PyBufferProcs *pb = base->ob_type->tp_as_buffer; |
| 98 | |
| 99 | if ( pb == NULL || |
| 100 | pb->bf_getreadbuffer == NULL || |
| 101 | pb->bf_getsegcount == NULL ) |
| 102 | { |
| 103 | PyErr_SetString(PyExc_TypeError, "buffer object expected"); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 107 | return _PyBuffer_FromObject(base, offset, size, |
| 108 | pb->bf_getreadbuffer, 1); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 112 | PyBuffer_FromReadWriteObject(PyObject *base, int offset, int size) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 113 | { |
| 114 | PyBufferProcs *pb = base->ob_type->tp_as_buffer; |
| 115 | |
| 116 | if ( pb == NULL || |
| 117 | pb->bf_getwritebuffer == NULL || |
| 118 | pb->bf_getsegcount == NULL ) |
| 119 | { |
| 120 | PyErr_SetString(PyExc_TypeError, "buffer object expected"); |
| 121 | return NULL; |
| 122 | } |
| 123 | |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 124 | return _PyBuffer_FromObject(base, offset, size, |
| 125 | (getreadbufferproc)pb->bf_getwritebuffer, |
| 126 | 0); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 130 | PyBuffer_FromMemory(void *ptr, int size) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 131 | { |
| 132 | return _PyBuffer_FromMemory(NULL, ptr, size, 1); |
| 133 | } |
| 134 | |
| 135 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 136 | PyBuffer_FromReadWriteMemory(void *ptr, int size) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 137 | { |
| 138 | return _PyBuffer_FromMemory(NULL, ptr, size, 0); |
| 139 | } |
| 140 | |
| 141 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 142 | PyBuffer_New(int size) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 143 | { |
| 144 | PyBufferObject * b; |
| 145 | |
Fred Drake | 4574f23 | 1999-08-04 13:08:19 +0000 | [diff] [blame] | 146 | if (size < 0) { |
| 147 | PyErr_SetString(PyExc_ValueError, |
| 148 | "size must be zero or positive"); |
| 149 | return NULL; |
| 150 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 151 | /* PyObject_New is inlined */ |
| 152 | b = (PyBufferObject *) PyObject_MALLOC(sizeof(*b) + size); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 153 | if ( b == NULL ) |
Fred Drake | 4574f23 | 1999-08-04 13:08:19 +0000 | [diff] [blame] | 154 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 155 | PyObject_INIT((PyObject *)b, &PyBuffer_Type); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 156 | |
| 157 | b->b_base = NULL; |
| 158 | b->b_ptr = (void *)(b + 1); |
| 159 | b->b_size = size; |
| 160 | b->b_readonly = 0; |
| 161 | #ifdef CACHE_HASH |
| 162 | b->b_hash = -1; |
| 163 | #endif |
| 164 | |
| 165 | return (PyObject *) b; |
| 166 | } |
| 167 | |
| 168 | /* Methods */ |
| 169 | |
| 170 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 171 | buffer_dealloc(PyBufferObject *self) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 172 | { |
| 173 | Py_XDECREF(self->b_base); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 174 | PyObject_DEL(self); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 178 | buffer_compare(PyBufferObject *self, PyBufferObject *other) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 179 | { |
| 180 | int len_self = self->b_size; |
| 181 | int len_other = other->b_size; |
| 182 | int min_len = (len_self < len_other) ? len_self : len_other; |
| 183 | int cmp; |
| 184 | if (min_len > 0) { |
| 185 | cmp = memcmp(self->b_ptr, other->b_ptr, min_len); |
| 186 | if (cmp != 0) |
| 187 | return cmp; |
| 188 | } |
| 189 | return (len_self < len_other) ? -1 : (len_self > len_other) ? 1 : 0; |
| 190 | } |
| 191 | |
| 192 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 193 | buffer_repr(PyBufferObject *self) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 194 | { |
| 195 | char buf[300]; |
| 196 | char *status = self->b_readonly ? "read-only" : "read-write"; |
| 197 | |
| 198 | if ( self->b_base == NULL ) |
| 199 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 200 | sprintf(buf, "<%s buffer ptr %p, size %d at %p>", |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 201 | status, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 202 | self->b_ptr, |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 203 | self->b_size, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 204 | self); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 205 | } |
| 206 | else |
| 207 | { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 208 | sprintf(buf, "<%s buffer for %p, ptr %p, size %d at %p>", |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 209 | status, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 210 | self->b_base, |
| 211 | self->b_ptr, |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 212 | self->b_size, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 213 | self); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return PyString_FromString(buf); |
| 217 | } |
| 218 | |
| 219 | static long |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 220 | buffer_hash(PyBufferObject *self) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 221 | { |
| 222 | register int len; |
| 223 | register unsigned char *p; |
| 224 | register long x; |
| 225 | |
| 226 | #ifdef CACHE_HASH |
| 227 | if ( self->b_hash != -1 ) |
| 228 | return self->b_hash; |
| 229 | #endif |
| 230 | |
| 231 | if ( !self->b_readonly ) |
| 232 | { |
| 233 | /* ### use different wording, since this is conditional? */ |
| 234 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
| 235 | return -1; |
| 236 | } |
| 237 | |
| 238 | len = self->b_size; |
| 239 | p = (unsigned char *) self->b_ptr; |
| 240 | x = *p << 7; |
| 241 | while (--len >= 0) |
| 242 | x = (1000003*x) ^ *p++; |
| 243 | x ^= self->b_size; |
| 244 | if (x == -1) |
| 245 | x = -2; |
| 246 | #ifdef CACHE_HASH |
| 247 | self->b_hash = x; |
| 248 | #endif |
| 249 | return x; |
| 250 | } |
| 251 | |
| 252 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 253 | buffer_str(PyBufferObject *self) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 254 | { |
| 255 | return PyString_FromStringAndSize(self->b_ptr, self->b_size); |
| 256 | } |
| 257 | |
| 258 | /* Sequence methods */ |
| 259 | |
| 260 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 261 | buffer_length(PyBufferObject *self) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 262 | { |
| 263 | return self->b_size; |
| 264 | } |
| 265 | |
| 266 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 267 | buffer_concat(PyBufferObject *self, PyObject *other) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 268 | { |
| 269 | PyBufferProcs *pb = other->ob_type->tp_as_buffer; |
| 270 | char *p1; |
| 271 | void *p2; |
| 272 | PyObject *ob; |
| 273 | int count; |
| 274 | |
| 275 | if ( pb == NULL || |
| 276 | pb->bf_getreadbuffer == NULL || |
| 277 | pb->bf_getsegcount == NULL ) |
| 278 | { |
| 279 | PyErr_BadArgument(); |
| 280 | return NULL; |
| 281 | } |
| 282 | if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) |
| 283 | { |
| 284 | /* ### use a different exception type/message? */ |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 285 | PyErr_SetString(PyExc_TypeError, |
| 286 | "single-segment buffer object expected"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | /* optimize special case */ |
| 291 | if ( self->b_size == 0 ) |
| 292 | { |
| 293 | Py_INCREF(other); |
| 294 | return other; |
| 295 | } |
| 296 | |
| 297 | if ( (count = (*pb->bf_getreadbuffer)(other, 0, &p2)) < 0 ) |
| 298 | return NULL; |
| 299 | |
| 300 | /* optimize special case */ |
| 301 | if ( count == 0 ) |
| 302 | { |
| 303 | Py_INCREF(self); |
| 304 | return (PyObject *)self; |
| 305 | } |
| 306 | |
| 307 | ob = PyString_FromStringAndSize(NULL, self->b_size + count); |
| 308 | p1 = PyString_AS_STRING(ob); |
| 309 | memcpy(p1, self->b_ptr, self->b_size); |
| 310 | memcpy(p1 + self->b_size, p2, count); |
| 311 | |
| 312 | /* there is an extra byte in the string object, so this is safe */ |
| 313 | p1[self->b_size + count] = '\0'; |
| 314 | |
| 315 | return ob; |
| 316 | } |
| 317 | |
| 318 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 319 | buffer_repeat(PyBufferObject *self, int count) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 320 | { |
| 321 | PyObject *ob; |
| 322 | register char *p; |
| 323 | void *ptr = self->b_ptr; |
| 324 | int size = self->b_size; |
| 325 | |
| 326 | if ( count < 0 ) |
| 327 | count = 0; |
| 328 | ob = PyString_FromStringAndSize(NULL, size * count); |
| 329 | if ( ob == NULL ) |
| 330 | return NULL; |
| 331 | |
| 332 | p = PyString_AS_STRING(ob); |
| 333 | while ( count-- ) |
| 334 | { |
| 335 | memcpy(p, ptr, size); |
| 336 | p += size; |
| 337 | } |
| 338 | |
| 339 | /* there is an extra byte in the string object, so this is safe */ |
| 340 | *p = '\0'; |
| 341 | |
| 342 | return ob; |
| 343 | } |
| 344 | |
| 345 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 346 | buffer_item(PyBufferObject *self, int idx) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 347 | { |
| 348 | if ( idx < 0 || idx >= self->b_size ) |
| 349 | { |
| 350 | PyErr_SetString(PyExc_IndexError, "buffer index out of range"); |
| 351 | return NULL; |
| 352 | } |
| 353 | return PyString_FromStringAndSize((char *)self->b_ptr + idx, 1); |
| 354 | } |
| 355 | |
| 356 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 357 | buffer_slice(PyBufferObject *self, int left, int right) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 358 | { |
| 359 | if ( left < 0 ) |
| 360 | left = 0; |
| 361 | if ( right < 0 ) |
| 362 | right = 0; |
| 363 | if ( right > self->b_size ) |
| 364 | right = self->b_size; |
| 365 | if ( left == 0 && right == self->b_size ) |
| 366 | { |
| 367 | /* same as self */ |
| 368 | Py_INCREF(self); |
| 369 | return (PyObject *)self; |
| 370 | } |
| 371 | if ( right < left ) |
| 372 | right = left; |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 373 | return PyString_FromStringAndSize((char *)self->b_ptr + left, |
| 374 | right - left); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 378 | buffer_ass_item(PyBufferObject *self, int idx, PyObject *other) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 379 | { |
| 380 | PyBufferProcs *pb; |
| 381 | void *p; |
| 382 | int count; |
| 383 | |
| 384 | if ( self->b_readonly ) { |
| 385 | PyErr_SetString(PyExc_TypeError, |
| 386 | "buffer is read-only"); |
| 387 | return -1; |
| 388 | } |
| 389 | |
| 390 | if (idx < 0 || idx >= self->b_size) { |
| 391 | PyErr_SetString(PyExc_IndexError, |
| 392 | "buffer assignment index out of range"); |
| 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | pb = other ? other->ob_type->tp_as_buffer : NULL; |
| 397 | if ( pb == NULL || |
| 398 | pb->bf_getreadbuffer == NULL || |
| 399 | pb->bf_getsegcount == NULL ) |
| 400 | { |
| 401 | PyErr_BadArgument(); |
| 402 | return -1; |
| 403 | } |
| 404 | if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) |
| 405 | { |
| 406 | /* ### use a different exception type/message? */ |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 407 | PyErr_SetString(PyExc_TypeError, |
| 408 | "single-segment buffer object expected"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | if ( (count = (*pb->bf_getreadbuffer)(other, 0, &p)) < 0 ) |
| 413 | return -1; |
| 414 | if ( count != 1 ) { |
| 415 | PyErr_SetString(PyExc_TypeError, |
| 416 | "right operand must be a single byte"); |
| 417 | return -1; |
| 418 | } |
| 419 | |
| 420 | ((char *)self->b_ptr)[idx] = *(char *)p; |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 425 | buffer_ass_slice(PyBufferObject *self, int left, int right, PyObject *other) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 426 | { |
| 427 | PyBufferProcs *pb; |
| 428 | void *p; |
| 429 | int slice_len; |
| 430 | int count; |
| 431 | |
| 432 | if ( self->b_readonly ) { |
| 433 | PyErr_SetString(PyExc_TypeError, |
| 434 | "buffer is read-only"); |
| 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | pb = other ? other->ob_type->tp_as_buffer : NULL; |
| 439 | if ( pb == NULL || |
| 440 | pb->bf_getreadbuffer == NULL || |
| 441 | pb->bf_getsegcount == NULL ) |
| 442 | { |
| 443 | PyErr_BadArgument(); |
| 444 | return -1; |
| 445 | } |
| 446 | if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) |
| 447 | { |
| 448 | /* ### use a different exception type/message? */ |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 449 | PyErr_SetString(PyExc_TypeError, |
| 450 | "single-segment buffer object expected"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 451 | return -1; |
| 452 | } |
| 453 | if ( (count = (*pb->bf_getreadbuffer)(other, 0, &p)) < 0 ) |
| 454 | return -1; |
| 455 | |
| 456 | if ( left < 0 ) |
| 457 | left = 0; |
| 458 | else if ( left > self->b_size ) |
| 459 | left = self->b_size; |
| 460 | if ( right < left ) |
| 461 | right = left; |
| 462 | else if ( right > self->b_size ) |
| 463 | right = self->b_size; |
| 464 | slice_len = right - left; |
| 465 | |
| 466 | if ( count != slice_len ) { |
Guido van Rossum | cd037e7 | 1999-03-24 19:05:31 +0000 | [diff] [blame] | 467 | PyErr_SetString( |
| 468 | PyExc_TypeError, |
| 469 | "right operand length must match slice length"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 470 | return -1; |
| 471 | } |
| 472 | |
| 473 | if ( slice_len ) |
| 474 | memcpy((char *)self->b_ptr + left, p, slice_len); |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | /* Buffer methods */ |
| 480 | |
| 481 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 482 | buffer_getreadbuf(PyBufferObject *self, int idx, void **pp) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 483 | { |
| 484 | if ( idx != 0 ) { |
| 485 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 486 | "accessing non-existent buffer segment"); |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 487 | return -1; |
| 488 | } |
| 489 | *pp = self->b_ptr; |
| 490 | return self->b_size; |
| 491 | } |
| 492 | |
| 493 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 494 | buffer_getwritebuf(PyBufferObject *self, int idx, void **pp) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 495 | { |
| 496 | if ( self->b_readonly ) |
| 497 | { |
| 498 | PyErr_SetString(PyExc_TypeError, "buffer is read-only"); |
| 499 | return -1; |
| 500 | } |
| 501 | return buffer_getreadbuf(self, idx, pp); |
| 502 | } |
| 503 | |
| 504 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 505 | buffer_getsegcount(PyBufferObject *self, int *lenp) |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 506 | { |
| 507 | if ( lenp ) |
| 508 | *lenp = self->b_size; |
| 509 | return 1; |
| 510 | } |
| 511 | |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 512 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame^] | 513 | buffer_getcharbuf(PyBufferObject *self, int idx, const char **pp) |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 514 | { |
| 515 | if ( idx != 0 ) { |
| 516 | PyErr_SetString(PyExc_SystemError, |
| 517 | "accessing non-existent buffer segment"); |
| 518 | return -1; |
| 519 | } |
| 520 | *pp = (const char *)self->b_ptr; |
| 521 | return self->b_size; |
| 522 | } |
| 523 | |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 524 | |
| 525 | static PySequenceMethods buffer_as_sequence = { |
| 526 | (inquiry)buffer_length, /*sq_length*/ |
| 527 | (binaryfunc)buffer_concat, /*sq_concat*/ |
| 528 | (intargfunc)buffer_repeat, /*sq_repeat*/ |
| 529 | (intargfunc)buffer_item, /*sq_item*/ |
| 530 | (intintargfunc)buffer_slice, /*sq_slice*/ |
| 531 | (intobjargproc)buffer_ass_item, /*sq_ass_item*/ |
| 532 | (intintobjargproc)buffer_ass_slice, /*sq_ass_slice*/ |
| 533 | }; |
| 534 | |
| 535 | static PyBufferProcs buffer_as_buffer = { |
| 536 | (getreadbufferproc)buffer_getreadbuf, |
| 537 | (getwritebufferproc)buffer_getwritebuf, |
| 538 | (getsegcountproc)buffer_getsegcount, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 539 | (getcharbufferproc)buffer_getcharbuf, |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | PyTypeObject PyBuffer_Type = { |
| 543 | PyObject_HEAD_INIT(&PyType_Type) |
| 544 | 0, |
| 545 | "buffer", |
| 546 | sizeof(PyBufferObject), |
| 547 | 0, |
| 548 | (destructor)buffer_dealloc, /*tp_dealloc*/ |
| 549 | 0, /*tp_print*/ |
| 550 | 0, /*tp_getattr*/ |
| 551 | 0, /*tp_setattr*/ |
| 552 | (cmpfunc)buffer_compare, /*tp_compare*/ |
| 553 | (reprfunc)buffer_repr, /*tp_repr*/ |
| 554 | 0, /*tp_as_number*/ |
| 555 | &buffer_as_sequence, /*tp_as_sequence*/ |
| 556 | 0, /*tp_as_mapping*/ |
| 557 | (hashfunc)buffer_hash, /*tp_hash*/ |
| 558 | 0, /*tp_call*/ |
| 559 | (reprfunc)buffer_str, /*tp_str*/ |
| 560 | 0, /*tp_getattro*/ |
| 561 | 0, /*tp_setattro*/ |
| 562 | &buffer_as_buffer, /*tp_as_buffer*/ |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 563 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
Guido van Rossum | 2e19bd7 | 1998-10-07 14:36:10 +0000 | [diff] [blame] | 564 | 0, /*tp_doc*/ |
| 565 | }; |