mirror of
https://github.com/python/cpython.git
synced 2025-11-01 02:38:53 +00:00
gh-131492, gh-131461: handle exceptions in GzipFile constructor while owning resources (#131462)
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
f53e7de6a8
commit
ce79274e9f
4 changed files with 65 additions and 49 deletions
12
Lib/gzip.py
12
Lib/gzip.py
|
|
@ -202,6 +202,8 @@ class GzipFile(_compression.BaseStream):
|
|||
raise ValueError("Invalid mode: {!r}".format(mode))
|
||||
if mode and 'b' not in mode:
|
||||
mode += 'b'
|
||||
|
||||
try:
|
||||
if fileobj is None:
|
||||
fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
|
||||
if filename is None:
|
||||
|
|
@ -247,6 +249,11 @@ class GzipFile(_compression.BaseStream):
|
|||
|
||||
if self.mode == WRITE:
|
||||
self._write_gzip_header(compresslevel)
|
||||
except:
|
||||
# Avoid a ResourceWarning if the write fails,
|
||||
# eg read-only file or KeyboardInterrupt
|
||||
self._close()
|
||||
raise
|
||||
|
||||
@property
|
||||
def mtime(self):
|
||||
|
|
@ -387,9 +394,12 @@ class GzipFile(_compression.BaseStream):
|
|||
elif self.mode == READ:
|
||||
self._buffer.close()
|
||||
finally:
|
||||
self._close()
|
||||
|
||||
def _close(self):
|
||||
self.fileobj = None
|
||||
myfileobj = self.myfileobj
|
||||
if myfileobj:
|
||||
if myfileobj is not None:
|
||||
self.myfileobj = None
|
||||
myfileobj.close()
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ from test import archiver_tests
|
|||
from test import support
|
||||
from test.support import os_helper
|
||||
from test.support import script_helper
|
||||
from test.support import warnings_helper
|
||||
|
||||
# Check for our compression modules.
|
||||
try:
|
||||
|
|
@ -1638,8 +1639,11 @@ class WriteTest(WriteTestBase, unittest.TestCase):
|
|||
raise exctype
|
||||
|
||||
f = BadFile()
|
||||
with self.assertRaises(exctype):
|
||||
tar = tarfile.open(tmpname, self.mode, fileobj=f,
|
||||
with (
|
||||
warnings_helper.check_no_resource_warning(self),
|
||||
self.assertRaises(exctype),
|
||||
):
|
||||
tarfile.open(tmpname, self.mode, fileobj=f,
|
||||
format=tarfile.PAX_FORMAT,
|
||||
pax_headers={'non': 'empty'})
|
||||
self.assertFalse(f.closed)
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Fix :exc:`ResourceWarning` when constructing a :class:`gzip.GzipFile` in write mode with a broken file object.
|
||||
|
|
@ -0,0 +1 @@
|
|||
Fix a resource leak when constructing a :class:`gzip.GzipFile` with a filename fails, for example when passing an invalid ``compresslevel``.
|
||||
Loading…
Add table
Add a link
Reference in a new issue