On 5/26/22, Steven D'Aprano <[email protected]> wrote:
>
> If you seek() to position 4, say, the results will be unpredictable but
> probably not anything good.
>
> In other words, the tell() and seek() cookies represent file positions
> in **bytes**, even though we are reading or writing a text file.
To clarify the general context, text I/O tell() and seek() cookies
aren't necessarily just a byte offset. They can be packed integers
that include a start position, decoder flags, a number of bytes to be
fed into the decoder, whether the decode operation should be final
(EOF), and the number of decoded characters (ordinals) to skip. For
example:
>>> open('spam.txt', 'w', encoding='utf-7').write('\u0100'*4)
4
>>> f = open('spam.txt', encoding='utf-7')
>>> f.read(2)
'ĀĀ'
>>> f.tell()
680564734871843039612185603579607777280
>>> start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip = (
... _pyio.TextIOWrapper._unpack_cookie(..., f.tell()))
>>> start_pos, dec_flags, bytes_to_feed, need_eof, chars_to_skip
(0, 55834574848, 2, False, 0)
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/NLU47DADXVIPBJLGP4IPLPKYBWH7DN7F/
Code of Conduct: http://python.org/psf/codeofconduct/