mirror of
https://github.com/python/cpython.git
synced 2025-10-21 14:12:27 +00:00
bpo-36434: Properly handle writing errors in ZIP files. (GH-12559)
Errors during writing no longer prevent to properly close the ZIP file.
This commit is contained in:
parent
7a465cb5ee
commit
2524fdefc9
3 changed files with 78 additions and 37 deletions
|
@ -402,6 +402,43 @@ class AbstractTestsWithSourceFile:
|
|||
self.assertEqual(one_info._compresslevel, 1)
|
||||
self.assertEqual(nine_info._compresslevel, 9)
|
||||
|
||||
def test_writing_errors(self):
|
||||
class BrokenFile(io.BytesIO):
|
||||
def write(self, data):
|
||||
nonlocal count
|
||||
if count is not None:
|
||||
if count == stop:
|
||||
raise OSError
|
||||
count += 1
|
||||
super().write(data)
|
||||
|
||||
stop = 0
|
||||
while True:
|
||||
testfile = BrokenFile()
|
||||
count = None
|
||||
with zipfile.ZipFile(testfile, 'w', self.compression) as zipfp:
|
||||
with zipfp.open('file1', 'w') as f:
|
||||
f.write(b'data1')
|
||||
count = 0
|
||||
try:
|
||||
with zipfp.open('file2', 'w') as f:
|
||||
f.write(b'data2')
|
||||
except OSError:
|
||||
stop += 1
|
||||
else:
|
||||
break
|
||||
finally:
|
||||
count = None
|
||||
with zipfile.ZipFile(io.BytesIO(testfile.getvalue())) as zipfp:
|
||||
self.assertEqual(zipfp.namelist(), ['file1'])
|
||||
self.assertEqual(zipfp.read('file1'), b'data1')
|
||||
|
||||
with zipfile.ZipFile(io.BytesIO(testfile.getvalue())) as zipfp:
|
||||
self.assertEqual(zipfp.namelist(), ['file1', 'file2'])
|
||||
self.assertEqual(zipfp.read('file1'), b'data1')
|
||||
self.assertEqual(zipfp.read('file2'), b'data2')
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
unlink(TESTFN)
|
||||
unlink(TESTFN2)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue