Skip to content

Commit 9035f78

Browse files
committed
modify test case
1 parent 034ded1 commit 9035f78

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

Lib/test/test_tarfile.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,20 +1065,37 @@ class LzmaDetectReadTest(LzmaTest, DetectReadTest):
10651065
class ZstdDetectReadTest(ZstdTest, DetectReadTest):
10661066
pass
10671067

1068+
10681069
@support.requires_zstd()
10691070
class ZstdOpenTest(unittest.TestCase):
10701071
"""
10711072
See: https://github.com/python/cpython/issues/150077
10721073
"""
10731074
def test_zstopen_closes_fileobj_on_base_exception(self):
1074-
fileobj = unittest.mock.Mock()
1075-
with unittest.mock.patch("compression.zstd.ZstdFile",
1076-
return_value=fileobj), \
1077-
unittest.mock.patch.object(tarfile.TarFile, "taropen",
1078-
side_effect=KeyboardInterrupt):
1079-
with self.assertRaises(KeyboardInterrupt):
1080-
tarfile.TarFile.zstopen("foo.tar.zst")
1081-
fileobj.close.assert_called_once()
1075+
path = os_helper.TESTFN + ".tar.zst"
1076+
self.addCleanup(os_helper.unlink, path)
1077+
with tarfile.open(path, "w:zst"):
1078+
pass
1079+
1080+
opened = []
1081+
real_ZstdFile = compression.zstd.ZstdFile
1082+
1083+
def tracking_ZstdFile(*args, **kwargs):
1084+
fileobj = real_ZstdFile(*args, **kwargs)
1085+
opened.append(fileobj)
1086+
return fileobj
1087+
1088+
with (
1089+
unittest.mock.patch("compression.zstd.ZstdFile", tracking_ZstdFile),
1090+
unittest.mock.patch.object(
1091+
tarfile.TarFile, "taropen", side_effect=KeyboardInterrupt),
1092+
self.assertRaises(KeyboardInterrupt),
1093+
):
1094+
tarfile.TarFile.zstopen(path)
1095+
1096+
self.assertEqual(len(opened), 1)
1097+
self.assertTrue(opened[0].closed)
1098+
10821099

10831100
class GzipBrokenHeaderCorrectException(GzipTest, unittest.TestCase):
10841101
"""
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
Fix ``tarfile.TarFile.zstopen`` to close the underlying zstd file object
22
when opening the tar archive is interrupted by a :exc:`BaseException`
33
subclass such as :exc:`KeyboardInterrupt`.
4-

0 commit comments

Comments
 (0)