| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Buffer object implementation */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  | 	PyObject_HEAD | 
					
						
							|  |  |  | 	PyObject *b_base; | 
					
						
							|  |  |  | 	void *b_ptr; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t b_size; | 
					
						
							|  |  |  | 	Py_ssize_t b_offset; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	int b_readonly; | 
					
						
							|  |  |  | 	long b_hash; | 
					
						
							|  |  |  | } PyBufferObject; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | enum buffer_t { | 
					
						
							| 
									
										
										
										
											2006-06-09 17:05:48 +00:00
										 |  |  |     READ_BUFFER, | 
					
						
							|  |  |  |     WRITE_BUFFER, | 
					
						
							|  |  |  |     CHAR_BUFFER, | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  |     ANY_BUFFER, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | get_buf(PyBufferObject *self, void **ptr, Py_ssize_t *size, | 
					
						
							|  |  |  | 	enum buffer_t buffer_type) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (self->b_base == NULL) { | 
					
						
							|  |  |  | 		assert (ptr != NULL); | 
					
						
							|  |  |  | 		*ptr = self->b_ptr; | 
					
						
							|  |  |  | 		*size = self->b_size; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 		Py_ssize_t count, offset; | 
					
						
							| 
									
										
										
										
											2006-06-10 22:38:13 +00:00
										 |  |  | 		readbufferproc proc = 0; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		PyBufferProcs *bp = self->b_base->ob_type->tp_as_buffer; | 
					
						
							|  |  |  | 		if ((*bp->bf_getsegcount)(self->b_base, NULL) != 1) { | 
					
						
							|  |  |  | 			PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"single-segment buffer object expected"); | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-06-09 17:05:48 +00:00
										 |  |  | 		if ((buffer_type == READ_BUFFER) || | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 			((buffer_type == ANY_BUFFER) && self->b_readonly)) | 
					
						
							|  |  |  | 		    proc = bp->bf_getreadbuffer; | 
					
						
							| 
									
										
										
										
											2006-06-09 17:05:48 +00:00
										 |  |  | 		else if ((buffer_type == WRITE_BUFFER) || | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 			(buffer_type == ANY_BUFFER)) | 
					
						
							|  |  |  |     		    proc = (readbufferproc)bp->bf_getwritebuffer; | 
					
						
							| 
									
										
										
										
											2006-06-09 17:05:48 +00:00
										 |  |  | 		else if (buffer_type == CHAR_BUFFER) { | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 		    if (!PyType_HasFeature(self->ob_type, | 
					
						
							|  |  |  | 				Py_TPFLAGS_HAVE_GETCHARBUFFER)) { | 
					
						
							|  |  |  | 			PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"Py_TPFLAGS_HAVE_GETCHARBUFFER needed"); | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		    } | 
					
						
							|  |  |  | 		    proc = (readbufferproc)bp->bf_getcharbuffer; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (!proc) { | 
					
						
							|  |  |  | 		    char *buffer_type_name; | 
					
						
							|  |  |  | 		    switch (buffer_type) { | 
					
						
							| 
									
										
										
										
											2006-06-09 17:05:48 +00:00
										 |  |  | 			case READ_BUFFER: | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 			    buffer_type_name = "read"; | 
					
						
							|  |  |  | 			    break; | 
					
						
							| 
									
										
										
										
											2006-06-09 17:05:48 +00:00
										 |  |  | 			case WRITE_BUFFER: | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 			    buffer_type_name = "write"; | 
					
						
							|  |  |  | 			    break; | 
					
						
							| 
									
										
										
										
											2006-06-09 17:05:48 +00:00
										 |  |  | 			case CHAR_BUFFER: | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 			    buffer_type_name = "char"; | 
					
						
							|  |  |  | 			    break; | 
					
						
							|  |  |  | 			default: | 
					
						
							|  |  |  | 			    buffer_type_name = "no"; | 
					
						
							|  |  |  | 			    break; | 
					
						
							|  |  |  | 		    } | 
					
						
							|  |  |  | 		    PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  | 			    "%s buffer type not available", | 
					
						
							|  |  |  | 			    buffer_type_name); | 
					
						
							|  |  |  | 		    return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		if ((count = (*proc)(self->b_base, 0, ptr)) < 0) | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		/* apply constraints to the start/end */ | 
					
						
							|  |  |  | 		if (self->b_offset > count) | 
					
						
							|  |  |  | 			offset = count; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			offset = self->b_offset; | 
					
						
							| 
									
										
										
										
											2004-03-25 16:16:28 +00:00
										 |  |  | 		*(char **)ptr = *(char **)ptr + offset; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		if (self->b_size == Py_END_OF_BUFFER) | 
					
						
							|  |  |  | 			*size = count; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			*size = self->b_size; | 
					
						
							|  |  |  | 		if (offset + *size > count) | 
					
						
							|  |  |  | 			*size = count - offset; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | buffer_from_memory(PyObject *base, Py_ssize_t size, Py_ssize_t offset, void *ptr, | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		   int readonly) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyBufferObject * b; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	if (size < 0 && size != Py_END_OF_BUFFER) { | 
					
						
							| 
									
										
										
										
											1999-03-19 19:04:25 +00:00
										 |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 				"size must be zero or positive"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-24 15:41:27 +00:00
										 |  |  | 	if (offset < 0) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 				"offset must be zero or positive"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1999-03-19 19:04:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	b = PyObject_NEW(PyBufferObject, &PyBuffer_Type); | 
					
						
							|  |  |  | 	if ( b == NULL ) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Py_XINCREF(base); | 
					
						
							|  |  |  | 	b->b_base = base; | 
					
						
							|  |  |  | 	b->b_ptr = ptr; | 
					
						
							|  |  |  | 	b->b_size = size; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	b->b_offset = offset; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	b->b_readonly = readonly; | 
					
						
							|  |  |  | 	b->b_hash = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (PyObject *) b; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | buffer_from_object(PyObject *base, Py_ssize_t size, Py_ssize_t offset, int readonly) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-24 19:17:26 +00:00
										 |  |  | 	if (offset < 0) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 				"offset must be zero or positive"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	if ( PyBuffer_Check(base) && (((PyBufferObject *)base)->b_base) ) { | 
					
						
							| 
									
										
										
										
											2004-09-24 15:41:27 +00:00
										 |  |  | 		/* another buffer, refer to the base object */ | 
					
						
							| 
									
										
										
										
											2004-09-24 19:17:26 +00:00
										 |  |  | 		PyBufferObject *b = (PyBufferObject *)base; | 
					
						
							|  |  |  | 		if (b->b_size != Py_END_OF_BUFFER) { | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 			Py_ssize_t base_size = b->b_size - offset; | 
					
						
							| 
									
										
										
										
											2004-09-24 19:17:26 +00:00
										 |  |  | 			if (base_size < 0) | 
					
						
							|  |  |  | 				base_size = 0; | 
					
						
							|  |  |  | 			if (size == Py_END_OF_BUFFER || size > base_size) | 
					
						
							|  |  |  | 				size = base_size; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		offset += b->b_offset; | 
					
						
							|  |  |  | 		base = b->b_base; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return buffer_from_memory(base, size, offset, NULL, readonly); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyBufferProcs *pb = base->ob_type->tp_as_buffer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ( pb == NULL || | 
					
						
							|  |  |  | 	     pb->bf_getreadbuffer == NULL || | 
					
						
							|  |  |  | 	     pb->bf_getsegcount == NULL ) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_TypeError, "buffer object expected"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	return buffer_from_object(base, size, offset, 1); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyBufferProcs *pb = base->ob_type->tp_as_buffer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ( pb == NULL || | 
					
						
							|  |  |  | 	     pb->bf_getwritebuffer == NULL || | 
					
						
							|  |  |  | 	     pb->bf_getsegcount == NULL ) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_TypeError, "buffer object expected"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	return buffer_from_object(base, size,  offset, 0); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | PyBuffer_FromMemory(void *ptr, Py_ssize_t size) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	return buffer_from_memory(NULL, size, 0, ptr, 1); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	return buffer_from_memory(NULL, size, 0, ptr, 0); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | PyBuffer_New(Py_ssize_t size) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2000-08-04 15:36:13 +00:00
										 |  |  | 	PyObject *o; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	PyBufferObject * b; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-08-04 13:08:19 +00:00
										 |  |  | 	if (size < 0) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 				"size must be zero or positive"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	/* XXX: check for overflow in multiply */ | 
					
						
							| 
									
										
										
										
											2002-08-19 19:26:42 +00:00
										 |  |  | 	/* Inline PyObject_New */ | 
					
						
							| 
									
										
										
										
											2006-04-11 06:54:30 +00:00
										 |  |  | 	o = (PyObject *)PyObject_MALLOC(sizeof(*b) + size); | 
					
						
							| 
									
										
										
										
											2000-08-04 15:36:13 +00:00
										 |  |  | 	if ( o == NULL ) | 
					
						
							| 
									
										
										
										
											1999-08-04 13:08:19 +00:00
										 |  |  | 		return PyErr_NoMemory(); | 
					
						
							| 
									
										
										
										
											2000-08-04 15:36:13 +00:00
										 |  |  | 	b = (PyBufferObject *) PyObject_INIT(o, &PyBuffer_Type); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	b->b_base = NULL; | 
					
						
							|  |  |  | 	b->b_ptr = (void *)(b + 1); | 
					
						
							|  |  |  | 	b->b_size = size; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	b->b_offset = 0; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	b->b_readonly = 0; | 
					
						
							|  |  |  | 	b->b_hash = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-04 15:36:13 +00:00
										 |  |  | 	return o; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Methods */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-14 20:41:17 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PyObject *ob; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t offset = 0; | 
					
						
							|  |  |  | 	Py_ssize_t size = Py_END_OF_BUFFER; | 
					
						
							| 
									
										
										
										
											2002-06-14 20:41:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-26 06:42:30 +00:00
										 |  |  | 	if (!_PyArg_NoKeywords("buffer()", kw)) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-16 19:44:46 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "O|nn:buffer", &ob, &offset, &size)) | 
					
						
							| 
									
										
										
										
											2002-06-14 20:41:17 +00:00
										 |  |  | 	    return NULL; | 
					
						
							|  |  |  | 	return PyBuffer_FromObject(ob, offset, size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(buffer_doc, | 
					
						
							|  |  |  | "buffer(object [, offset[, size]])\n\
 | 
					
						
							|  |  |  | \n\ | 
					
						
							|  |  |  | Create a new buffer object which references the given object.\n\ | 
					
						
							|  |  |  | The buffer will reference a slice of the target object from the\n\ | 
					
						
							|  |  |  | start of the object (or at the specified offset). The slice will\n\ | 
					
						
							|  |  |  | extend to the end of the target object (or with the specified size)."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-09 04:06:11 +00:00
										 |  |  | buffer_dealloc(PyBufferObject *self) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	Py_XDECREF(self->b_base); | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  | 	PyObject_DEL(self); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2000-07-09 04:06:11 +00:00
										 |  |  | buffer_compare(PyBufferObject *self, PyBufferObject *other) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *p1, *p2; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t len_self, len_other, min_len; | 
					
						
							|  |  |  | 	int cmp; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	if (!get_buf(self, &p1, &len_self, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	if (!get_buf(other, &p2, &len_other, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	min_len = (len_self < len_other) ? len_self : len_other; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	if (min_len > 0) { | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		cmp = memcmp(p1, p2, min_len); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		if (cmp != 0) | 
					
						
							| 
									
										
										
										
											2006-08-08 17:37:00 +00:00
										 |  |  | 			return cmp < 0 ? -1 : 1; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return (len_self < len_other) ? -1 : (len_self > len_other) ? 1 : 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 04:06:11 +00:00
										 |  |  | buffer_repr(PyBufferObject *self) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	const char *status = self->b_readonly ? "read-only" : "read-write"; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ( self->b_base == NULL ) | 
					
						
							| 
									
										
										
										
											2006-02-16 06:54:25 +00:00
										 |  |  | 		return PyString_FromFormat("<%s buffer ptr %p, size %zd at %p>", | 
					
						
							| 
									
										
										
										
											2001-08-24 18:34:26 +00:00
										 |  |  | 					   status, | 
					
						
							|  |  |  | 					   self->b_ptr, | 
					
						
							| 
									
										
										
										
											2006-02-16 06:59:22 +00:00
										 |  |  | 					   self->b_size, | 
					
						
							| 
									
										
										
										
											2001-08-24 18:34:26 +00:00
										 |  |  | 					   self); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2001-08-24 18:34:26 +00:00
										 |  |  | 		return PyString_FromFormat( | 
					
						
							| 
									
										
										
										
											2006-02-16 06:54:25 +00:00
										 |  |  | 			"<%s buffer for %p, size %zd, offset %zd at %p>", | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 			status, | 
					
						
							| 
									
										
										
										
											2000-06-30 15:01:00 +00:00
										 |  |  | 			self->b_base, | 
					
						
							| 
									
										
										
										
											2006-02-16 06:59:22 +00:00
										 |  |  | 			self->b_size, | 
					
						
							|  |  |  | 			self->b_offset, | 
					
						
							| 
									
										
										
										
											2000-06-30 15:01:00 +00:00
										 |  |  | 			self); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static long | 
					
						
							| 
									
										
										
										
											2000-07-09 04:06:11 +00:00
										 |  |  | buffer_hash(PyBufferObject *self) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *ptr; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							|  |  |  | 	register Py_ssize_t len; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	register unsigned char *p; | 
					
						
							|  |  |  | 	register long x; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ( self->b_hash != -1 ) | 
					
						
							|  |  |  | 		return self->b_hash; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-11 01:00:44 +00:00
										 |  |  | 	/* XXX potential bugs here, a readonly buffer does not imply that the
 | 
					
						
							|  |  |  | 	 * underlying memory is immutable.  b_readonly is a necessary but not | 
					
						
							|  |  |  | 	 * sufficient condition for a buffer to be hashable.  Perhaps it would | 
					
						
							|  |  |  | 	 * be better to only allow hashing if the underlying object is known to | 
					
						
							|  |  |  | 	 * be immutable (e.g. PyString_Check() is true).  Another idea would | 
					
						
							|  |  |  | 	 * be to call tp_hash on the underlying object and see if it raises | 
					
						
							|  |  |  | 	 * an error. */ | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	if ( !self->b_readonly ) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2004-03-11 01:00:44 +00:00
										 |  |  | 		PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"writable buffers are not hashable"); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	if (!get_buf(self, &ptr, &size, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	p = (unsigned char *) ptr; | 
					
						
							|  |  |  | 	len = size; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	x = *p << 7; | 
					
						
							|  |  |  | 	while (--len >= 0) | 
					
						
							|  |  |  | 		x = (1000003*x) ^ *p++; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	x ^= size; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	if (x == -1) | 
					
						
							|  |  |  | 		x = -2; | 
					
						
							|  |  |  | 	self->b_hash = x; | 
					
						
							|  |  |  | 	return x; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 04:06:11 +00:00
										 |  |  | buffer_str(PyBufferObject *self) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *ptr; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	if (!get_buf(self, &ptr, &size, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2006-04-11 06:54:30 +00:00
										 |  |  | 	return PyString_FromStringAndSize((const char *)ptr, size); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Sequence methods */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | static Py_ssize_t | 
					
						
							| 
									
										
										
										
											2000-07-09 04:06:11 +00:00
										 |  |  | buffer_length(PyBufferObject *self) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *ptr; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	if (!get_buf(self, &ptr, &size, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	return size; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 04:06:11 +00:00
										 |  |  | buffer_concat(PyBufferObject *self, PyObject *other) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyBufferProcs *pb = other->ob_type->tp_as_buffer; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *ptr1, *ptr2; | 
					
						
							|  |  |  | 	char *p; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	PyObject *ob; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size, count; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ( pb == NULL || | 
					
						
							|  |  |  | 	     pb->bf_getreadbuffer == NULL || | 
					
						
							|  |  |  | 	     pb->bf_getsegcount == NULL ) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		PyErr_BadArgument(); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		/* ### use a different exception type/message? */ | 
					
						
							| 
									
										
										
										
											1999-03-24 19:05:31 +00:00
										 |  |  | 		PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"single-segment buffer object expected"); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  |  	if (!get_buf(self, &ptr1, &size, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  |  		return NULL; | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	/* optimize special case */ | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	if ( size == 0 ) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	    Py_INCREF(other); | 
					
						
							|  |  |  | 	    return other; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 ) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  |  	ob = PyString_FromStringAndSize(NULL, size + count); | 
					
						
							| 
									
										
										
										
											2005-12-18 08:02:38 +00:00
										 |  |  | 	if ( ob == NULL ) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  |  	p = PyString_AS_STRING(ob); | 
					
						
							|  |  |  |  	memcpy(p, ptr1, size); | 
					
						
							|  |  |  |  	memcpy(p + size, ptr2, count); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* there is an extra byte in the string object, so this is safe */ | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	p[size + count] = '\0'; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return ob; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | buffer_repeat(PyBufferObject *self, Py_ssize_t count) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *ob; | 
					
						
							|  |  |  | 	register char *p; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *ptr; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ( count < 0 ) | 
					
						
							|  |  |  | 		count = 0; | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	if (!get_buf(self, &ptr, &size, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	ob = PyString_FromStringAndSize(NULL, size * count); | 
					
						
							|  |  |  | 	if ( ob == NULL ) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	p = PyString_AS_STRING(ob); | 
					
						
							|  |  |  | 	while ( count-- ) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	    memcpy(p, ptr, size); | 
					
						
							|  |  |  | 	    p += size; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* there is an extra byte in the string object, so this is safe */ | 
					
						
							|  |  |  | 	*p = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ob; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | buffer_item(PyBufferObject *self, Py_ssize_t idx) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *ptr; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	if (!get_buf(self, &ptr, &size, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	if ( idx < 0 || idx >= size ) { | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		PyErr_SetString(PyExc_IndexError, "buffer index out of range"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	return PyString_FromStringAndSize((char *)ptr + idx, 1); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | buffer_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *ptr; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	if (!get_buf(self, &ptr, &size, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	if ( left < 0 ) | 
					
						
							|  |  |  | 		left = 0; | 
					
						
							|  |  |  | 	if ( right < 0 ) | 
					
						
							|  |  |  | 		right = 0; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	if ( right > size ) | 
					
						
							|  |  |  | 		right = size; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	if ( right < left ) | 
					
						
							|  |  |  | 		right = left; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	return PyString_FromStringAndSize((char *)ptr + left, | 
					
						
							| 
									
										
										
										
											1999-03-24 19:05:31 +00:00
										 |  |  | 					  right - left); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | buffer_ass_item(PyBufferObject *self, Py_ssize_t idx, PyObject *other) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyBufferProcs *pb; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *ptr1, *ptr2; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							|  |  |  | 	Py_ssize_t count; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ( self->b_readonly ) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"buffer is read-only"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	if (!get_buf(self, &ptr1, &size, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (idx < 0 || idx >= size) { | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		PyErr_SetString(PyExc_IndexError, | 
					
						
							|  |  |  | 				"buffer assignment index out of range"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pb = other ? other->ob_type->tp_as_buffer : NULL; | 
					
						
							|  |  |  | 	if ( pb == NULL || | 
					
						
							|  |  |  | 	     pb->bf_getreadbuffer == NULL || | 
					
						
							|  |  |  | 	     pb->bf_getsegcount == NULL ) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		PyErr_BadArgument(); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		/* ### use a different exception type/message? */ | 
					
						
							| 
									
										
										
										
											1999-03-24 19:05:31 +00:00
										 |  |  | 		PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"single-segment buffer object expected"); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 ) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	if ( count != 1 ) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"right operand must be a single byte"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	((char *)ptr1)[idx] = *(char *)ptr2; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | buffer_ass_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right, PyObject *other) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyBufferProcs *pb; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *ptr1, *ptr2; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							|  |  |  | 	Py_ssize_t slice_len; | 
					
						
							|  |  |  | 	Py_ssize_t count; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ( self->b_readonly ) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"buffer is read-only"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pb = other ? other->ob_type->tp_as_buffer : NULL; | 
					
						
							|  |  |  | 	if ( pb == NULL || | 
					
						
							|  |  |  | 	     pb->bf_getreadbuffer == NULL || | 
					
						
							|  |  |  | 	     pb->bf_getsegcount == NULL ) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		PyErr_BadArgument(); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ( (*pb->bf_getsegcount)(other, NULL) != 1 ) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		/* ### use a different exception type/message? */ | 
					
						
							| 
									
										
										
										
											1999-03-24 19:05:31 +00:00
										 |  |  | 		PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"single-segment buffer object expected"); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	if (!get_buf(self, &ptr1, &size, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	if ( (count = (*pb->bf_getreadbuffer)(other, 0, &ptr2)) < 0 ) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ( left < 0 ) | 
					
						
							|  |  |  | 		left = 0; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	else if ( left > size ) | 
					
						
							|  |  |  | 		left = size; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	if ( right < left ) | 
					
						
							|  |  |  | 		right = left; | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	else if ( right > size ) | 
					
						
							|  |  |  | 		right = size; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	slice_len = right - left; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ( count != slice_len ) { | 
					
						
							| 
									
										
										
										
											1999-03-24 19:05:31 +00:00
										 |  |  | 		PyErr_SetString( | 
					
						
							|  |  |  | 			PyExc_TypeError, | 
					
						
							|  |  |  | 			"right operand length must match slice length"); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ( slice_len ) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	    memcpy((char *)ptr1 + left, ptr2, slice_len); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Buffer methods */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | static Py_ssize_t | 
					
						
							|  |  |  | buffer_getreadbuf(PyBufferObject *self, Py_ssize_t idx, void **pp) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	if ( idx != 0 ) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_SystemError, | 
					
						
							| 
									
										
										
										
											1998-10-08 02:18:52 +00:00
										 |  |  | 				"accessing non-existent buffer segment"); | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-06-09 17:05:48 +00:00
										 |  |  | 	if (!get_buf(self, pp, &size, READ_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	return size; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | static Py_ssize_t | 
					
						
							|  |  |  | buffer_getwritebuf(PyBufferObject *self, Py_ssize_t idx, void **pp) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	if ( self->b_readonly ) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_TypeError, "buffer is read-only"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if ( idx != 0 ) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  | 				"accessing non-existent buffer segment"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-06-09 17:05:48 +00:00
										 |  |  | 	if (!get_buf(self, pp, &size, WRITE_BUFFER)) | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	return size; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | static Py_ssize_t | 
					
						
							|  |  |  | buffer_getsegcount(PyBufferObject *self, Py_ssize_t *lenp) | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *ptr; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	if (!get_buf(self, &ptr, &size, ANY_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	if (lenp) | 
					
						
							|  |  |  | 		*lenp = size; | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | static Py_ssize_t | 
					
						
							|  |  |  | buffer_getcharbuf(PyBufferObject *self, Py_ssize_t idx, const char **pp) | 
					
						
							| 
									
										
										
										
											1998-10-08 02:18:52 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 	void *ptr; | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	Py_ssize_t size; | 
					
						
							| 
									
										
										
										
											1998-10-08 02:18:52 +00:00
										 |  |  | 	if ( idx != 0 ) { | 
					
						
							|  |  |  | 		PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  | 				"accessing non-existent buffer segment"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-06-09 17:05:48 +00:00
										 |  |  | 	if (!get_buf(self, &ptr, &size, CHAR_BUFFER)) | 
					
						
							| 
									
										
										
										
											2004-03-11 02:42:45 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	*pp = (const char *)ptr; | 
					
						
							|  |  |  | 	return size; | 
					
						
							| 
									
										
										
										
											1998-10-08 02:18:52 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | static PySequenceMethods buffer_as_sequence = { | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	(lenfunc)buffer_length, /*sq_length*/ | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | 	(binaryfunc)buffer_concat, /*sq_concat*/ | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	(ssizeargfunc)buffer_repeat, /*sq_repeat*/ | 
					
						
							|  |  |  | 	(ssizeargfunc)buffer_item, /*sq_item*/ | 
					
						
							|  |  |  | 	(ssizessizeargfunc)buffer_slice, /*sq_slice*/ | 
					
						
							|  |  |  | 	(ssizeobjargproc)buffer_ass_item, /*sq_ass_item*/ | 
					
						
							|  |  |  | 	(ssizessizeobjargproc)buffer_ass_slice, /*sq_ass_slice*/ | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyBufferProcs buffer_as_buffer = { | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | 	(readbufferproc)buffer_getreadbuf, | 
					
						
							|  |  |  | 	(writebufferproc)buffer_getwritebuf, | 
					
						
							|  |  |  | 	(segcountproc)buffer_getsegcount, | 
					
						
							|  |  |  | 	(charbufferproc)buffer_getcharbuf, | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyTypeObject PyBuffer_Type = { | 
					
						
							|  |  |  | 	PyObject_HEAD_INIT(&PyType_Type) | 
					
						
							|  |  |  | 	0, | 
					
						
							|  |  |  | 	"buffer", | 
					
						
							|  |  |  | 	sizeof(PyBufferObject), | 
					
						
							|  |  |  | 	0, | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | 	(destructor)buffer_dealloc, 		/* tp_dealloc */ | 
					
						
							|  |  |  | 	0,					/* tp_print */ | 
					
						
							|  |  |  | 	0,					/* tp_getattr */ | 
					
						
							|  |  |  | 	0,					/* tp_setattr */ | 
					
						
							|  |  |  | 	(cmpfunc)buffer_compare,		/* tp_compare */ | 
					
						
							|  |  |  | 	(reprfunc)buffer_repr,			/* tp_repr */ | 
					
						
							|  |  |  | 	0,					/* tp_as_number */ | 
					
						
							|  |  |  | 	&buffer_as_sequence,			/* tp_as_sequence */ | 
					
						
							|  |  |  | 	0,					/* tp_as_mapping */ | 
					
						
							|  |  |  | 	(hashfunc)buffer_hash,			/* tp_hash */ | 
					
						
							|  |  |  | 	0,					/* tp_call */ | 
					
						
							|  |  |  | 	(reprfunc)buffer_str,			/* tp_str */ | 
					
						
							|  |  |  | 	PyObject_GenericGetAttr,		/* tp_getattro */ | 
					
						
							|  |  |  | 	0,					/* tp_setattro */ | 
					
						
							|  |  |  | 	&buffer_as_buffer,			/* tp_as_buffer */ | 
					
						
							| 
									
										
										
										
											2006-06-08 17:00:45 +00:00
										 |  |  | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GETCHARBUFFER, /* tp_flags */ | 
					
						
							| 
									
										
										
										
											2002-06-14 20:41:17 +00:00
										 |  |  | 	buffer_doc,				/* tp_doc */ | 
					
						
							|  |  |  | 	0,					/* tp_traverse */ | 
					
						
							|  |  |  | 	0,					/* tp_clear */ | 
					
						
							|  |  |  | 	0,					/* tp_richcompare */ | 
					
						
							|  |  |  | 	0,					/* tp_weaklistoffset */ | 
					
						
							|  |  |  | 	0,					/* tp_iter */ | 
					
						
							|  |  |  | 	0,					/* tp_iternext */ | 
					
						
							|  |  |  | 	0,					/* tp_methods */	 | 
					
						
							|  |  |  | 	0,					/* tp_members */ | 
					
						
							|  |  |  | 	0,					/* tp_getset */ | 
					
						
							|  |  |  | 	0,					/* tp_base */ | 
					
						
							|  |  |  | 	0,					/* tp_dict */ | 
					
						
							|  |  |  | 	0,					/* tp_descr_get */ | 
					
						
							|  |  |  | 	0,					/* tp_descr_set */ | 
					
						
							|  |  |  | 	0,					/* tp_dictoffset */ | 
					
						
							|  |  |  | 	0,					/* tp_init */ | 
					
						
							|  |  |  | 	0,					/* tp_alloc */ | 
					
						
							|  |  |  | 	buffer_new,				/* tp_new */ | 
					
						
							| 
									
										
										
										
											1998-10-07 14:36:10 +00:00
										 |  |  | }; |