Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Akuli opened this issue May 22, 2022 · 1 comment
Open

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

Akuli opened this issue May 22, 2022 · 1 comment
Labels
topic: io I/O related issues

Comments

@Akuli
Copy link
Collaborator

Akuli commented May 22, 2022

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.

@srittau
Copy link
Collaborator

srittau commented May 27, 2022

We could experiment with NewType here and see what primer says.

@AlexWaygood AlexWaygood added the topic: io I/O related issues label Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: io I/O related issues
3 participants