Skip to content

GzipFile leaves GzipFile.myfileobject open in constructor if exception is raised #131492

Closed
@graingert

Description

@graingert

Bug report

Bug description:

consider the following program, it should raise an ExceptionGroup(..., [ValueError(), ValueError(), ...] but actually it raises OSError: [Errno 24] Too many open files: '/tmp/tmpk8gaprry'

import gzip
import tempfile
import pathlib
import os

def main():
    with tempfile.TemporaryDirectory() as tmp_dir:
        tmp_path = pathlib.Path(tmp_dir)
        zip_path = tmp_path / "some_file.zip"

        exceptions = []
        for i in range(2000):
            try:
                gzip.GzipFile(filename=os.fsdecode(zip_path), mode="w", compresslevel=99)
            except ValueError as e:
                exceptions.append(e)
        if exceptions:
            raise ExceptionGroup("multiple errors creating GzipFiles", exceptions)

main()

CPython versions tested on:

CPython main branch, 3.14, 3.13, 3.12, 3.11

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions