Skip to content

ZstdFile raises AttributeError instead of io.UnsupportedOperation when iterating over a non-readable file #153037

Closed

Description

@lkk7

Bug report

Bug description:

All other compression modules raise io.UnsupportedOperation when iterating over a non-readable file:

next(iter(lzma.LZMAFile(io.BytesIO(), "w")))   # io.UnsupportedOperation: File not open for reading
next(iter(bz2.BZ2File(io.BytesIO(), "w")))     # io.UnsupportedOperation: ...
next(iter(gzip.GzipFile(fileobj=io.BytesIO(), mode="w")))  # ...

But ZstdFile.__next__ doesn't call _check_can_read(), so it dereferences self._buffer, which is None in write mode:

import io
from compression.zstd import ZstdFile

next(iter(ZstdFile(io.BytesIO(), "w")))

This results in:

AttributeError: 'NoneType' object has no attribute 'readline'

CPython versions tested on:

3.14, 3.15, CPython main branch

Operating systems tested on:

macOS, but it does not depend on OS

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Jul 4, 2026
  2. lkk7 commented on Jul 4, 2026

    @lkk7
    ContributorAuthor

    I have a patch ready and working, I believe, so I'll push that soon.

  3. lkk7 commented on Jul 4, 2026

    @lkk7
    ContributorAuthor

    @emmatyping tagging you because I see you're the zstd expert and I'd be very happy to submit a patch if you agree this should be fixed.

  4. emmatyping commented on Jul 4, 2026

    @emmatyping
    Member

    I agree consistency here would be good. Thank you for working on a patch @lkk7!

  5. added a commit that references this issue on Jul 4, 2026
  6. added
    stdlibStandard Library Python modules in the Lib/ directory
    and removed on Jul 4, 2026
  7. added 3 commits that reference this issue on Jul 13, 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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions