Skip to content

Commit 8bf1895

Browse files
committed
pythongh-130806: emit ResourceWarning if GzipFile unclosed
This may indicate accidental data loss. Ways to make sure all data is written: 1. Use the [file-like object](https://docs.python.org/3/glossary.html#term-file-object) as a [“With Statement Context Manager”](https://docs.python.org/3/reference/datamodel.html#context-managers). - All objects which [inherit](https://docs.python.org/3/tutorial/classes.html#inheritance) from [IOBase](https://docs.python.org/3/library/io.html#io.IOBase) support this. - [`BufferedIOBase`](https://docs.python.org/3/library/io.html#io.BufferedIOBase), [`BufferedWriter`](https://docs.python.org/3/library/io.html#io.BufferedWriter), and [`GzipFile`](https://docs.python.org/3/library/gzip.html#gzip.GzipFile) all support this. - Ensures `.close()` is called in both exception and regular cases. 1. Ensure [`.close()`](https://docs.python.org/3/library/io.html#io.IOBase.close) is always called which flushes data before closing. 1. If the underlying stream need to be kept open, use [`.detach()`](https://docs.python.org/3/library/io.html#io.BufferedIOBase.detach) Since 3.12 flushing has been necessary in GzipFile (see pythongh-105808 which was a release blocker), this makes that more visible. Users have been encountering as they upgrade to 3.12 (ex. pythongh-129726). There are a number of cases of unclosed file-like objects being deleted in CPython libraries and the test suite. This issue includes resolving those cases where the new ResourceWarning is emitted.
1 parent e53d105 commit 8bf1895

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

Lib/gzip.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ def __init__(self, filename=None, mode=None,
193193
194194
"""
195195

196+
# Ensure attributes exist at __del__
197+
self.mode = None
198+
self.fileobj = None
199+
self._buffer = None
200+
196201
if mode and ('t' in mode or 'U' in mode):
197202
raise ValueError("Invalid mode: {!r}".format(mode))
198203
if mode and 'b' not in mode:
@@ -358,7 +363,7 @@ def closed(self):
358363

359364
def close(self):
360365
fileobj = self.fileobj
361-
if fileobj is None or self._buffer.closed:
366+
if fileobj is None or self._buffer is None or self._buffer.closed:
362367
return
363368
try:
364369
if self.mode == WRITE:
@@ -435,6 +440,13 @@ def readline(self, size=-1):
435440
self._check_not_closed()
436441
return self._buffer.readline(size)
437442

443+
def __del__(self):
444+
if self.mode == WRITE and not self.closed:
445+
import warnings
446+
warnings.warn("unclosed GzipFile",
447+
ResourceWarning, source=self, stacklevel=2)
448+
449+
return super().__del__()
438450

439451
def _read_exact(fp, n):
440452
'''Read exactly *n* bytes from `fp`

Lib/test/test_gzip.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import struct
1010
import sys
1111
import unittest
12+
import warnings
1213
from subprocess import PIPE, Popen
1314
from test.support import catch_unraisable_exception
1415
from test.support import import_helper
@@ -867,9 +868,10 @@ def test_refloop_unraisable(self):
867868
# fileobj would be closed before the GzipFile as the result of a
868869
# reference loop. See issue gh-129726
869870
with catch_unraisable_exception() as cm:
870-
gzip.GzipFile(fileobj=io.BytesIO(), mode="w")
871-
gc.collect()
872-
self.assertIsNone(cm.unraisable)
871+
with self.assertWarns(ResourceWarning):
872+
gzip.GzipFile(fileobj=io.BytesIO(), mode="w")
873+
gc.collect()
874+
self.assertIsNone(cm.unraisable)
873875

874876

875877
class TestOpen(BaseTest):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deleting :class:`gzip.GzipFile` before it is closed now emits a
2+
:exc:`ResourceWarning`.

0 commit comments

Comments
 (0)