Last active
January 22, 2019 15:48
-
-
Save bbayles/4ab8158ecd88a746191658ba8a40b512 to your computer and use it in GitHub Desktop.
Test for bpo21417 patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tests for bpo-21417 | |
import itertools | |
import os.path | |
import tempfile | |
import zipfile | |
# Compress a large-ish file, like the dictionary | |
file_path = '/usr/share/dict/american-english' | |
# Read its contents for later comparison | |
with open(file_path, 'rb') as infile: | |
file_contents = infile.read() | |
# Check all combinations of compression type and level | |
compression_types = [ | |
zipfile.ZIP_STORED, | |
zipfile.ZIP_DEFLATED, | |
zipfile.ZIP_BZIP2, | |
zipfile.ZIP_LZMA, | |
] | |
compression_levels = [None, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | |
it = itertools.product(compression_types, compression_levels) | |
# Create an archive with each type of compression and level, then read it | |
# back to verify the contents | |
with tempfile.TemporaryDirectory() as temp_dir: | |
for compression, compresslevel in it: | |
# Illegal combination | |
if compression == zipfile.ZIP_BZIP2 and compresslevel == 0: | |
continue | |
archive_path = os.path.join( | |
temp_dir, f'{compression}_{compresslevel}.zip' | |
) | |
kwargs = {'compression': compression, 'compresslevel': compresslevel} | |
with zipfile.ZipFile(archive_path, mode='w', **kwargs) as archive: | |
arcname = f'compressed_{compresslevel}.txt' | |
archive.write(file_path, arcname, compresslevel=compresslevel) | |
print(compression, compresslevel, os.path.getsize(archive_path), sep='\t') | |
with zipfile.ZipFile(archive_path, mode='r') as archive: | |
retrieved_contents = archive.read(arcname) | |
assert file_contents == retrieved_contents | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment