Message106630
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
... |
|
Date |
User |
Action |
Args |
2010-05-28 00:12:27 | terry.reedy | set | recipients:
+ terry.reedy |
2010-05-28 00:12:26 | terry.reedy | set | messageid: <1275005546.82.0.0795236723796.issue8840@psf.upfronthosting.co.za> |
2010-05-28 00:12:24 | terry.reedy | link | issue8840 messages |
2010-05-28 00:12:22 | terry.reedy | create | |
|