summaryrefslogtreecommitdiffstats
path: root/Objects/bufferobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/bufferobject.c')
-rw-r--r--Objects/bufferobject.c124
1 files changed, 63 insertions, 61 deletions
diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c
index 56748e6..4a9e3ae 100644
--- a/Objects/bufferobject.c
+++ b/Objects/bufferobject.c
@@ -8,15 +8,15 @@ typedef struct {
PyObject_HEAD
PyObject *b_base;
void *b_ptr;
- int b_size;
- int b_offset;
+ Py_ssize_t b_size;
+ Py_ssize_t b_offset;
int b_readonly;
long b_hash;
} PyBufferObject;
static int
-get_buf(PyBufferObject *self, void **ptr, int *size)
+get_buf(PyBufferObject *self, void **ptr, Py_ssize_t *size)
{
if (self->b_base == NULL) {
assert (ptr != NULL);
@@ -24,8 +24,8 @@ get_buf(PyBufferObject *self, void **ptr, int *size)
*size = self->b_size;
}
else {
- int count, offset;
- getreadbufferproc proc;
+ Py_ssize_t count, offset;
+ readbufferproc proc;
PyBufferProcs *bp = self->b_base->ob_type->tp_as_buffer;
if ((*bp->bf_getsegcount)(self->b_base, NULL) != 1) {
PyErr_SetString(PyExc_TypeError,
@@ -35,7 +35,7 @@ get_buf(PyBufferObject *self, void **ptr, int *size)
if (self->b_readonly)
proc = bp->bf_getreadbuffer;
else
- proc = (getreadbufferproc)bp->bf_getwritebuffer;
+ proc = (readbufferproc)bp->bf_getwritebuffer;
if ((count = (*proc)(self->b_base, 0, ptr)) < 0)
return 0;
/* apply constraints to the start/end */
@@ -56,7 +56,7 @@ get_buf(PyBufferObject *self, void **ptr, int *size)
static PyObject *
-buffer_from_memory(PyObject *base, int size, int offset, void *ptr,
+buffer_from_memory(PyObject *base, Py_ssize_t size, Py_ssize_t offset, void *ptr,
int readonly)
{
PyBufferObject * b;
@@ -88,7 +88,7 @@ buffer_from_memory(PyObject *base, int size, int offset, void *ptr,
}
static PyObject *
-buffer_from_object(PyObject *base, int size, int offset, int readonly)
+buffer_from_object(PyObject *base, Py_ssize_t size, Py_ssize_t offset, int readonly)
{
if (offset < 0) {
PyErr_SetString(PyExc_ValueError,
@@ -99,7 +99,7 @@ buffer_from_object(PyObject *base, int size, int offset, int readonly)
/* another buffer, refer to the base object */
PyBufferObject *b = (PyBufferObject *)base;
if (b->b_size != Py_END_OF_BUFFER) {
- int base_size = b->b_size - offset;
+ Py_ssize_t base_size = b->b_size - offset;
if (base_size < 0)
base_size = 0;
if (size == Py_END_OF_BUFFER || size > base_size)
@@ -113,7 +113,7 @@ buffer_from_object(PyObject *base, int size, int offset, int readonly)
PyObject *
-PyBuffer_FromObject(PyObject *base, int offset, int size)
+PyBuffer_FromObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
{
PyBufferProcs *pb = base->ob_type->tp_as_buffer;
@@ -129,7 +129,7 @@ PyBuffer_FromObject(PyObject *base, int offset, int size)
}
PyObject *
-PyBuffer_FromReadWriteObject(PyObject *base, int offset, int size)
+PyBuffer_FromReadWriteObject(PyObject *base, Py_ssize_t offset, Py_ssize_t size)
{
PyBufferProcs *pb = base->ob_type->tp_as_buffer;
@@ -145,19 +145,19 @@ PyBuffer_FromReadWriteObject(PyObject *base, int offset, int size)
}
PyObject *
-PyBuffer_FromMemory(void *ptr, int size)
+PyBuffer_FromMemory(void *ptr, Py_ssize_t size)
{
return buffer_from_memory(NULL, size, 0, ptr, 1);
}
PyObject *
-PyBuffer_FromReadWriteMemory(void *ptr, int size)
+PyBuffer_FromReadWriteMemory(void *ptr, Py_ssize_t size)
{
return buffer_from_memory(NULL, size, 0, ptr, 0);
}
PyObject *
-PyBuffer_New(int size)
+PyBuffer_New(Py_ssize_t size)
{
PyObject *o;
PyBufferObject * b;
@@ -167,6 +167,7 @@ PyBuffer_New(int size)
"size must be zero or positive");
return NULL;
}
+ /* XXX: check for overflow in multiply */
/* Inline PyObject_New */
o = PyObject_MALLOC(sizeof(*b) + size);
if ( o == NULL )
@@ -189,13 +190,13 @@ static PyObject *
buffer_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
PyObject *ob;
- int offset = 0;
- int size = Py_END_OF_BUFFER;
+ Py_ssize_t offset = 0;
+ Py_ssize_t size = Py_END_OF_BUFFER;
if (!_PyArg_NoKeywords("buffer()", kw))
return NULL;
- if (!PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size))
+ if (!PyArg_ParseTuple(args, "O|ll:buffer", &ob, &offset, &size))
return NULL;
return PyBuffer_FromObject(ob, offset, size);
}
@@ -220,7 +221,8 @@ static int
buffer_compare(PyBufferObject *self, PyBufferObject *other)
{
void *p1, *p2;
- int len_self, len_other, min_len, cmp;
+ Py_ssize_t len_self, len_other, min_len;
+ int cmp;
if (!get_buf(self, &p1, &len_self))
return -1;
@@ -238,17 +240,17 @@ buffer_compare(PyBufferObject *self, PyBufferObject *other)
static PyObject *
buffer_repr(PyBufferObject *self)
{
- char *status = self->b_readonly ? "read-only" : "read-write";
+ const char *status = self->b_readonly ? "read-only" : "read-write";
if ( self->b_base == NULL )
- return PyString_FromFormat("<%s buffer ptr %p, size %d at %p>",
+ return PyString_FromFormat("<%s buffer ptr %p, size %ld at %p>",
status,
self->b_ptr,
self->b_size,
self);
else
return PyString_FromFormat(
- "<%s buffer for %p, size %d, offset %d at %p>",
+ "<%s buffer for %p, size %ld, offset %ld at %p>",
status,
self->b_base,
self->b_size,
@@ -260,8 +262,8 @@ static long
buffer_hash(PyBufferObject *self)
{
void *ptr;
- int size;
- register int len;
+ Py_ssize_t size;
+ register Py_ssize_t len;
register unsigned char *p;
register long x;
@@ -300,7 +302,7 @@ static PyObject *
buffer_str(PyBufferObject *self)
{
void *ptr;
- int size;
+ Py_ssize_t size;
if (!get_buf(self, &ptr, &size))
return NULL;
return PyString_FromStringAndSize(ptr, size);
@@ -308,11 +310,11 @@ buffer_str(PyBufferObject *self)
/* Sequence methods */
-static int
+static Py_ssize_t
buffer_length(PyBufferObject *self)
{
void *ptr;
- int size;
+ Py_ssize_t size;
if (!get_buf(self, &ptr, &size))
return -1;
return size;
@@ -325,7 +327,7 @@ buffer_concat(PyBufferObject *self, PyObject *other)
void *ptr1, *ptr2;
char *p;
PyObject *ob;
- int size, count;
+ Py_ssize_t size, count;
if ( pb == NULL ||
pb->bf_getreadbuffer == NULL ||
@@ -369,12 +371,12 @@ buffer_concat(PyBufferObject *self, PyObject *other)
}
static PyObject *
-buffer_repeat(PyBufferObject *self, int count)
+buffer_repeat(PyBufferObject *self, Py_ssize_t count)
{
PyObject *ob;
register char *p;
void *ptr;
- int size;
+ Py_ssize_t size;
if ( count < 0 )
count = 0;
@@ -398,10 +400,10 @@ buffer_repeat(PyBufferObject *self, int count)
}
static PyObject *
-buffer_item(PyBufferObject *self, int idx)
+buffer_item(PyBufferObject *self, Py_ssize_t idx)
{
void *ptr;
- int size;
+ Py_ssize_t size;
if (!get_buf(self, &ptr, &size))
return NULL;
if ( idx < 0 || idx >= size ) {
@@ -412,10 +414,10 @@ buffer_item(PyBufferObject *self, int idx)
}
static PyObject *
-buffer_slice(PyBufferObject *self, int left, int right)
+buffer_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right)
{
void *ptr;
- int size;
+ Py_ssize_t size;
if (!get_buf(self, &ptr, &size))
return NULL;
if ( left < 0 )
@@ -431,12 +433,12 @@ buffer_slice(PyBufferObject *self, int left, int right)
}
static int
-buffer_ass_item(PyBufferObject *self, int idx, PyObject *other)
+buffer_ass_item(PyBufferObject *self, Py_ssize_t idx, PyObject *other)
{
PyBufferProcs *pb;
void *ptr1, *ptr2;
- int size;
- int count;
+ Py_ssize_t size;
+ Py_ssize_t count;
if ( self->b_readonly ) {
PyErr_SetString(PyExc_TypeError,
@@ -482,13 +484,13 @@ buffer_ass_item(PyBufferObject *self, int idx, PyObject *other)
}
static int
-buffer_ass_slice(PyBufferObject *self, int left, int right, PyObject *other)
+buffer_ass_slice(PyBufferObject *self, Py_ssize_t left, Py_ssize_t right, PyObject *other)
{
PyBufferProcs *pb;
void *ptr1, *ptr2;
- int size;
- int slice_len;
- int count;
+ Py_ssize_t size;
+ Py_ssize_t slice_len;
+ Py_ssize_t count;
if ( self->b_readonly ) {
PyErr_SetString(PyExc_TypeError,
@@ -541,10 +543,10 @@ buffer_ass_slice(PyBufferObject *self, int left, int right, PyObject *other)
/* Buffer methods */
-static int
-buffer_getreadbuf(PyBufferObject *self, int idx, void **pp)
+static Py_ssize_t
+buffer_getreadbuf(PyBufferObject *self, Py_ssize_t idx, void **pp)
{
- int size;
+ Py_ssize_t size;
if ( idx != 0 ) {
PyErr_SetString(PyExc_SystemError,
"accessing non-existent buffer segment");
@@ -555,8 +557,8 @@ buffer_getreadbuf(PyBufferObject *self, int idx, void **pp)
return size;
}
-static int
-buffer_getwritebuf(PyBufferObject *self, int idx, void **pp)
+static Py_ssize_t
+buffer_getwritebuf(PyBufferObject *self, Py_ssize_t idx, void **pp)
{
if ( self->b_readonly )
{
@@ -566,11 +568,11 @@ buffer_getwritebuf(PyBufferObject *self, int idx, void **pp)
return buffer_getreadbuf(self, idx, pp);
}
-static int
-buffer_getsegcount(PyBufferObject *self, int *lenp)
+static Py_ssize_t
+buffer_getsegcount(PyBufferObject *self, Py_ssize_t *lenp)
{
void *ptr;
- int size;
+ Py_ssize_t size;
if (!get_buf(self, &ptr, &size))
return -1;
if (lenp)
@@ -578,11 +580,11 @@ buffer_getsegcount(PyBufferObject *self, int *lenp)
return 1;
}
-static int
-buffer_getcharbuf(PyBufferObject *self, int idx, const char **pp)
+static Py_ssize_t
+buffer_getcharbuf(PyBufferObject *self, Py_ssize_t idx, const char **pp)
{
void *ptr;
- int size;
+ Py_ssize_t size;
if ( idx != 0 ) {
PyErr_SetString(PyExc_SystemError,
"accessing non-existent buffer segment");
@@ -596,20 +598,20 @@ buffer_getcharbuf(PyBufferObject *self, int idx, const char **pp)
static PySequenceMethods buffer_as_sequence = {
- (inquiry)buffer_length, /*sq_length*/
+ (lenfunc)buffer_length, /*sq_length*/
(binaryfunc)buffer_concat, /*sq_concat*/
- (intargfunc)buffer_repeat, /*sq_repeat*/
- (intargfunc)buffer_item, /*sq_item*/
- (intintargfunc)buffer_slice, /*sq_slice*/
- (intobjargproc)buffer_ass_item, /*sq_ass_item*/
- (intintobjargproc)buffer_ass_slice, /*sq_ass_slice*/
+ (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*/
};
static PyBufferProcs buffer_as_buffer = {
- (getreadbufferproc)buffer_getreadbuf,
- (getwritebufferproc)buffer_getwritebuf,
- (getsegcountproc)buffer_getsegcount,
- (getcharbufferproc)buffer_getcharbuf,
+ (readbufferproc)buffer_getreadbuf,
+ (writebufferproc)buffer_getwritebuf,
+ (segcountproc)buffer_getsegcount,
+ (charbufferproc)buffer_getcharbuf,
};
PyTypeObject PyBuffer_Type = {