summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_zipfile.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-03-30 13:52:16 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2019-03-30 13:52:16 (GMT)
commit4724ba9b57c45ce4bca3c828f2ed8dcff3800a0c (patch)
tree09480b16011fb93d4c6871a963f3e194fd89687d /Lib/test/test_zipfile.py
parent128e40f06f3ddc18e113339af464ffefd070eaef (diff)
downloadcpython-4724ba9b57c45ce4bca3c828f2ed8dcff3800a0c.zip
cpython-4724ba9b57c45ce4bca3c828f2ed8dcff3800a0c.tar.gz
cpython-4724ba9b57c45ce4bca3c828f2ed8dcff3800a0c.tar.bz2
bpo-36434: Properly handle writing errors in ZIP files. (GH-12559) (GH-12628)
Errors during writing no longer prevent to properly close the ZIP file. (cherry picked from commit 2524fdefc9bb2a97b99319190aeb23703079ad4c) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/test/test_zipfile.py')
-rw-r--r--Lib/test/test_zipfile.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index b48366a..ac8f64c 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -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)