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 <rep...@bugs.python.org> <http://bugs.python.org/issue30250> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com