New submission from Terry J. Reedy <[email protected]>:
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
...
----------
components: IO, Library (Lib)
messages: 106630
nosy: tjreedy
priority: normal
severity: normal
stage: needs patch
status: open
title: io.StringIO: truncate+print disabled in 3.1.2
type: behavior
versions: Python 3.1
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue8840>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com