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.

classification
Title: StringIO module truncate behavior of current position
Type: Stage: resolved
Components: Documentation, IO Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Albert.Zeyer, ZackerySpytz, docs@python, josh.r
Priority: normal Keywords:

Created on 2017-05-03 09:21 by Albert.Zeyer, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg292866 - (view) Author: Albert Zeyer (Albert.Zeyer) * Date: 2017-05-03 09:21
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.
msg292933 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2017-05-03 19:02
For the record, the StringIO module is Python 2 only (replaced by io.StringIO and io.BytesIO for Py3, which are also available on 2.6 and higher if you need consistency), and only documents truncate by reference to the generic docs for the built-in file object. Admittedly, the built-in file object docs say the position shouldn't change on truncate, but given:

1. StringIO is deprecated and no longer maintained
2. Code using it may rely on this incorrect behavior

I can't see fixing this beyond possibly mentioning the quirk in the 2.x StringIO docs. Since io.StringIO is correct, there is nothing to fix in 3.x.
msg368479 - (view) Author: Zackery Spytz (ZackerySpytz) * (Python triager) Date: 2020-05-08 22:50
Python 2 is EOL.
History
Date User Action Args
2022-04-11 14:58:46adminsetgithub: 74436
2020-05-08 23:00:51zach.waresetstatus: open -> closed
resolution: out of date
stage: resolved
2020-05-08 22:50:17ZackerySpytzsetnosy: + ZackerySpytz
messages: + msg368479
2018-10-13 12:11:14martin.pantersetassignee: docs@python

nosy: + docs@python
components: + Documentation
title: StreamIO truncate behavior of current position -> StringIO module truncate behavior of current position
2017-05-03 19:02:47josh.rsetnosy: + josh.r
messages: + msg292933
2017-05-03 09:21:26Albert.Zeyercreate