]> granicus.if.org Git - python/commitdiff
Patch 1012740: cStringIO's truncate doesn't
authorTim Peters <tim.peters@gmail.com>
Sat, 21 Aug 2004 06:55:43 +0000 (06:55 +0000)
committerTim Peters <tim.peters@gmail.com>
Sat, 21 Aug 2004 06:55:43 +0000 (06:55 +0000)
truncate() left the stream position unchanged, which meant the
"truncated" data didn't go away:

>>> io.write('abc')
>>> io.truncate(0)
>>> io.write('xyz')
>>> io.getvalue()
'abcxyz'

Patch by Dima Dorfman.

Lib/test/test_StringIO.py
Misc/NEWS
Modules/cStringIO.c

index c318eaafc79c0bcc545f7c27071e7b48757a35b7..6842c5ef88dc25d165a5c3fe84b08827b1a80327 100644 (file)
@@ -49,9 +49,10 @@ class TestGenericStringIO(unittest.TestCase):
         f.seek(10)
         f.truncate()
         eq(f.getvalue(), 'abcdefghij')
-        f.seek(0)
         f.truncate(5)
         eq(f.getvalue(), 'abcde')
+        f.write('xyz')
+        eq(f.getvalue(), 'abcdexyz')
         f.close()
         self.assertRaises(ValueError, f.write, 'frobnitz')
 
index a8aee9c32095290c76ce780446f8a7c37b097428..6b1dcff26e1462ac9bd18ac737e2b2795a2af339 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -41,6 +41,11 @@ Core and builtins
 Extension modules
 -----------------
 
+- Patch 1012740:  truncate() on a writeable cStringIO now resets the
+  position to the end of the stream.  This is consistent with the original
+  StringIO module and avoids inadvertently resurrecting data that was
+  supposed to have been truncated away.
+
 - Added socket.socketpair().
 
 Library
@@ -59,7 +64,7 @@ Library
 - A new function tkFont.nametofont was added to return an existing
   font. The Font class constructor now has an additional exists argument
   which, if True, requests to return/configure an existing font, rather
-  than creating a new one. 
+  than creating a new one.
 
 - Updated the decimal package's min() and max() methods to match the
   latest revision of the General Decimal Arithmetic Specification.
index 7e75879215a053cfb153f19a08ea6cc418b2786e..b7333fdd380d458df262c7d852b358e5bff1d2ec 100644 (file)
@@ -289,6 +289,7 @@ IO_truncate(IOobject *self, PyObject *args) {
         if (pos < 0) pos = self->pos;
 
         if (self->string_size > pos) self->string_size = pos;
+        self->pos = self->string_size;
 
         Py_INCREF(Py_None);
         return Py_None;