[Python-Dev] TextIOWrapper.tell()

Antoine Pitrou solipsis at pitrou.net
Wed Jun 30 19:20:34 CEST 2010


On Wed, 30 Jun 2010 10:03:49 -0700
Guido van Rossum <guido at python.org> wrote:
> 
> > Also, please note that values used by seek() and tell() on
> > text I/O are "opaque cookies". While they can happen to match the
> > raw binary file position, it is a mere coincidence (or an
> > implementation detail, at your will). Therefore, reusing tell() values
> > of a binary file to seek() a TextIOWrapper accessing the same file
> > is wrong.
> 
> Well, um, I actually designed it carefully so that bytes offsets
> *would* work as text offsets in those cases where they make sense at
> all.

Ah, this is embarrassing. I always assumed it was an implementation
detail since neither the PEP nor the module docs say otherwise.

PEP 3116 clearly says:

“Unlike with raw I/O, the units for .seek() are not specified - some
implementations (e.g. StringIO) use characters and others (e.g.
TextIOWrapper) use bytes.”

And also:

“.seek(pos: object, whence: int = 0) -> int

    Seek to position pos. If pos is non-zero, it must be a cookie
    returned from .tell() and whence must be zero.”

“it must be a cookie returned from .tell()” here seems to imply that
non-zero values of other origin should not be used.

Regards

Antoine.


More information about the Python-Dev mailing list