[Python-checkins] r56123 - python/branches/cpy_merge/Modules/_bytes_iomodule.c python/branches/cpy_merge/Modules/_string_iomodule.c

alexandre.vassalotti python-checkins at python.org
Fri Jun 29 00:39:45 CEST 2007


Author: alexandre.vassalotti
Date: Fri Jun 29 00:39:45 2007
New Revision: 56123
Modified:
 python/branches/cpy_merge/Modules/_bytes_iomodule.c
 python/branches/cpy_merge/Modules/_string_iomodule.c
Log:
Expose the internal buffer of StringIO and BytesIO objects.
This makes subclassing these objects easier.
Modified: python/branches/cpy_merge/Modules/_bytes_iomodule.c
==============================================================================
--- python/branches/cpy_merge/Modules/_bytes_iomodule.c	(original)
+++ python/branches/cpy_merge/Modules/_bytes_iomodule.c	Fri Jun 29 00:39:45 2007
@@ -142,6 +142,36 @@
 return PyString_FromStringAndSize(self->buf, self->string_size);
 }
 
+/* Not exposed as a method of BytesIO. */
+static int
+bytes_io_setvalue(BytesIOObject *self, PyObject *value)
+{
+ if (self->buf == NULL) {
+ err_closed();
+ return -1;
+ }
+
+ self->pos = 0;
+ self->string_size = 0;
+
+ if (value == NULL)
+ return 0;
+
+ if (!PyString_Check(value)) {
+ PyErr_SetString(PyExc_TypeError, "need a string");
+ return -1;
+ }
+ if ((write_bytes(self, PyString_AsString(value),
+ PyString_Size(value))) < 0) {
+ return -1; /* out of memory */
+ }
+ /* Reset the position back to beginning-of-file, since 
+ write_bytes changed it. */
+ self->pos = 0;
+
+ return 0;
+}
+
 static PyObject *
 bytes_io_isatty(BytesIOObject *self)
 {
@@ -524,6 +554,8 @@
 static PyGetSetDef BytesIO_getsetlist[] = {
 {"closed", (getter) bytes_io_get_closed, NULL,
 "True if the file is closed."},
+ {"_buffer", (getter) bytes_io_getvalue, (setter) bytes_io_setvalue,
+ NULL},
 {0}, /* sentinel */
 };
 
Modified: python/branches/cpy_merge/Modules/_string_iomodule.c
==============================================================================
--- python/branches/cpy_merge/Modules/_string_iomodule.c	(original)
+++ python/branches/cpy_merge/Modules/_string_iomodule.c	Fri Jun 29 00:39:45 2007
@@ -141,6 +141,36 @@
 return PyUnicode_FromUnicode(self->buf, self->string_size);
 }
 
+/* Not exposed as a method of StringIO. */
+static int
+string_io_setvalue(StringIOObject *self, PyObject *value)
+{
+ if (self->buf == NULL) {
+ err_closed();
+ return -1;
+ }
+
+ self->pos = 0;
+ self->string_size = 0;
+
+ if (value == NULL)
+ return 0;
+
+ if (!PyUnicode_Check(value)) {
+ PyErr_SetString(PyExc_TypeError, "need a unicode object");
+ return -1;
+ }
+ if ((write_str(self, PyUnicode_AsUnicode(value),
+ PyUnicode_GetSize(value))) < 0) {
+ return -1; /* out of memory */
+ }
+ /* Reset the position back to beginning-of-file, since 
+ write_str changed it. */
+ self->pos = 0;
+
+ return 0;
+}
+
 static PyObject *
 string_io_isatty(StringIOObject *self)
 {
@@ -530,6 +560,8 @@
 static PyGetSetDef StringIO_getsetlist[] = {
 {"closed", (getter) string_io_get_closed, NULL,
 "True if the file is closed"},
+ {"_buffer", (getter) string_io_getvalue, (setter) string_io_setvalue,
+ NULL},
 {0}, /* sentinel */
 };
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /