Relative seeks on string IO

Pierre Quentel pierre.quentel at
Wed Sep 7 09:00:24 CEST 2011

> Please post code without non-code indents, like so:
Sorry about that. After the line "Example :" I indented the next
block, out of habit ;-)
> What system are you using? Does it have a narrow or wide unicode build?
> (IE, what is the value of sys.maxunicode?)
I use Windows XP Pro, version 2002, SP3. sys.maxunicode is 65535

I have the same behaviour with 3.1.1 and with 2.7

I don't understand why variable sized code units would cause problems.
On text file objects, read(nb) reads nb characters, regardless of the
number of bytes used to encode them, and tell() returns a position in
the text stream just after the next (unicode) character read

As for SringIO, a wrapper around file objects simulates a correct
behaviour for relative seeks :

txt = "abcdef"
txt += "تخصيص هذه الطبعة"
txt += "머니투데이"
txt += "endof file"

out = open("test.txt","w",encoding="utf-8")

fobj = open("test.txt",encoding="utf-8")
except IOError:
    print('raises IOError')

class _file:

    def __init__(self,file_obj):
        self.file_obj = file_obj

    def read(self,nb=None):
        if nb is None:

    def seek(self,offset,whence=0):
        if whence==0:
            if whence==2:
                # read till EOF
                while True:
                    buf =
                    if not buf:

fobj = _file(open("test.txt",encoding="utf-8")),1),2)

- Pierre

More information about the Python-list mailing list