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

alexandre.vassalotti python-checkins at python.org
Mon Jul 2 20:13:30 CEST 2007


Author: alexandre.vassalotti
Date: Mon Jul  2 20:13:29 2007
New Revision: 56148

Modified:
   python/branches/cpy_merge/Modules/_bytes_iomodule.c
Log:
Use the buffer protocol API for writelines and setvalue.



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	Mon Jul  2 20:13:29 2007
@@ -146,6 +146,9 @@
 static int
 bytes_io_setvalue(BytesIOObject *self, PyObject *value)
 {
+    const char *bytes;
+    Py_ssize_t len;
+
     if (self->buf == NULL) {
         err_closed();
         return -1;
@@ -156,13 +159,11 @@
 
     if (value == NULL)
         return 0;
-
-    if (!PyString_Check(value)) {
-        PyErr_SetString(PyExc_TypeError, "need a string");
+    
+    if (PyObject_AsCharBuffer(value, &bytes, &len) == -1)
         return -1;
-    }
-    if ((write_bytes(self, PyString_AsString(value),
-                     PyString_Size(value))) < 0) {
+
+    if (write_bytes(self, bytes, len) < 0) {
         return -1;  /* out of memory */
     }
     /* Reset the position back to beginning-of-file, since 
@@ -385,6 +386,8 @@
 bytes_io_writelines(BytesIOObject *self, PyObject *v)
 {
     PyObject *it, *item;
+    const char *bytes;
+    Py_ssize_t len;
 
     if (self->buf == NULL)
         return err_closed();
@@ -394,16 +397,14 @@
         return NULL;
 
     while ((item = PyIter_Next(it)) != NULL) {
-        Py_ssize_t n;
-        char *c;
-        if (PyString_AsStringAndSize(item, &c, &n) == -1) {
+        if (PyObject_AsCharBuffer(item, &bytes, &len) == -1) {
             Py_DECREF(it);
             Py_DECREF(item);
             return NULL;
         }
         Py_DECREF(item);
 
-        if (write_bytes(self, c, n) == -1) {
+        if (write_bytes(self, bytes, len) == -1) {
             Py_DECREF(it);
             return NULL;
         }


More information about the Python-checkins mailing list