Skip to content

ResourceWarning in GzipFile (write mode) if constructor raises (3.14 only) #131461

Closed
@graingert

Description

@graingert

Bug report

Bug description:

import io
import gzip

class BadFile(io.BytesIO):
    first = False
    def write(self, data):
        if self.first:
            self.first = False
            raise OSError

def main():
    try:
        gzip.GzipFile(fileobj=BadFile(), mode="w")
    except OSError:
        pass

main()

when run produces:

./python -W error ../../demo.py
Exception ignored while calling deallocator <function GzipFile.__del__ at 0x7b14defe09e0>:
Traceback (most recent call last):
  File "/home/graingert/projects/cpython/Lib/gzip.py", line 458, in __del__
    warnings.warn("unclosed GzipFile",
ResourceWarning: unclosed GzipFile

This warning is also raised by the test suite, in test_tarfile.WriteTestBase.test_open_nonwritable_fileobj

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions