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 Albert.Zeyer
Recipients Albert.Zeyer
Date 2017-05-03.09:21:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1493803286.93.0.729183089652.issue30250@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2017-05-03 09:21:27Albert.Zeyersetrecipients: + Albert.Zeyer
2017-05-03 09:21:26Albert.Zeyersetmessageid: <1493803286.93.0.729183089652.issue30250@psf.upfronthosting.co.za>
2017-05-03 09:21:26Albert.Zeyerlinkissue30250 messages
2017-05-03 09:21:26Albert.Zeyercreate