Skip to content

Commit 037b3ee

Browse files
committed
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.
1 parent 7109b28 commit 037b3ee

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

‎Lib/test/test_StringIO.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ def test_truncate(self):
4949
f.seek(10)
5050
f.truncate()
5151
eq(f.getvalue(), 'abcdefghij')
52-
f.seek(0)
5352
f.truncate(5)
5453
eq(f.getvalue(), 'abcde')
54+
f.write('xyz')
55+
eq(f.getvalue(), 'abcdexyz')
5556
f.close()
5657
self.assertRaises(ValueError, f.write, 'frobnitz')
5758

‎Misc/NEWS‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ Core and builtins
4141
Extension modules
4242
-----------------
4343

44+
- Patch 1012740: truncate() on a writeable cStringIO now resets the
45+
position to the end of the stream. This is consistent with the original
46+
StringIO module and avoids inadvertently resurrecting data that was
47+
supposed to have been truncated away.
48+
4449
- Added socket.socketpair().
4550

4651
Library
@@ -59,7 +64,7 @@ Library
5964
- A new function tkFont.nametofont was added to return an existing
6065
font. The Font class constructor now has an additional exists argument
6166
which, if True, requests to return/configure an existing font, rather
62-
than creating a new one.
67+
than creating a new one.
6368

6469
- Updated the decimal package's min() and max() methods to match the
6570
latest revision of the General Decimal Arithmetic Specification.

‎Modules/cStringIO.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ IO_truncate(IOobject *self, PyObject *args) {
289289
if (pos < 0) pos = self->pos;
290290

291291
if (self->string_size > pos) self->string_size = pos;
292+
self->pos = self->string_size;
292293

293294
Py_INCREF(Py_None);
294295
return Py_None;

0 commit comments

Comments
 (0)