From 9604f6739db7caf3c3501bd6206d22d466846ae4 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 17 Apr 2020 22:09:54 +0200 Subject: [PATCH 1/2] bpo-40286: Use random.randbytes() in tests --- Lib/test/test_bz2.py | 2 +- Lib/test/test_lzma.py | 2 +- Lib/test/test_tarfile.py | 2 +- Lib/test/test_zipfile.py | 11 ++++------- Lib/test/test_zlib.py | 3 +-- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py index 030d564fc59e62..78b95d88faafa6 100644 --- a/Lib/test/test_bz2.py +++ b/Lib/test/test_bz2.py @@ -710,7 +710,7 @@ def testEOFError(self): def testDecompress4G(self, size): # "Test BZ2Decompressor.decompress() with >4GiB input" blocksize = 10 * 1024 * 1024 - block = random.getrandbits(blocksize * 8).to_bytes(blocksize, 'little') + block = random.randbytes(blocksize) try: data = block * (size // blocksize + 1) compressed = bz2.compress(data) diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py index f24ed3ca3d4390..0f3af27efa909c 100644 --- a/Lib/test/test_lzma.py +++ b/Lib/test/test_lzma.py @@ -350,7 +350,7 @@ def test_compressor_bigmem(self, size): def test_decompressor_bigmem(self, size): lzd = LZMADecompressor() blocksize = 10 * 1024 * 1024 - block = random.getrandbits(blocksize * 8).to_bytes(blocksize, "little") + block = random.randbytes(blocksize) try: input = block * (size // blocksize + 1) cdata = lzma.compress(input) diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index cae96802ded67e..99196f60431919 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -386,7 +386,7 @@ def test_null_tarfile(self): def test_ignore_zeros(self): # Test TarFile's ignore_zeros option. # generate 512 pseudorandom bytes - data = Random(0).getrandbits(512*8).to_bytes(512, 'big') + data = Random(0).randbytes(512) for char in (b'\0', b'a'): # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a') # are ignored correctly. diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 643c5b477bab32..29d98c8092d30c 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -16,7 +16,7 @@ from tempfile import TemporaryFile -from random import randint, random, getrandbits +from random import randint, random, randbytes from test.support import script_helper from test.support import (TESTFN, findfile, unlink, rmtree, temp_dir, temp_cwd, @@ -33,9 +33,6 @@ ('ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'), ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')] -def getrandbytes(size): - return getrandbits(8 * size).to_bytes(size, 'little') - def get_files(test): yield TESTFN2 with TemporaryFile() as f: @@ -324,7 +321,7 @@ def test_read_return_size(self): # than requested. for test_size in (1, 4095, 4096, 4097, 16384): file_size = test_size + 1 - junk = getrandbytes(file_size) + junk = randbytes(file_size) with zipfile.ZipFile(io.BytesIO(), "w", self.compression) as zipf: zipf.writestr('foo', junk) with zipf.open('foo', 'r') as fp: @@ -2423,8 +2420,8 @@ def test_open_write(self): class TestsWithMultipleOpens(unittest.TestCase): @classmethod def setUpClass(cls): - cls.data1 = b'111' + getrandbytes(10000) - cls.data2 = b'222' + getrandbytes(10000) + cls.data1 = b'111' + randbytes(10000) + cls.data2 = b'222' + randbytes(10000) def make_test_archive(self, f): # Create the ZIP archive diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py index f828b4c737a5fc..d2c6d609b74bc9 100644 --- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -134,8 +134,7 @@ def check_big_compress_buffer(self, size, compress_func): # 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) From d2fed77e42d73d760ee92be542cce428ae673eb1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 17 Apr 2020 22:34:37 +0200 Subject: [PATCH 2/2] Remove genblock() of test_zlib: use randbytes() --- Lib/test/test_zlib.py | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/Lib/test/test_zlib.py b/Lib/test/test_zlib.py index d2c6d609b74bc9..02509cdf5532c9 100644 --- a/Lib/test/test_zlib.py +++ b/Lib/test/test_zlib.py @@ -487,7 +487,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) @@ -824,20 +824,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: @@ -846,7 +832,6 @@ def choose_lines(source, number, seed=None, generator=random): return [generator.choice(sources) for n in range(number)] - HAMLET_SCENE = b""" LAERTES pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy