From 037b3ee44e7de00b4653d73d4808c0f679a909a7 Mon Sep 17 00:00:00 2001
From: Tim Peters <tim.peters@gmail.com>
Date: Sat, 21 Aug 2004 06:55:43 +0000
Subject: Patch 1012740:  cStringIO's truncate doesn't

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 | 3 ++-
 Misc/NEWS                 | 7 ++++++-
 Modules/cStringIO.c       | 1 +
 3 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/Lib/test/test_StringIO.py b/Lib/test/test_StringIO.py
index c318eaa..6842c5e 100644
--- a/Lib/test/test_StringIO.py
+++ b/Lib/test/test_StringIO.py
@@ -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')
 
diff --git a/Misc/NEWS b/Misc/NEWS
index a8aee9c..6b1dcff 100644
--- 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.
diff --git a/Modules/cStringIO.c b/Modules/cStringIO.c
index 7e75879..b7333fd 100644
--- a/Modules/cStringIO.c
+++ b/Modules/cStringIO.c
@@ -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;
-- 
cgit v0.12