[New-bugs-announce] [issue30250] StreamIO truncate behavior of current position

Albert Zeyer report at bugs.python.org
Wed May 3 05:21:26 EDT 2017


New submission from Albert Zeyer:

The doc says that StringIO.truncate should not change the current position.
Consider this code:

  try:
    import StringIO
  except ImportError:
    import io as StringIO
  buf = StringIO.StringIO()
  assert_equal(buf.getvalue(), "")
  print("buf: %r" % buf.getvalue())

  buf.write("hello")
  print("buf: %r" % buf.getvalue())
  assert_equal(buf.getvalue(), "hello")
  buf.truncate(0)
  print("buf: %r" % buf.getvalue())
  assert_equal(buf.getvalue(), "")

  buf.write("hello")
  print("buf: %r" % buf.getvalue())
  assert_equal(buf.getvalue(), "\x00\x00\x00\x00\x00hello")
  buf.truncate(0)
  print("buf: %r" % buf.getvalue())
  assert_equal(buf.getvalue(), "")


On Python 3.6, I get the output:

buf: ''
buf: 'hello'
buf: ''
buf: '\x00\x00\x00\x00\x00hello'

On Python 2.7, I get the output:

buf: ''
buf: 'hello'
buf: ''
buf: 'hello'


Thus it seems that Python 2.7 StringIO.truncate does actually resets the position for this case or there is some other bug in Python 2.7. At least from the doc, it seems that the Python 3.6 behavior is the expected behavior.

----------
components: IO
messages: 292866
nosy: Albert.Zeyer
priority: normal
severity: normal
status: open
title: StreamIO truncate behavior of current position
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30250>
_______________________________________


More information about the New-bugs-announce mailing list