This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: cStringIO's truncate doesn't
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: tim.peters Nosy List: ddorfman, tim.peters
Priority: high Keywords: patch

Created on 2004-08-20 07:39 by ddorfman, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
csiotrunc.diff ddorfman, 2004-08-20 07:39 Fix, test case, and NEWS update
Messages (4)
msg46749 - (view) Author: Dima Dorfman (ddorfman) Date: 2004-08-20 07:39
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.
msg46750 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-08-20 15:14
Logged In: YES 
user_id=31435

Boosted priority because I think I've bumped into this too.
msg46751 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-08-21 06:11
Logged In: YES 
user_id=31435

Assigned to me.
msg46752 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-08-21 06:57
Logged In: YES 
user_id=31435

Thanks -- nice work!  Accepted and checked in:

Lib/test/test_StringIO.py 1.18
Misc/NEWS 1.1107
Modules/cStringIO.c 2.49
History
Date User Action Args
2022-04-11 14:56:06adminsetgithub: 40793
2004-08-20 07:39:47ddorfmancreate