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.

Author terry.reedy
Recipients terry.reedy
Date 2010-05-28.00:12:21
SpamBayes Score 0.003517095
Marked as misclassified No
Message-id <1275005546.82.0.0795236723796.issue8840@psf.upfronthosting.co.za>
In-reply-to
Content
Some of my tests use io.StringIO and assert that captured print output equals expected output. Until now, I reused one buffer by truncating between tests. I recently replaced 3.1.1 with 3.1.2 (WinXP) and the *second* test of each run started failing. The following minimal code shows the problem (and should suggest a new unit test):

from io import StringIO; s = StringIO(); print(repr(s.getvalue()))
print('abc', file=s); print(repr(s.getvalue()))
s.truncate(0); print(repr(s.getvalue()))
print('abc', file=s); print(repr(s.getvalue()))

prints (both command window and IDLE)

''
'abc\n'
''
'\x00\x00\x00\x00abc\n' 
# should be and previously would have been 'abc\n'

s.truncate(0) zeros the buffer and appears to set the length to 0, but a subsequent print sees the length as what it was before the truncate and appends after the zeroed characters. Ugh.

I presume the problem is StringIO-emulation specific but have not tested 'real' files to be sure.

---
also...

>>> help(s.truncate)
Help on built-in function truncate:

truncate(...)
    Truncate size to pos.
...

should be, for greater clarity, something like

truncate([pos])
    Truncate the size of the file or buffer to pos
...
History
Date User Action Args
2010-05-28 00:12:27terry.reedysetrecipients: + terry.reedy
2010-05-28 00:12:26terry.reedysetmessageid: <1275005546.82.0.0795236723796.issue8840@psf.upfronthosting.co.za>
2010-05-28 00:12:24terry.reedylinkissue8840 messages
2010-05-28 00:12:22terry.reedycreate