Skip to content

Opaque type for io.TextIOWrapper seek and tell? #7921

Open
@Akuli

Description

@Akuli

The following prints 18446744073709551620 (as I discovered in python/cpython#93077):

import io
stream = io.TextIOWrapper(io.BytesIO(b'"")\r\nx\r\n'))
stream.read(1)
stream.read(1)
stream.read(1)
print('-----> tell:', stream.tell())

This is not a bug. The documentation says that .tell() returns "an opaque number" that "does not usually represent a number of bytes in the underlying binary storage".

In fact, calling .seek() with anything else than 0 or a return value of .tell() "produces undefined behaviour". I just relied on that today, without knowing it. I wrote this code:

def _upgrade_high_scores_file(file: IO[str], old_version: int) -> None:
    ...
    file.seek(len(b"catris high scores file v"))
    ...

This shouldn't pass the type check IMO.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: ioI/O related issues

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions