@@ -4916,24 +4916,29 @@ class ZstdBoundedDecompressTests(AbstractBoundedDecompressTests,
49164916 compression = zipfile .ZIP_ZSTANDARD
49174917
49184918
4919- class ThirdPartyDecompressorTests (unittest .TestCase ):
4920- # A decompressor installed by replacing _get_decompressor() may support
4921- # neither decompress(data, max_length) nor needs_input. ZipExtFile must
4922- # still read through it (unbounded, as before bounded decompression).
4919+ class MonkeypatchedDecompressorTests (unittest .TestCase ):
4920+ # Some third-party projects monkey-patch _get_decompressor() to add
4921+ # additional compression schemes. This can break at any time as the
4922+ # internal compressor objects change.
4923+ # To protect users, we try to keep this case working (until it becomes
4924+ # too big of a burden, or we make the API public).
4925+ # See also: GH-156002 and GH-113756.
49234926 COMPRESSION = 99
49244927
49254928 class Compressor :
4929+ """Compressor with only the original BZ2Compressor API"""
49264930 def compress (self , data ):
4927- return data
4931+ return data . swapcase ()
49284932
49294933 def flush (self ):
49304934 return b''
49314935
49324936 class Decompressor :
4937+ """Decmpressor with only the 3.3+ BZ2Decompressor API"""
49334938 eof = False
49344939
49354940 def decompress (self , data ):
4936- return data
4941+ return data . swapcase ()
49374942
49384943 def setUp (self ):
49394944 orig_check_compression = zipfile ._check_compression
@@ -4961,11 +4966,12 @@ def get_decompressor(compress_type):
49614966 self .enterContext (mock .patch .object (
49624967 zipfile , '_get_decompressor' , get_decompressor ))
49634968
4964- def test_read_through_third_party_decompressor (self ):
4965- data = bytes (range (256 )) * 256
4969+ def test_roundtrip_monkeypatched_decompressor (self ):
4970+ data = bytes (range (256 )) * 8
49664971 buf = io .BytesIO ()
49674972 with zipfile .ZipFile (buf , "w" , compression = self .COMPRESSION ) as zf :
49684973 zf .writestr ("member" , data )
4974+ self .assertIn (data .swapcase (), buf .getvalue ())
49694975 with zipfile .ZipFile (io .BytesIO (buf .getvalue ())) as zf :
49704976 self .assertEqual (zf .read ("member" ), data )
49714977 with zf .open ("member" ) as f :
0 commit comments