@@ -1065,20 +1065,37 @@ class LzmaDetectReadTest(LzmaTest, DetectReadTest):
10651065class ZstdDetectReadTest (ZstdTest , DetectReadTest ):
10661066 pass
10671067
1068+
10681069@support .requires_zstd ()
10691070class 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
10831100class GzipBrokenHeaderCorrectException (GzipTest , unittest .TestCase ):
10841101 """
0 commit comments