[Python-3000] StringIO/BytesIO in io.py doesn't over-seek properly

Guido van Rossum guido at python.org
Sat Jun 23 19:52:19 CEST 2007


On 6/23/07, Alexandre Vassalotti <alexandre at peadrop.com> wrote:
> Hello,
>
> I think found a bug in the implementation of StringIO/BytesIO in the
> new io module.  I would like to fix it, but I am not sure what should
> be the correct behavior. Any hint on this?

BytesIO should behave the way Unix files work: just seeking only sets
the read/write position, but writing inserts null bytes between the
existing end of the file and the new write position. (Writing zero
bytes doesn't count; I've just experimentally verified this.)

I think however that for StringIO this should not be allowed -- seek()
on StringIO is only allowed to accept cookies returned by tell() on
the same file object.

> And one more thing, the close method on StringIO/BytesIO objects
> doesn't work.  I will try to fix that too.

What do you want it to do? I'm thinking perhaps it doesn't need to do anything.

--Guido

> Thanks,
> -- Alexandre
>
> Python 3.0x (py3k-struni:56080M, Jun 22 2007, 17:18:04)
> [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import io
> >>> s1 = io.StringIO()
> >>> s1.seek(10)
> 10
> >>> s1.write('hello')
> 5
> >>> s1.getvalue()
> 'hello'
> >>> s1.seek(0)
> 0
> >>> s1.write('abc')
> 3
> >>> s1.getvalue()
> 'abclo'
> >>> import StringIO
> >>> s2 = StringIO.StringIO()
> >>> s2.seek(10)
> >>> s2.write('hello')
> >>> s2.getvalue()
> '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00hello'
> >>> s2.seek(0)
> >>> s2.write('abc')
> >>> s2.getvalue()
> 'abc\x00\x00\x00\x00\x00\x00\x00hello'
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/guido%40python.org
>


-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list