Issue 1012740: cStringIO's truncate doesn't (original) (raw)

truncate on a writeable cStringIO doesn't reset the cursor, and when another write occurs, the old data magically comes back:

io = cStringIO.StringIO() io.write('abc') io.truncate(0) io.write('xyz') io.getvalue() 'abcxyz'

Using the original StringIO module, the latter value is 'xyz', as expected. As far as I can tell, this can't be used to crash the interpreter because cStringIO never shrinks its buffer, so the old position is still in bounds. (It probably should shrink its buffer, but that can be fixed later, if desired, since it would not change the perceived behavior.)

Even though this introduces a user-visible change, it should be safe to fix this for 2.4. Anyone that relies on this wacky behavior already has it coming, and we're not even in beta yet.

The patch includes a test case that used to succeed with StringIO but fail with cStringIO. An update to NEWS is also included since this does, after all, introduce a visible change.