Checkin of Jack's buffer mods.
Not really checked, but didn't fail any tests either...
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index cee41a1..a4b1d4b 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -419,6 +419,41 @@
return v;
}
+static PyObject *
+file_readinto(f, args)
+ PyFileObject *f;
+ PyObject *args;
+{
+ char *ptr;
+ int ntodo, ndone, nnow;
+
+ if (f->f_fp == NULL)
+ return err_closed();
+ if (!PyArg_Parse(args, "w#", &ptr, &ntodo))
+ return NULL;
+ ndone = 0;
+ /*
+ ** XXXX Is this correct? Other threads may see partially-completed
+ ** reads if they look at the object we're reading into...
+ */
+ Py_BEGIN_ALLOW_THREADS
+ while(ntodo > 0) {
+ nnow = fread(ptr+ndone, 1, ntodo, f->f_fp);
+ if (nnow < 0 ) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ clearerr(f->f_fp);
+ return NULL;
+ }
+ if (nnow == 0)
+ break;
+ ndone += nnow;
+ ntodo -= nnow;
+ }
+ Py_END_ALLOW_THREADS
+ return PyInt_FromLong(ndone);
+}
+
+
/* Internal routine to get a line.
Size argument interpretation:
> 0: max length;
@@ -688,6 +723,7 @@
{"tell", (PyCFunction)file_tell, 0},
{"write", (PyCFunction)file_write, 0},
{"writelines", (PyCFunction)file_writelines, 0},
+ {"readinto", (PyCFunction)file_readinto, 0},
{NULL, NULL} /* sentinel */
};
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 1f95aa1..dbcb1a9 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -456,6 +456,40 @@
return x;
}
+static int
+string_buffer_getreadbuf(self, index, ptr)
+ PyStringObject *self;
+ int index;
+ const void **ptr;
+{
+ if ( index != 0 ) {
+ PyErr_SetString(PyExc_SystemError, "Accessing non-existent string segment");
+ return -1;
+ }
+ *ptr = (void *)self->ob_sval;
+ return self->ob_size;
+}
+
+static int
+string_buffer_getwritebuf(self, index, ptr)
+ PyStringObject *self;
+ int index;
+ const void **ptr;
+{
+ PyErr_SetString(PyExc_TypeError, "Cannot use string as modifyable buffer");
+ return -1;
+}
+
+static int
+string_buffer_getsegcount(self, lenp)
+ PyStringObject *self;
+ int *lenp;
+{
+ if ( lenp )
+ *lenp = self->ob_size;
+ return 1;
+}
+
static PySequenceMethods string_as_sequence = {
(inquiry)string_length, /*sq_length*/
(binaryfunc)string_concat, /*sq_concat*/
@@ -466,6 +500,12 @@
0, /*sq_ass_slice*/
};
+static PyBufferProcs string_as_buffer = {
+ (getreadbufferproc)string_buffer_getreadbuf,
+ (getwritebufferproc)string_buffer_getwritebuf,
+ (getsegcountproc)string_buffer_getsegcount,
+};
+
PyTypeObject PyString_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
@@ -486,7 +526,7 @@
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
- 0, /*tp_xxx3*/
+ &string_as_buffer, /*tp_as_buffer*/
0, /*tp_xxx4*/
0, /*tp_doc*/
};