Skip to content

GzipFile.seek() returns a stale position when writing buffered padding #160071

Description

@repzakske

Bug report

gzip.GzipFile.seek() returns a stale position when a forward seek in write mode adds padding that remains in the write buffer. The file position itself is correct.

import gzip
import io

data = io.BytesIO()
with gzip.GzipFile(fileobj=data, mode='wb') as stream:
    stream.write(b'header')
    print(stream.seek(16))
    print(stream.tell())

Actual output:

6
16

Expected output:

16
16

The same problem occurs with SEEK_CUR. Code that records the value returned by seek() as a record offset can consequently build an incorrect index, even though the compressed file is valid.

GzipFile implements the io.BufferedIOBase interface, whose seek() returns the new absolute position. The padding is written through the buffered writer, but seek() returns self.offset, which only includes data already passed to the compressor. Returning the computed target offset avoids an extra flush.

Reproduced on Windows with Python 3.12.10, 3.13.16, 3.14.8, 3.15.0, and a debug build of CPython main at 796b26b360cd1955d1268f22d91081fa1a15afb7.

Related: #108111 fixed the position used before writing padding; this report concerns the return value after adding padding.

CPython versions tested on:

3.12, 3.13, 3.14, 3.15, CPython main branch

Operating systems tested on:

Windows

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 21, 2026
  2. added 2 commits that reference this issue on Oct 21, 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

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions