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

alexandre.vassalotti python-checkins at python.org
Mon Jul 2 23:46:52 CEST 2007


Author: alexandre.vassalotti
Date: Mon Jul  2 23:46:52 2007
New Revision: 56154

Modified:
   python/branches/cpy_merge/Modules/_bytes_iomodule.c
   python/branches/cpy_merge/Modules/_string_iomodule.c
Log:
Fix a slight inefficiency in the seek method.


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 23:46:52 2007
@@ -342,7 +342,7 @@
 static PyObject *
 bytes_io_seek(BytesIOObject *self, PyObject *args)
 {
-    Py_ssize_t newpos;
+    Py_ssize_t newpos, prevpos;
     int mode = 0;
 
     if (self->buf == NULL)
@@ -371,12 +371,15 @@
     if (resize_buffer(self, newpos) < 0)
         return NULL;  /* out of memory */
 
+    prevpos = self->pos;
     self->pos = newpos;
 
-    /* Pad with zeros the buffer region larger than the string size.
-       XXX This is inefficient for multiple seeks. */
-    while (--newpos >= self->string_size)
+    /* Pad with zeros the buffer region larger than the string size and
+       not previously padded with zeros. */
+    while (newpos >= self->string_size && newpos >= prevpos) {
         self->buf[newpos] = 0;
+        newpos--;
+    }
 
     return PyInt_FromSsize_t(self->pos);
 }

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	Mon Jul  2 23:46:52 2007
@@ -322,7 +322,7 @@
 static PyObject *
 string_io_seek(StringIOObject *self, PyObject *args)
 {
-    Py_ssize_t newpos;
+    Py_ssize_t newpos, prevpos;
     int mode = 0;
 
     if (self->buf == NULL)
@@ -351,12 +351,15 @@
     if (resize_buffer(self, newpos) < 0)
         return NULL;  /* out of memory */
 
+    prevpos = self->pos;
     self->pos = newpos;
 
-    /* Pad with zeros the buffer region larger than the string size.
-       XXX This is inefficient for multiple seeks. */
-    while (--newpos >= self->string_size)
+    /* Pad with zeros the buffer region larger than the string size and
+       not previously padded with zeros. */
+    while (newpos >= self->string_size && newpos >= prevpos) {
         self->buf[newpos] = 0;
+        newpos--;
+    }
 
     return PyInt_FromSsize_t(self->pos);
 }


More information about the Python-checkins mailing list