[Python-checkins] r57251 - python/branches/alex-py3k/Modules/_bytesiomodule.c python/branches/alex-py3k/Modules/_stringiomodule.c

alexandre.vassalotti python-checkins at python.org
Tue Aug 21 07:38:11 CEST 2007


Author: alexandre.vassalotti
Date: Tue Aug 21 07:38:11 2007
New Revision: 57251

Modified:
   python/branches/alex-py3k/Modules/_bytesiomodule.c
   python/branches/alex-py3k/Modules/_stringiomodule.c
Log:
Use memset(), instead of a while loop, for padding with zeroes in
seek().
Remove unnecessary optimization in seek().


Modified: python/branches/alex-py3k/Modules/_bytesiomodule.c
==============================================================================
--- python/branches/alex-py3k/Modules/_bytesiomodule.c	(original)
+++ python/branches/alex-py3k/Modules/_bytesiomodule.c	Tue Aug 21 07:38:11 2007
@@ -352,15 +352,15 @@
 static PyObject *
 bytesio_seek(BytesIOObject *self, PyObject *args)
 {
-    Py_ssize_t newpos, prevpos;
+    Py_ssize_t pos;
     int mode = 0;
 
-    if (!PyArg_ParseTuple(args, "n|i:seek", &newpos, &mode))
+    if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode))
         return NULL;
 
-    if (newpos < 0 && mode == 0) {
+    if (pos < 0 && mode == 0) {
         PyErr_Format(PyExc_ValueError,
-                     "Negative seek value %zd", newpos);
+                     "Negative seek value %zd", pos);
         return NULL;
     }
 
@@ -368,10 +368,10 @@
        mode 1: offset relative to current position.
        mode 2: offset relative the end of the string. */
     if (mode == 1) {
-        newpos += self->pos;
+        pos += self->pos;
     }
     else if (mode == 2) {
-        newpos += self->string_size;
+        pos += self->string_size;
     }
     else if (mode != 0) {
         PyErr_Format(PyExc_ValueError,
@@ -379,22 +379,19 @@
         return NULL;
     }
 
-    if (newpos < 0)
-        newpos = 0;
+    if (pos < 0)
+        pos = 0;
 
-    if (newpos >= self->string_size) {
-        if (resize_buffer(self, newpos + 1) < 0)
+    if (pos >= self->string_size) {
+        if (resize_buffer(self, pos + 1) < 0)
             return NULL;  /* out of memory */
     }
+    self->pos = pos;
 
-    prevpos = self->pos;
-    self->pos = newpos;
-
-    /* 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--;
+    /* Pad with zeros the buffer region larger than the string size. */
+    if (self->pos > self->string_size) {
+        memset(self->buf + self->string_size, '\0',
+               (self->pos - self->string_size) * sizeof(char));
     }
 
     return PyInt_FromSsize_t(self->pos);

Modified: python/branches/alex-py3k/Modules/_stringiomodule.c
==============================================================================
--- python/branches/alex-py3k/Modules/_stringiomodule.c	(original)
+++ python/branches/alex-py3k/Modules/_stringiomodule.c	Tue Aug 21 07:38:11 2007
@@ -317,10 +317,10 @@
 static PyObject *
 stringio_seek(StringIOObject *self, PyObject *args)
 {
-    Py_ssize_t newpos, prevpos;
+    Py_ssize_t pos;
     int mode = 0;
 
-    if (!PyArg_ParseTuple(args, "n|i:seek", &newpos, &mode))
+    if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode))
         return NULL;
 
     if (mode != 0 && mode != 1 && mode != 2) {
@@ -328,13 +328,13 @@
                      "Invalid whence (%i, should be 0, 1 or 2)", mode);
         return NULL;
     }
-    else if (newpos < 0 && mode == 0) {
+    else if (pos < 0 && mode == 0) {
         PyErr_Format(PyExc_ValueError,
-                     "Negative seek position %zd", newpos);
+                     "Negative seek position %zd", pos);
         return NULL;
     }
-    else if (mode != 0 && newpos != 0) {
-        PyErr_SetString(PyExc_IOError, 
+    else if (mode != 0 && pos != 0) {
+        PyErr_SetString(PyExc_IOError,
                         "Can't do nonzero cur-relative seeks");
         return NULL;
     }
@@ -343,25 +343,22 @@
        mode 1: no change to current position.
        mode 2: change position to end of file. */
     if (mode == 1) {
-        newpos = self->pos;
+        pos = self->pos;
     }
     else if (mode == 2) {
-        newpos = self->string_size;
+        pos = self->string_size;
     }
 
-    if (newpos > self->string_size) {
-        if (resize_buffer(self, newpos + 1) < 0)
+    if (pos > self->string_size) {
+        if (resize_buffer(self, pos + 1) < 0)
             return NULL;  /* out of memory */
     }
+    self->pos = pos;
 
-    prevpos = self->pos;
-    self->pos = newpos;
-
-    /* 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--;
+    /* Pad with zeros the buffer region larger than the string size. */
+    if (self->pos > self->string_size) {
+        memset(self->buf + self->string_size, '\0',
+               (self->pos - self->string_size) * sizeof(Py_UNICODE));
     }
 
     return PyInt_FromSsize_t(self->pos);


More information about the Python-checkins mailing list