Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update test_zlib from CPython 3.10.6
  • Loading branch information
CPython Developers authored and youknowone committed Aug 14, 2022
commit ead12b5611bf466bd079b8c45794c0683d33e5ff
30 changes: 11 additions & 19 deletions Lib/test/test_zlib.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import unittest
from test import support
from test.support import import_helper
import binascii
import copy
import pickle
import random
import sys
from test.support import bigmemtest, _1G, _4G, import_helper
from test.support import bigmemtest, _1G, _4G


zlib = import_helper.import_module('zlib')

Expand Down Expand Up @@ -129,15 +131,20 @@ def test_overflow(self):
with self.assertRaisesRegex(OverflowError, 'int too large'):
zlib.decompressobj().flush(sys.maxsize + 1)

@support.cpython_only
def test_disallow_instantiation(self):
# Ensure that the type disallows instantiation (bpo-43916)
support.check_disallow_instantiation(self, type(zlib.compressobj()))
support.check_disallow_instantiation(self, type(zlib.decompressobj()))


class BaseCompressTestCase(object):
def check_big_compress_buffer(self, size, compress_func):
_1M = 1024 * 1024
# Generate 10 MiB worth of random, and expand it by repeating it.
# The assumption is that zlib's memory is not big enough to exploit
# such spread out redundancy.
data = b''.join([random.getrandbits(8 * _1M).to_bytes(_1M, 'little')
for i in range(10)])
data = random.randbytes(_1M * 10)
data = data * (size // len(data) + 1)
try:
compress_func(data)
Expand Down Expand Up @@ -498,7 +505,7 @@ def test_odd_flush(self):
# others might simply have a single RNG
gen = random
gen.seed(1)
data = genblock(1, 17 * 1024, generator=gen)
data = gen.randbytes(17 * 1024)

# compress, sync-flush, and decompress
first = co.compress(data)
Expand Down Expand Up @@ -847,20 +854,6 @@ def test_wbits(self):
self.assertEqual(dco.decompress(gzip), HAMLET_SCENE)


def genblock(seed, length, step=1024, generator=random):
"""length-byte stream of random data from a seed (in step-byte blocks)."""
if seed is not None:
generator.seed(seed)
randint = generator.randint
if length < step or step < 2:
step = length
blocks = bytes()
for i in range(0, length, step):
blocks += bytes(randint(0, 255) for x in range(step))
return blocks



def choose_lines(source, number, seed=None, generator=random):
"""Return a list of number lines randomly chosen from the source"""
if seed is not None:
Expand All @@ -869,7 +862,6 @@ def choose_lines(source, number, seed=None, generator=random):
return [generator.choice(sources) for n in range(number)]



HAMLET_SCENE = b"""
LAERTES

Expand Down