Skip to content

FileIO.seekable() is incorrect if determined by an invalid seek call #159677

Description

@KowalskiThomas

Bug report

While working on concurrency fixes for FileIO, I discovered an edge case (quite rare...) that makes seekable incorrect if determined by a call to seek that results in an error.

Reproducer

f = io.FileIO(tempfile.mktemp(), 'w+')
f.seek(-1)       # OSError: [Errno 22] Invalid argument
f.seekable()     # False but it should be true

The problem comes from this part of the implementation for seek, which sets seekable regardless of what the problem was (which could be that the file indeed is not seekable, or that in our example the passed offset was not valid).

cpython/Modules/_io/fileio.c

Lines 1016 to 1018 in b00a140

if (self->seekable < 0) {
self->seekable = (res >= 0);
}

This PR should fix it: #159678

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. changed the title [-]`FileIO`'s `seekable` is incorrect if determined by an invalid `seek` call[/-] [+]`FileIO.seekable()` is incorrect if determined by an invalid `seek` call[/+] on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions