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

alexandre.vassalotti python-checkins at python.org
Mon Aug 20 18:41:44 CEST 2007


Author: alexandre.vassalotti
Date: Mon Aug 20 18:41:43 2007
New Revision: 57218

Modified:
   python/branches/alex-py3k/Modules/_bytesiomodule.c
   python/branches/alex-py3k/Modules/_stringiomodule.c
Log:
Resize buffer after major truncations.


Modified: python/branches/alex-py3k/Modules/_bytesiomodule.c
==============================================================================
--- python/branches/alex-py3k/Modules/_bytesiomodule.c	(original)
+++ python/branches/alex-py3k/Modules/_bytesiomodule.c	Mon Aug 20 18:41:43 2007
@@ -52,7 +52,11 @@
 {
     Py_ssize_t alloc = self->buf_size;
 
-    if (size < alloc) {
+    if (size < alloc / 2) {
+        /* Major downsize; resize down to exact size */
+        alloc = size + 1;
+    }
+    else if (size < alloc) {
         /* Within allocated size; quick exit */
         return 0;
     }
@@ -320,8 +324,11 @@
         return NULL;
     }
 
-    if (self->string_size > size)
+    if (size < self->string_size) {
         self->string_size = size;
+        if (resize_buffer(self, size) < 0)
+            return NULL;
+    }
     self->pos = self->string_size;
 
     return PyInt_FromSsize_t(self->string_size);

Modified: python/branches/alex-py3k/Modules/_stringiomodule.c
==============================================================================
--- python/branches/alex-py3k/Modules/_stringiomodule.c	(original)
+++ python/branches/alex-py3k/Modules/_stringiomodule.c	Mon Aug 20 18:41:43 2007
@@ -51,7 +51,11 @@
 {
     Py_ssize_t alloc = self->buf_size;
 
-    if (size < alloc) {
+    if (size < alloc / 2) {
+        /* Major downsize; resize down to exact size */
+        alloc = size + 1;
+    }
+    else if (size < alloc) {
         /* Within allocated size; quick exit */
         return 0;
     }
@@ -285,8 +289,11 @@
         return NULL;
     }
 
-    if (self->string_size > size)
+    if (size < self->string_size) {
         self->string_size = size;
+        if (resize_buffer(self, size) < 0)
+            return NULL;
+    }
     self->pos = self->string_size;
 
     return PyInt_FromSsize_t(self->string_size);


More information about the Python-checkins mailing list